tramway-admin 1.29.2 → 1.31.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb8572d620231b636f36ee41b63de90c82cf6bbda55bd49d43018e13a81e9bb4
4
- data.tar.gz: 38aa095dc281d92baacf40504cd56790bac6ff071dcfd238d0d6a98e4b49cd7e
3
+ metadata.gz: 85b6f6446c7992da10f8047f5307cde053f95c0cddbecd3fb8452b54b1e1f275
4
+ data.tar.gz: f1c47470978a8da9341e5cde7327f6561bdfda5f2ea8629779457ca3994f33b3
5
5
  SHA512:
6
- metadata.gz: e9534f3052c14aa43b8dbf06dbbef7c61030e4fe5e8a901dfc07863caf349af8c2d8b3da153b766cc39d3a5bf98e53e5507208c224643af79f6bc48458649b53
7
- data.tar.gz: 777695f2b36a4ee82e9848c25fefb79e40bfb35bbd214e97a0b3e6c0c676499ea24961f636883ed15a70fae36230d6a348cd2a854b07eb1cece6e3ba63dd9292
6
+ metadata.gz: 04655db55e12c76dc60edf28cf11c9e3cc73cc3256646e02363e4793522aed373300aea0c0af9391bf5a92f01391f5b72b9893426a4d7cac5eb33b9b3eb9758c
7
+ data.tar.gz: b529d3b27ce0c0a9288df0282ae0ca979ad6363446cef0cd6771b5e0279a3a42987126fc093dea569cb76ba84f48aea24bef5813ef1ef2ac19beefe099fac063
data/README.md CHANGED
@@ -99,12 +99,12 @@ Tramway::Admin.navbar_structure(
99
99
 
100
100
  #### 9. Create decorator for models
101
101
 
102
- *app/decorators/your_model_decorator.rb
102
+ *app/decorators/your_model_decorator.rb*
103
103
  ```ruby
104
104
  class YourModelDecorator < Tramway::Core::ApplicationDecorator
105
105
  class << self
106
106
  def collections
107
- [ :all ]
107
+ [ :all, :scope1, :scope2 ]
108
108
  end
109
109
  end
110
110
 
@@ -112,6 +112,8 @@ class YourModelDecorator < Tramway::Core::ApplicationDecorator
112
112
  end
113
113
  ```
114
114
 
115
+ **NOTE:** `collections` methods must return array of scopes of `YourModel`. Every collection will be a tab in a list of your model in admin panel.
116
+
115
117
  #### 10. Add inheritance to YourModel
116
118
 
117
119
  *app/models/your_model.rb*
@@ -138,6 +140,24 @@ class Admin::YourModelForm < Tramway::Core::ApplicationForm
138
140
  end
139
141
  end
140
142
  ```
143
+
144
+ ### 12. You can add search to your index page
145
+
146
+ Tramway use gem [PgSearch](https://github.com/Casecommons/pg_search`) as search engine
147
+
148
+ Just add `search` method to `YourModel` like this
149
+
150
+ ```ruby
151
+ search_by *attributes, **associations # `attributes` and `associations` should be the same syntax as in PgSearch
152
+ ```
153
+
154
+ Example:
155
+
156
+ ```ruby
157
+ class YourModel < Tramway::Core::ApplicationRecord
158
+ search_by :my_attribute, :another_attribute, my_association: [ :my_association_attribute, :another_my_association_attribute ]
159
+ ```
160
+
141
161
  #### 12. Run server `rails s`
142
162
  #### 13. Launch `localhost:3000/admin`
143
163
 
@@ -31,8 +31,13 @@ module Tramway
31
31
  def collections_counts
32
32
  @counts = decorator_class.collections.reduce({}) do |hash, collection|
33
33
  records = model_class.active.send(collection)
34
- records = records.send "#{current_user.role}_scope", current_user.id
34
+ records = records.send "#{current_admin.role}_scope", current_admin.id
35
35
  records = records.ransack(params[:filter]).result if params[:filter].present?
36
+ params[:list_filters]&.each do |filter, value|
37
+ if value.present?
38
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, value)
39
+ end
40
+ end
36
41
  hash.merge! collection => records.count
37
42
  end
38
43
  end
@@ -44,8 +49,10 @@ module Tramway
44
49
  end
45
50
 
46
51
  def notifications
47
- @notifications ||= Tramway::Admin.notificable_queries&.reduce({}) do |hash, notification|
48
- hash.merge! notification[0] => notification[1].call(current_user)
52
+ if current_admin
53
+ @notifications ||= Tramway::Admin.notificable_queries&.reduce({}) do |hash, notification|
54
+ hash.merge! notification[0] => notification[1].call(current_admin)
55
+ end
49
56
  end
50
57
  @notifications
51
58
  end
@@ -76,7 +83,7 @@ module Tramway
76
83
  end
77
84
 
78
85
  def admin_form_class
79
- "::#{current_user.role.camelize}::#{model_class}Form".constantize
86
+ "::#{current_admin.role.camelize}::#{model_class}Form".constantize
80
87
  end
81
88
 
82
89
  def model_given?
@@ -89,8 +96,8 @@ module Tramway
89
96
  # :tramway, :admin, :application_controller, :form_given, :model_not_included_to_tramway_admin,
90
97
  # model: params[:model]
91
98
  # )
92
- raise "Looks like model #{params[:model]} is not included to tramway-admin for `#{current_user.role}` role. Add it in the `config/initializers/tramway.rb`. This way `Tramway::Admin.set_available_models(#{params[:model]})`"
93
- Tramway::Admin.forms.include? params[:form].underscore.sub(%r{^admin/}, '').sub(/_form$/, '')
99
+ # raise "Looks like model #{params[:model]} is not included to tramway-admin for `#{current_admin.role}` role. Add it in the `config/initializers/tramway.rb`. This way `Tramway::Admin.set_available_models(#{params[:model]})`"
100
+ Tramway::Admin.forms.include? params[:form].underscore.sub(%r{^admin/}, '').sub(/_form$/, '') if params[:form].present?
94
101
  end
95
102
 
96
103
  def available_scope_given?
@@ -105,12 +112,25 @@ module Tramway
105
112
  check_models_given? :singleton
106
113
  end
107
114
 
115
+ def current_admin
116
+ user = Tramway::User::User.find_by id: session[:admin_id]
117
+ return false unless user
118
+
119
+ Tramway::User::UserDecorator.decorate user
120
+ end
121
+
108
122
  private
109
123
 
110
124
  def check_models_given?(model_type)
111
- models = ::Tramway::Admin.send("#{model_type}_models", role: current_user.role)
125
+ models = ::Tramway::Admin.send("#{model_type}_models", role: current_admin.role)
112
126
  models.any? && params[:model].in?(models.map(&:to_s))
113
127
  end
128
+
129
+ def authenticate_admin!
130
+ if !current_admin && !request.path.in?(['/admin/session/new', '/admin/session'])
131
+ redirect_to '/admin/session/new'
132
+ end
133
+ end
114
134
  end
115
135
  end
116
136
  end
@@ -3,8 +3,14 @@
3
3
  class Tramway::Admin::HasAndBelongsToManyRecordsController < ::Tramway::Admin::ApplicationController
4
4
  def create
5
5
  base_object = params[:model_class].constantize.find params[:object_id]
6
- record_form = params[:form].constantize.new base_object
7
- if record_form.submit params[params[:model_class].underscore]
6
+ form_class = params[:form].constantize
7
+ record_form = form_class.new base_object
8
+ sending_params = if params[params[:model_class].underscore].present?
9
+ params[params[:model_class].underscore]
10
+ else
11
+ params[form_class.associated_as]
12
+ end
13
+ if record_form.submit sending_params
8
14
  redirect_to params[:redirect].present? ? params[:redirect] : record_path(base_object, model: base_object.class)
9
15
  else
10
16
  redirect_to params[:redirect].present? ? params[:redirect] : record_path(base_object, model: base_object.class)
@@ -6,7 +6,12 @@ class Tramway::Admin::RecordsController < ::Tramway::Admin::ApplicationControlle
6
6
  records = model_class.active.order(id: :desc).send scope
7
7
  records = records.full_text_search params[:search] if params[:search].present?
8
8
  records = records.ransack(params[:filter]).result if params[:filter].present?
9
- records = records.send "#{current_user.role}_scope", current_user.id
9
+ params[:list_filters]&.each do |filter, value|
10
+ if value.present?
11
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, value)
12
+ end
13
+ end
14
+ records = records.send "#{current_admin.role}_scope", current_admin.id
10
15
  @records = decorator_class.decorate records.page params[:page]
11
16
  end
12
17
 
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tramway::Admin::SessionsController < ::Tramway::Admin::ApplicationController
4
+ before_action :redirect_if_signed_in, except: :destroy
5
+ skip_before_action :check_available!
6
+ skip_before_action :collections_counts
7
+
8
+ def new
9
+ @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.new
10
+ end
11
+
12
+ def create
13
+ @session_form = ::Tramway::Auth::SessionForm.new ::Tramway::User::User.active.find_or_initialize_by email: params[:user][:email]
14
+ if @session_form.validate params[:user]
15
+ admin_sign_in @session_form.model
16
+ redirect_to Tramway::Admin::Engine.routes.url_helpers.root_path
17
+ else
18
+ render :new
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ admin_sign_out
24
+ redirect_to '/admin/session/new'
25
+ end
26
+
27
+ private
28
+
29
+ def redirect_if_signed_in
30
+ redirect_to Tramway::Admin::Engine.routes.url_helpers.root_path if current_admin
31
+ end
32
+
33
+ def admin_sign_in(user)
34
+ session[:admin_id] = user.id
35
+ end
36
+
37
+ def admin_sign_out
38
+ session[:admin_id] = nil
39
+ end
40
+ end
@@ -6,7 +6,7 @@ module Tramway::Admin::ActionsHelper
6
6
  association_object,
7
7
  project: (@application_engine || @application.name),
8
8
  model_name: association_object.model.class.name,
9
- role: current_user.role,
9
+ role: current_admin.role,
10
10
  action: :destroy
11
11
  )
12
12
  end
@@ -16,7 +16,7 @@ module Tramway::Admin::ActionsHelper
16
16
  association_object,
17
17
  project: (@application_engine || @application.name),
18
18
  model_name: association_object.model.class.name,
19
- role: current_user.role,
19
+ role: current_admin.role,
20
20
  action: :update
21
21
  )
22
22
  end
@@ -4,7 +4,6 @@ module Tramway
4
4
  module Admin
5
5
  module ApplicationHelper
6
6
  include ::FontAwesome5::Rails::IconHelper
7
- include AuthManagement
8
7
  include AdditionalButtonsBuilder
9
8
  include ::SmartButtons
10
9
  include CasesHelper
@@ -26,6 +25,13 @@ module Tramway
26
25
  end
27
26
  ::Tramway::Admin.available_models_for(@application_engine || @application.name).map(&:to_s).include?(object_class_name) ? :record : :singleton
28
27
  end
28
+
29
+ def current_admin
30
+ user = Tramway::User::User.find_by id: session[:admin_id]
31
+ return false unless user
32
+
33
+ Tramway::User::UserDecorator.decorate user
34
+ end
29
35
  end
30
36
  end
31
37
  end
@@ -47,6 +47,11 @@ module Tramway::Admin
47
47
  model_class.methods.include? :full_text_search
48
48
  end
49
49
 
50
+ def build_options_for_select(name, collection)
51
+ selected_value = params[:list_filters].present? ? params[:list_filters][name] : nil
52
+ options_for_select(collection, selected_value)
53
+ end
54
+
50
55
  def admin_index_path_of_model(model_class, tab, filter)
51
56
  if tab
52
57
  records_path model: model_class, filter: filter, scope: tab
@@ -7,9 +7,9 @@
7
7
  %button.navbar-toggler.collapsed{ aria: { controls: :navbar, expanded: "false", label: 'Toggle Navigation' }, data: { target: "#navbar", toggle: :collapse }, type: :button }
8
8
  %span.navbar-toggler-icon
9
9
  .navbar-collapse.collapse#navbar
10
- - if signed_in?
10
+ - if current_admin
11
11
  %ul.navbar-nav
12
- - ::Tramway::Admin.navbar_items_for(@application_engine || @application.name, role: current_user.role)&.each do |item|
12
+ - ::Tramway::Admin.navbar_items_for(@application_engine || @application.name, role: current_admin.role)&.each do |item|
13
13
  - case item.keys.first
14
14
  - when Class
15
15
  - model = item.keys.first
@@ -31,7 +31,7 @@
31
31
  = dropdown_model_item model: model, route: ::Tramway::Admin::Engine.routes.url_helpers.records_path(model: model, scope: decorator_class(model).collections.first), pluralize: plural(model.model_name).capitalize
32
32
 
33
33
  %ul.nav.navbar-nav.ml-auto
34
- - if signed_in?
34
+ - if current_admin
35
35
  - if @notifications_count.present?
36
36
  - if @notifications_count > 0
37
37
  %li.nav-item.dropdown.notifications
@@ -50,7 +50,9 @@
50
50
  %span.badge.badge-light
51
51
  = @notifications_count
52
52
  %li.nav-item
53
- = link_to Tramway::Auth::Engine.routes.url_helpers.session_path, method: :delete, class: 'nav-link' do
53
+ -# FIXME url_helpers return with /admin for some reason
54
+ -#= link_to Tramway::Auth::Engine.routes.url_helpers.session_path(model: Tramway::User::User).sub('/admin', ''), method: :delete, class: 'nav-link' do
55
+ = link_to '/admin/sign_out', class: 'nav-link' do
54
56
  = fa_icon 'sign-out-alt'
55
57
  = t('helpers.links.sign_out')
56
58
  - else
@@ -18,9 +18,12 @@
18
18
  %td
19
19
  = record.send attribute
20
20
  %td.actions
21
- = link_to fa_icon('pencil-alt'), edit_current_model_record_path(record.id), class: 'btn btn-warning btn-xs'
22
- = delete_button url: current_model_record_path(record.id), form_options: { class: :smart_button }, button_options: { class: 'btn btn-xs btn-danger' } do
23
- = fa_icon 'trash-alt'
21
+ .row
22
+ &nbsp;&nbsp;&nbsp;
23
+ = link_to fa_icon('pencil-alt'), edit_current_model_record_path(record.id), class: 'btn btn-warning btn-xs'
24
+ &nbsp;&nbsp;
25
+ = delete_button url: current_model_record_path(record.id), form_options: { class: :smart_button }, button_options: { class: 'btn btn-xs btn-danger' } do
26
+ = fa_icon 'trash-alt'
24
27
  %br
25
28
  %br
26
29
  .btn-group{ data: { toggle: :buttons } }
@@ -1,12 +1,15 @@
1
- - if searchable_model?(model_class)
1
+ - if searchable_model?(model_class) || decorator_class(model_class).list_filters.any?
2
2
  .col-md-6
3
3
  .search
4
4
  = form_tag records_path, method: :get do |f|
5
5
  .form-group.text
6
6
  .input-group
7
- = text_field_tag :search, params[:search], class: 'text form-control'
7
+ - if searchable_model?(model_class)
8
+ = text_field_tag :search, params[:search], class: 'text form-control'
8
9
  = hidden_field_tag :model, params[:model]
9
10
  = hidden_field_tag :scope, params[:scope]
10
11
  = hidden_field_tag :filter, params[:filter]
12
+ - decorator_class(model_class).list_filters.each do |filter|
13
+ = select_tag "list_filters[#{filter[0]}]", build_options_for_select(filter[0], filter[1][:select_collection]), include_blank: true, class: 'form-control'
11
14
  .input-group-append
12
15
  = submit_tag t('helpers.actions.search'), class: 'btn btn-primary'
@@ -0,0 +1,9 @@
1
+ - title
2
+ .row
3
+ .col-md-6
4
+ = simple_form_for @session_form.model, url: '/admin/session', method: :post, html: { class: 'form-horizontal' } do |f|
5
+ = f.input :email, as: :string
6
+ = f.input :password
7
+ = hidden_field_tag :model, @session_form.model.class.to_s
8
+ = hidden_field_tag :redirect, Tramway::Admin::Engine.routes.url_helpers.root_path
9
+ = f.button :submit, t('helpers.links.enter'), class: 'btn btn-success'
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Tramway::Admin::Engine.routes.draw do
4
- mount Tramway::Auth::Engine, at: '/auth'
5
4
  mount Tramway::Export::Engine, at: '/' if defined? Tramway::Export::Engine
6
5
 
7
6
  root to: 'welcome#index'
@@ -9,4 +8,6 @@ Tramway::Admin::Engine.routes.draw do
9
8
  resources :records
10
9
  resource :singleton, only: %i[new create show edit update]
11
10
  resources :has_and_belongs_to_many_records, only: %i[create destroy]
11
+ resource :session, only: %i[new create]
12
+ get 'sign_out', to: 'sessions#destroy'
12
13
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Admin
5
- VERSION = '1.29.2'
5
+ VERSION = '1.31.0.2'
6
6
  end
7
7
  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: 1.29.2
4
+ version: 1.31.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
11
+ date: 2020-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bootstrap-kaminari-views
@@ -183,6 +183,7 @@ files:
183
183
  - app/controllers/tramway/admin/application_controller.rb
184
184
  - app/controllers/tramway/admin/has_and_belongs_to_many_records_controller.rb
185
185
  - app/controllers/tramway/admin/records_controller.rb
186
+ - app/controllers/tramway/admin/sessions_controller.rb
186
187
  - app/controllers/tramway/admin/singletons_controller.rb
187
188
  - app/controllers/tramway/admin/welcome_controller.rb
188
189
  - app/controllers/tramway/export/application_controller.rb
@@ -207,6 +208,7 @@ files:
207
208
  - app/views/tramway/admin/records/index.html.haml
208
209
  - app/views/tramway/admin/records/new.html.haml
209
210
  - app/views/tramway/admin/records/show.html.haml
211
+ - app/views/tramway/admin/sessions/new.html.haml
210
212
  - app/views/tramway/admin/shared/_show.html.haml
211
213
  - app/views/tramway/admin/shared/errors/server_error.html.haml
212
214
  - app/views/tramway/admin/shared/show/_attribute_tr.html.haml