From f98de3b66b35052bafa19786c4290de80079e13b Mon Sep 17 00:00:00 2001 From: Karol Date: Thu, 26 Nov 2020 17:11:18 +0100 Subject: [PATCH] Add Rails admin --- Gemfile | 1 + Gemfile.lock | 43 ++++++++++++++ app/policies/application_policy.rb | 3 + app/views/application/_navigation.html.slim | 1 + config/initializers/rails_admin.rb | 66 +++++++++++++++++++++ config/locales/en.yml | 2 + config/routes.rb | 1 + 7 files changed, 117 insertions(+) create mode 100644 config/initializers/rails_admin.rb diff --git a/Gemfile b/Gemfile index 5f246b35..c20882b7 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'faraday' gem 'proforma', git: 'https://github.com/openHPI/proforma.git', tag: 'v0.5' gem 'whenever', require: false gem 'rails-timeago' +gem 'rails_admin', '~> 2.0' # Error Tracing gem 'concurrent-ruby' diff --git a/Gemfile.lock b/Gemfile.lock index 9bea828f..e4c4f210 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,6 +50,10 @@ GEM globalid (>= 0.3.6) activemodel (5.2.4.4) activesupport (= 5.2.4.4) + activemodel-serializers-xml (1.0.2) + activemodel (> 5.x) + activesupport (> 5.x) + builder (~> 3.1) activerecord (5.2.4.4) activemodel (= 5.2.4.4) activesupport (= 5.2.4.4) @@ -139,6 +143,9 @@ GEM forgery (0.8.1) globalid (0.4.2) activesupport (>= 4.2.0) + haml (5.2.0) + temple (>= 0.8.0) + tilt hashdiff (1.0.1) headless (2.3.1) highline (2.0.3) @@ -157,8 +164,26 @@ GEM oauth (>= 0.4.5, < 0.6) jbuilder (2.10.1) activesupport (>= 5.0.0) + jquery-rails (4.4.0) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + jquery-ui-rails (6.0.1) + railties (>= 3.2.16) json (2.3.1) jwt (2.2.2) + kaminari (1.2.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.1) + kaminari-activerecord (= 1.2.1) + kaminari-core (= 1.2.1) + kaminari-actionview (1.2.1) + actionview + kaminari-core (= 1.2.1) + kaminari-activerecord (1.2.1) + activerecord + kaminari-core (= 1.2.1) + kaminari-core (1.2.1) kramdown (2.3.0) rexml listen (3.3.1) @@ -192,6 +217,7 @@ GEM multi_json (1.15.0) multi_xml (0.6.0) multipart-post (2.1.1) + nested_form (0.3.2) netrc (0.11.0) newrelic_rpm (6.14.0) nio4r (2.5.4) @@ -231,6 +257,9 @@ GEM rack (2.2.3) rack-mini-profiler (2.2.0) rack (>= 1.2.0) + rack-pjax (1.1.0) + nokogiri (~> 1.5) + rack (>= 1.1) rack-proxy (0.6.5) rack rack-test (1.1.0) @@ -263,6 +292,18 @@ GEM rails-timeago (2.19.0) actionpack (>= 3.1) activesupport (>= 3.1) + rails_admin (2.0.2) + activemodel-serializers-xml (>= 1.0) + builder (~> 3.1) + haml (>= 4.0, < 6) + jquery-rails (>= 3.0, < 5) + jquery-ui-rails (>= 5.0, < 7) + kaminari (>= 0.14, < 2.0) + nested_form (~> 0.3) + rack-pjax (>= 0.7) + rails (>= 5.0, < 7) + remotipart (~> 1.3) + sassc-rails (>= 1.3, < 3) railties (5.2.4.4) actionpack (= 5.2.4.4) activesupport (= 5.2.4.4) @@ -280,6 +321,7 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) regexp_parser (1.8.2) + remotipart (1.4.4) rest-client (2.1.0) http-accept (>= 1.7.0, < 2.0) http-cookie (>= 1.0.2, < 2.0) @@ -460,6 +502,7 @@ DEPENDENCIES rails-controller-testing rails-i18n rails-timeago + rails_admin (~> 2.0) ransack rest-client rspec-autotest diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 28f33b7c..f92d7fcc 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -76,4 +76,7 @@ class ApplicationPolicy end private :require_user! end + [:dashboard?, :index?, :new?, :export?, :bulk_delete?, :show?, :edit?, :delete?, :show_in_app?,].each do |action| + define_method(action) { admin? } + end end diff --git a/app/views/application/_navigation.html.slim b/app/views/application/_navigation.html.slim index 5921a208..c28939d5 100644 --- a/app/views/application/_navigation.html.slim +++ b/app/views/application/_navigation.html.slim @@ -7,6 +7,7 @@ ul.dropdown-menu.p-0.mt-1 role='menu' - if current_user.admin? li = link_to(t('breadcrumbs.dashboard.show'), admin_dashboard_path, class: 'dropdown-item', 'data-turbolinks' => "false") if policy([:admin, :dashboard]).show? + li = link_to(t('breadcrumbs.rails_admin.show'), rails_admin.dashboard_path, class: 'dropdown-item', 'data-turbolinks' => "false") if policy([:admin, :dashboard]).show? li = link_to(t('breadcrumbs.statistics.show'), statistics_path, class: 'dropdown-item') if policy(:statistics).show? li.dropdown-divider role='separator' = render('navigation_submenu', title: t('activerecord.models.exercise.other'), diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb new file mode 100644 index 00000000..ccef6bdc --- /dev/null +++ b/config/initializers/rails_admin.rb @@ -0,0 +1,66 @@ +RailsAdmin.config do |config| + + ### Popular gems integration + + ## == Devise == + # config.authenticate_with do + # warden.authenticate! scope: :user + # end + # config.current_user_method(&:current_user) + + ## == CancanCan == + # config.authorize_with :cancancan + + ## == Pundit == + # config.authorize_with :pundit + config.authorize_with do + redirect_to main_app.root_path unless current_user.admin? + # unless current_user.admin? + # flash[:alert] = 'Access denied.' + # redirect_to main_app.root_path + # end + end + + ## == PaperTrail == + # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0 + + ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration + + ## == Gravatar integration == + ## To disable Gravatar integration in Navigation Bar set to false + # config.show_gravatar = true + + config.actions do + dashboard # mandatory + index # mandatory + new + export + bulk_delete + show + edit + delete + show_in_app + + ## With an audit adapter, you can add: + # history_index + # history_show + end + + # stolen from https://github.com/kaminari/kaminari/issues/162#issuecomment-52083985 + if defined?(WillPaginate) + module WillPaginate + module ActiveRecord + module RelationMethods + def per(value = nil) per_page(value) end + def total_count() count end + def first_page?() self == first end + def last_page?() self == last end + end + end + module CollectionMethods + alias_method :num_pages, :total_pages + end + end + end +end + diff --git a/config/locales/en.yml b/config/locales/en.yml index c0dad445..aa552bbc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -261,6 +261,8 @@ en: graphs: "Graphs" user_activity_history: User Activity History rfc_activity_history: RfC Activity History + rails_admin: + show: "Rails admin" consumers: show: link: Consumer diff --git a/config/routes.rb b/config/routes.rb index 74f2ac81..2e00d9f4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ FILENAME_REGEXP = /[\w\.]+/ unless Kernel.const_defined?(:FILENAME_REGEXP) Rails.application.routes.draw do + mount RailsAdmin::Engine => '/rails_admin', as: 'rails_admin' resources :error_template_attributes resources :error_templates do member do