Skip to content

Commit

Permalink
Add server side rendering (#578)
Browse files Browse the repository at this point in the history
* Remove ember

* Switch Rails to non api mode

* Add index and show for people

* Migrate to haml instead of erb

* Remove frontend pipeline stuff

* update crud & list controller show all skills via SSR

* implement show and list for people

* add bootstrap

* remove index.html and add favicon

* add puzzleshell

* add images folder and add it to assetpipeline

* split bootstrap.scss and global scss in diffrent files

* update node version

* render template only if existing

* fix picture_controller.rb test

* split api and ssr

* xdescribe irrelevant test

* Make rubocop happy

---------

Co-authored-by: Robin Steiner <[email protected]>
Co-authored-by: Yanick Minder <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent 097e644 commit 5494691
Show file tree
Hide file tree
Showing 697 changed files with 542 additions and 71,428 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,7 @@ jobs:
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- uses: actions/setup-node@v2
with:
node-version: '14'

- name: Install Ember-Cli with NPM
run: npm install -g [email protected]
working-directory: frontend

- name: Install dependencies Yarn
run: yarn install --frozen-lockfile
working-directory: frontend
node-version: '18'

- name: Setup test database
env:
Expand All @@ -56,6 +48,3 @@ jobs:

- name: Run RuboCop
run: bundle exec rubocop --parallel

- name: Run Frontend Tests
run: bundle exec rake spec:frontend
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ config/docker/development/home/rails/.local
config/docker/development/home/rails/.yarnrc
config/docker/development/home/rails/.irb_history
config/docker/development/home/rails/.bash_history

/app/assets/builds/*
!/app/assets/builds/.keep
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ gem 'bootsnap'
gem 'carrierwave'
gem 'config'
gem 'countries'
gem 'cssbundling-rails'
gem 'database_cleaner'
gem 'faker'
gem 'haml-rails'
gem 'i18n_data'
gem 'jsbundling-rails'
gem 'keycloak-api-rails'
gem 'language_list'
gem 'mimemagic', '~> 0.3.6'
Expand All @@ -34,6 +37,7 @@ gem 'rails-i18n'
gem 'rest-client'
gem 'seed-fu'
gem 'sentry-raven'
gem 'sprockets-rails'

group :metrics do
gem 'brakeman'
Expand Down
58 changes: 43 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ GEM
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
active_model_serializers (0.10.13)
actionpack (>= 4.1, < 7.1)
activemodel (>= 4.1, < 7.1)
active_model_serializers (0.10.14)
actionpack (>= 4.1)
activemodel (>= 4.1)
case_transform (>= 0.2)
jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
activejob (7.0.4.2)
Expand Down Expand Up @@ -111,6 +111,8 @@ GEM
sixarm_ruby_unaccent (~> 1.1)
unicode_utils (~> 1.4)
crass (1.0.6)
cssbundling-rails (1.4.0)
railties (>= 6.0.0)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.0)
Expand Down Expand Up @@ -157,7 +159,7 @@ GEM
dry-equalizer (~> 0.2)
dry-initializer (~> 3.0)
dry-schema (~> 1.5, >= 1.5.2)
erubi (1.10.0)
erubi (1.12.0)
faker (2.17.0)
i18n (>= 1.6, < 2)
faraday (1.3.0)
Expand All @@ -166,8 +168,17 @@ GEM
ruby2_keywords
faraday-net_http (1.0.1)
ffi (1.16.3)
globalid (1.1.0)
activesupport (>= 5.0)
globalid (1.2.1)
activesupport (>= 6.1)
haml (6.3.0)
temple (>= 0.8.2)
thor
tilt
haml-rails (2.1.0)
actionpack (>= 5.1)
activesupport (>= 5.1)
haml (>= 4.0.6)
railties (>= 5.1)
http-accept (1.7.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
Expand All @@ -178,6 +189,8 @@ GEM
image_processing (1.12.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
jsbundling-rails (1.3.0)
railties (>= 6.0.0)
json (2.6.3)
json-jwt (1.15.3)
activesupport (>= 4.2)
Expand All @@ -192,9 +205,9 @@ GEM
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
loofah (2.19.1)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
nokogiri (>= 1.12.0)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
Expand Down Expand Up @@ -266,16 +279,18 @@ GEM
activesupport (= 7.0.4.2)
bundler (>= 1.15.0)
railties (= 7.0.4.2)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-erd (1.6.1)
activerecord (>= 4.2)
activesupport (>= 4.2)
choice (~> 0.2.0)
ruby-graphviz (~> 1.2)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails-i18n (7.0.6)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
Expand Down Expand Up @@ -355,9 +370,18 @@ GEM
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
ssrf_filter (1.1.2)
thor (1.1.0)
timeout (0.3.1)
temple (0.10.3)
thor (1.3.0)
tilt (2.3.0)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unf (0.1.4)
Expand All @@ -366,7 +390,7 @@ GEM
unicode-display_width (2.4.2)
unicode_utils (1.4.0)
uniform_notifier (1.16.0)
websocket-driver (0.7.5)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.7)
Expand All @@ -386,9 +410,12 @@ DEPENDENCIES
carrierwave
config
countries
cssbundling-rails
database_cleaner
faker
haml-rails
i18n_data
jsbundling-rails
keycloak-api-rails
language_list
listen (~> 3.0.5)
Expand Down Expand Up @@ -420,6 +447,7 @@ DEPENDENCIES
simplecov
spring
spring-watcher-listen (~> 2.0.0)
sprockets-rails
tzinfo-data

BUNDLED WITH
Expand Down
3 changes: 3 additions & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
web: env RUBY_DEBUG_OPEN=true bin/rails server
js: yarn build --watch
css: yarn watch:css
File renamed without changes.
2 changes: 2 additions & 0 deletions app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//= link_tree ../builds
//= link_tree ../images
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion public/logo.svg → app/assets/images/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/assets/stylesheets/bootstrap.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import 'bootstrap/scss/bootstrap';
@import 'bootstrap-icons/font/bootstrap-icons';
1 change: 1 addition & 0 deletions app/assets/stylesheets/styles.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "bootstrap";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ActivitiesController < PersonRelationsController
class Api::ActivitiesController < Api::PersonRelationsController
self.permitted_attrs = %i[description role technology year_from
month_from year_to month_to person_id]
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class AdvancedTrainingsController < PersonRelationsController
class Api::AdvancedTrainingsController < Api::PersonRelationsController
self.permitted_attrs = %i[description year_to month_to year_from month_from person_id]
end
4 changes: 4 additions & 0 deletions app/controllers/api/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Api::ApplicationController < ActionController::API
end
4 changes: 4 additions & 0 deletions app/controllers/api/branch_adresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Api::BranchAdressesController < Api::CrudController
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CategoriesController < CrudController
class Api::CategoriesController < Api::CrudController
self.permitted_attrs = %i[title position parent_id]

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CompaniesController < CrudController
class Api::CompaniesController < Api::CrudController
self.permitted_attrs = %i[name]
self.nested_models = %i[people]

Expand Down
130 changes: 130 additions & 0 deletions app/controllers/api/crud_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

# A generic controller to display, create, update and destroy entries of a certain model class.
class Api::CrudController < Api::ListController
class_attribute :permitted_attrs, :nested_models, :permitted_relationships

# GET /users/1
def show(options = {})
render_entry({ include: '*' }.merge(options[:render_options] || {}))
end

# POST /users
def create(options = {})
build_entry
if entry.save
render_entry({ status: :created,
location: entry_url }
.merge(options[:render_options] || {}))
else
render_errors
end
end

# PATCH/PUT /users/1
def update(options = {})
entry.attributes = model_params
if entry.save
render_entry(options[:render_options])
else
render_errors
end
end

# DELETE /users/1
def destroy(_options = {})
if entry.destroy
head :no_content
else
render_errors
end
end

private

def entry
instance_variable_get(:"@#{ivar_name}") ||
instance_variable_set(:"@#{ivar_name}", fetch_entry)
end

def fetch_entry
model_scope.find(params.fetch(:id))
end

def build_entry
instance_variable_set(:"@#{ivar_name}", model_scope.new(model_params))
end

def render_entry(options = {})
render({ json: entry,
serializer: model_serializer,
root: model_root_key }
.merge(render_options)
.merge(options || {}))
end

def render_errors
render json: entry, status: :unprocessable_entity,
adapter: :json_api, serializer: ActiveModel::Serializer::ErrorSerializer
end

def entry_url
send("#{self.class.name.underscore
.gsub(/_controller$/, '')
.gsub(/\//, '_').singularize}_url", entry)
end

# Only allow a trusted parameter "white list" through.
def model_params
attrs = params[:data][:attributes].permit(permitted_attrs)
attrs = map_relationships(attrs)
AttributeDeserializer.new(attrs, nested_models: nested_models).run
end

def map_relationships(attrs)
relationships = params[:data][:relationships]

return attrs if relationships.blank?

relationships.each do |model_name, data|
attrs[relationship_param_name(model_name)] = relationship_ids(data, model_name)
end
attrs
end

def relationship_param_name(model_name)
permitted_param?("#{model_name}_id") ? "#{model_name}_id" : "#{model_name.singularize}_ids"
end

def relationship_ids(model_data, name)
return unless model_data[:data]
return model_data[:data][:id] unless model_data[:data].is_a?(Array)

if permitted_relationship?(name)
model_data[:data].collect do |e|
e[:id]
end
end
end

def permitted_param?(attribute_name)
permitted_attrs.map(&:to_s).include?(attribute_name)
end

def permitted_relationship?(attribute_name)
permitted_relationships.map(&:to_s).include?(attribute_name)
end

def ivar_name
model_class.model_name.param_key
end

def get_asset_path(filename)
manifest_file = Rails.application.assets_manifest.assets[filename]
if manifest_file
File.join(Rails.application.assets_manifest.directory, manifest_file)
else
Rails.application.assets&.[](filename)&.filename
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/api/departments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Api::DepartmentsController < Api::CrudController
end
Loading

0 comments on commit 5494691

Please sign in to comment.