tramway-admin 4.0.1 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f49bf5b4e9e0ac337b90230037a618c730eb92fc0d11c2139a8452e4a44fc66
4
- data.tar.gz: 1560fc7b9a504b76a78a3272f9feae6c672add55ce468255aee7593c4453f1dd
3
+ metadata.gz: 52f598a393f67ead5da2a67ef98a7b721edea4cda7c5968dcc5fe2224c75ba11
4
+ data.tar.gz: 4ea29fb442be1e667e843c813dd4dba340672add79d93437abd3226852e6f8a8
5
5
  SHA512:
6
- metadata.gz: 27ce4b592fed1ea8a20b2c164187947313a35f044bea80cffa324647fad7191518e97b37e2b1eb6e0d2e23eae3d2afec95bddcdfbb03789ca5d93fbe1a3092f8
7
- data.tar.gz: 3d44b49c30c6fea3c44db3e934613e8341cc0198e0c18280c6990965eedd91befa1bd8589a7c34f10816f4afe281830484839ecf54d5dbf144c281b519c45cd9
6
+ metadata.gz: 00645c93a5d1e36ad76fe059a9fd775146614cb8be7046c119f186bf9f81b00aefe636af0e56cebdebbe8abee5b4b288d3e3f0156bff8e35d336a574c13b555c
7
+ data.tar.gz: cdc9339fac651475ea6853d70096cf2ad3c7826d80668d61feb96012749e16e3f37d09310fbe4dfdedaf58a2457cae8b794ad82be98ea9b1895032d60f12fae6
data/README.md CHANGED
@@ -574,6 +574,13 @@ end
574
574
 
575
575
  * **Model or Form is not available** - `params[:model]` or `params[:form]` is empty **OR** current user does not have access to model or form in `params[:model]` or `params[:form]`
576
576
 
577
+ ## Change admin user base model
578
+
579
+ *config/initializers/tramway.rb*
580
+ ```ruby
581
+ ::Tramway::Admin.auth_config = { user_model: User, auth_attributes: %i[email username] }
582
+ ```
583
+
577
584
  ## Good features
578
585
 
579
586
  ### Get actions log in admin panel
@@ -20,6 +20,10 @@ module Tramway
20
20
 
21
21
  protected
22
22
 
23
+ def admin_model
24
+ ::Tramway::Admin.admin_model
25
+ end
26
+
23
27
  def check_available!
24
28
  raise 'Tramway::Admin - Model or Form is not available. Looks like current user does not have access to change this model. Update your tramway initializer file' if !model_given? && !form_given?
25
29
  end
@@ -120,7 +124,7 @@ module Tramway
120
124
  end
121
125
 
122
126
  def current_admin
123
- user = Tramway::User::User.find_by id: session[:admin_id]
127
+ user = admin_model.find_by id: session[:admin_id]
124
128
  return false unless user
125
129
 
126
130
  Tramway::User::UserDecorator.decorate user
@@ -6,11 +6,11 @@ class Tramway::Admin::SessionsController < ::Tramway::Admin::ApplicationControll
6
6
  skip_before_action :collections_counts
7
7
 
8
8
  def new
9
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.new
9
+ @session_form = ::Tramway::Auth::SessionForm.new admin_model.new
10
10
  end
11
11
 
12
12
  def create
13
- @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.find_or_initialize_by email: params[:user][:email]
13
+ @session_form = ::Tramway::Auth::SessionForm.new admin_model.find_or_initialize_by email: params[:user][:email]
14
14
  if @session_form.validate params[:user]
15
15
  admin_sign_in @session_form.model
16
16
  redirect_to Tramway::Admin::Engine.routes.url_helpers.root_path
@@ -17,6 +17,7 @@ module Tramway
17
17
  include ::Tramway::Collections::Helper
18
18
  include ::Tramway::Core::CopyToClipboardHelper
19
19
  include ::Tramway::Admin::TramwayModelHelper
20
+ include ::Tramway::Admin::FrontendHelper
20
21
 
21
22
  def object_type(object)
22
23
  object_class_name = if object.class.ancestors.include? ::Tramway::Core::ApplicationDecorator
@@ -28,7 +29,7 @@ module Tramway
28
29
  end
29
30
 
30
31
  def current_admin
31
- user = Tramway::User::User.find_by id: session[:admin_id]
32
+ user = Tramway::Admin.admin_model.find_by id: session[:admin_id]
32
33
  return false unless user
33
34
 
34
35
  Tramway::User::UserDecorator.decorate user
@@ -0,0 +1,21 @@
1
+ module Tramway::Admin::FrontendHelper
2
+ def react_params(form, action, method)
3
+ url = case action
4
+ when :create
5
+ Tramway::Admin::Engine.routes.url_helpers.records_path(model: form.model.class)
6
+ else
7
+ Tramway::Admin::Engine.routes.url_helpers.record_path(form.model.id, model: form.model.class)
8
+ end
9
+ form.properties.reduce({ action: url, method: method, authenticity_token: form_authenticity_token }) do |hash, property|
10
+ case property[1]
11
+ when :association
12
+ hash.merge!(
13
+ property[0] => {
14
+ collection: build_collection_for_association(form, property[0])
15
+ }
16
+ )
17
+ end
18
+ hash
19
+ end.merge model: form.model.attributes
20
+ end
21
+ end
@@ -8,6 +8,10 @@
8
8
  = javascript_include_tag 'tramway/admin/application'
9
9
  - if File.exists?("#{Rails.root}/app/assets/javascripts/admin/application.js") || File.exists?("#{Rails.root}/app/assets/javascripts/admin/application.js.coffee")
10
10
  = javascript_include_tag 'admin/application'
11
+ - if File.exists?("#{Rails.root}/app/assets/stylesheets/admin/application.css") || File.exists?("#{Rails.root}/app/assets/stylesheets/admin/application.scss")
12
+ = stylesheet_link_tag 'admin/application'
13
+ - if File.exists?("#{Rails.root}/app/javascript/packs/admin/application.js")
14
+ = javascript_pack_tag 'admin/application'
11
15
  = csrf_meta_tags
12
16
  - if @application.favicon.present?
13
17
  = favicon_link_tag @application.favicon
@@ -1,5 +1,4 @@
1
1
  %nav.navbar.navbar-expand-md.navbar-dark.bg-dark
2
- -# FIXME use helper methods
3
2
  - if ::Tramway::Admin.customized_admin_navbar.present?
4
3
  = ::Tramway::Admin.customized_admin_navbar
5
4
  - else
@@ -54,8 +53,6 @@
54
53
  %span.badge.badge-light
55
54
  = @notifications_count
56
55
  %li.nav-item
57
- -# FIXME url_helpers return with /admin for some reason
58
- -#= link_to Tramway::Auth::Engine.routes.url_helpers.session_path(model: Tramway::User::User).sub('/admin', ''), method: :delete, class: 'nav-link' do
59
56
  = link_to '/admin/sign_out', class: 'nav-link' do
60
57
  = fa_icon 'sign-out-alt'
61
58
  = t('helpers.links.sign_out')
@@ -8,12 +8,15 @@
8
8
  = render 'tramway/core/shared/messages', object: @record_form
9
9
  .row
10
10
  .col-lg-12
11
- = simple_form_for @record_form, url: { controller: :records, action: action, model: @record_form.model.class }, method: method, input_html: { class: 'form-horizontal' } do |f|
12
- - @record_form.properties.each do |property, type|
13
- = render 'tramway/core/shared/input', property: property, object: :record, type: type, form: f, destination: :admin, value: value_from_params(model_class: model_class, property: property, type: type)
14
- = hidden_field_tag :redirect, params[:redirect]
15
- = f.button :submit, t('helpers.links.save'), class: 'btn btn-success'
16
- = link_to t('helpers.links.back'), current_model_records_path, class: 'btn btn-secondary'
11
+ - if @record_form.class.is_react_component?
12
+ = react_component "#{@record_form.class.name.gsub('::', '/')}", react_params(@record_form, action, method)
13
+ - else
14
+ = simple_form_for @record_form, url: { controller: :records, action: action, model: @record_form.model.class }, method: method, input_html: { class: 'form-horizontal' } do |f|
15
+ - @record_form.properties.each do |property, type|
16
+ = render 'tramway/core/shared/input', property: property, object: :record, type: type, form: f, destination: :admin, value: value_from_params(model_class: model_class, property: property, type: type)
17
+ = hidden_field_tag :redirect, params[:redirect]
18
+ = f.button :submit, t('helpers.links.save'), class: 'btn btn-success'
19
+ = link_to t('helpers.links.back'), current_model_records_path, class: 'btn btn-secondary'
17
20
 
18
21
  -# NOTES
19
22
  -# * value_from_params helper is in tramway-core gem app/helpers/inputs_helpers.rb
@@ -3,8 +3,9 @@
3
3
  .d-flex.flex-row-reverse.filters
4
4
  = form_tag records_path, class: 'form-inline', method: :get do |f|
5
5
  - if searchable_model?(model_class)
6
- .col-md-8
6
+ .input-group
7
7
  = text_field_tag :search, params[:search], class: 'text form-control'
8
+ = submit_tag t('helpers.actions.search'), class: 'btn btn-primary'
8
9
  = hidden_field_tag :model, params[:model]
9
10
  = hidden_field_tag :scope, params[:scope]
10
11
  = hidden_field_tag :filter, (params[:filter].is_a?(ActionController::Parameters) ? params[:filter].permit!.to_h.to_json : params[:filter])
@@ -31,5 +32,3 @@
31
32
  $('#filter_datepicker_begin_date').datepicker();
32
33
  $('#filter_datepicker_end_date').datepicker();
33
34
  });
34
- .col-md-4.submit
35
- = submit_tag t('helpers.actions.search'), class: 'btn btn-primary'
@@ -15,7 +15,7 @@
15
15
  .col
16
16
  = render 'search', model_class: model_class
17
17
  %hr
18
- %ul.nav.nav-tabs
18
+ %ul.nav.nav-tabs.mb-3
19
19
  - if params[:search].present?
20
20
  %li.nav-item
21
21
  = link_to search_tab_title(@records.total_count), '#', class: 'nav-link active'
@@ -7,7 +7,7 @@
7
7
  %td
8
8
  = model_class.human_attribute_name association.name
9
9
  %hr
10
- - if create_is_available?(association.name) && (association_type != :has_one || !object.send(association.name).present?)
10
+ - if create_is_available?(association.class_name) && (association_type != :has_one || !object.send(association.name).present?)
11
11
  = link_to "#{I18n.t('helpers.actions.add')} #{model_class.human_attribute_name(association.name).singularize.downcase}", new_associated_record_path(association: association, object: object, as: object.send("#{association.name}_as")), class: 'btn btn-primary'
12
12
  %td{ colspan: 2 }
13
13
  - if association_type.in? [ :has_one, :belongs_to ]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Admin
5
- VERSION = '4.0.1'
5
+ VERSION = '4.1.1'
6
6
  end
7
7
  end
data/lib/tramway/admin.rb CHANGED
@@ -79,6 +79,36 @@ module Tramway
79
79
  new_hash.merge! pair[0].to_s => pair[1]
80
80
  end
81
81
  end
82
+
83
+ def admin_model
84
+ @@auth_config.first[:user_model]
85
+ end
86
+
87
+ def auth_config
88
+ @@auth_config ||= [{ user_model: ::Tramway::User::User, auth_attributes: :email }]
89
+ end
90
+
91
+ def auth_config=(params)
92
+ if params.is_a? Hash
93
+ @@auth_config = [params]
94
+ elsif params.is_a? Array
95
+ @@auth_config = params
96
+ end
97
+ end
98
+
99
+ def user_based_models
100
+ @@auth_config ||= []
101
+ @@auth_config.map do |conf|
102
+ conf[:user_model]
103
+ end
104
+ end
105
+
106
+ def auth_attributes
107
+ @@auth_config ||= []
108
+ @@auth_config.reduce({}) do |hash, conf|
109
+ hash.merge! conf[:user_model] => conf[:auth_attributes]
110
+ end
111
+ end
82
112
  end
83
113
  end
84
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tramway-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-10 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tramway-core
@@ -256,6 +256,7 @@ files:
256
256
  - app/helpers/tramway/admin/application_helper.rb
257
257
  - app/helpers/tramway/admin/cases_helper.rb
258
258
  - app/helpers/tramway/admin/focus_generator_helper.rb
259
+ - app/helpers/tramway/admin/frontend_helper.rb
259
260
  - app/helpers/tramway/admin/navbar_helper.rb
260
261
  - app/helpers/tramway/admin/records_helper.rb
261
262
  - app/helpers/tramway/admin/russian_cases_helper.rb