tramway-admin 1.30.0.1 → 1.32.0.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: bdea27f5059ea05e7dbe882c13f0c2e1a668a4c088e9969e0d90987ac76520bb
4
- data.tar.gz: 37c7087ffb2c9bbfc67b24852a757883a3fd98b3403eacabcef80f2a771cfa03
3
+ metadata.gz: 8ae44ca35830b2da4ed0a9d58887ab9da4e5422d79cc1c055950d25b50d348a6
4
+ data.tar.gz: 49e9923821a7995e714016c35ae3b87cd410859218b6230b3d92f90d92d9e69a
5
5
  SHA512:
6
- metadata.gz: 30d2265f4664079d95170d9f00d391f2b9be0a32f47c1e5494f1a1f87d83374dc359b5fc14a1351e932fbaa5b7fa9c9d02ee87329bb1985de0009864590bb440
7
- data.tar.gz: 4083cd8b26c5503f520d4b38a810b24134ecde85e1d1e3e816162ee6503694e4df0a50c43435a39f8f00e817cfa7da4b4fbd48b66009ebf42915631de13cc674
6
+ metadata.gz: d83925eb840eaa03df35b3b55f84229b1be0c52d3e500b05e9bc826d06ebec5cf1c0c0620cf695c6144b473fa6b87219223de5ec5350952b1296322669ccb793
7
+ data.tar.gz: 8f49db5d5bbd00419f3c15310e8fb73730062512ea91d0184175e37e17f5718a35017cffdc913261e9c843b5297438876ade41971d4434aff830eb8e9b989553
data/README.md CHANGED
@@ -99,12 +99,30 @@ 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
+ end
109
+
110
+ def list_filters
111
+ {
112
+ filter_name: {
113
+ type: :select,
114
+ select_collection: filter_collection,
115
+ query: lambda do |list, value|
116
+ list.where some_attribute: value
117
+ end
118
+ },
119
+ date_filter_name: {
120
+ type: :dates,
121
+ query: lambda do |list, begin_date, end_date|
122
+ list.where 'created_at > ? AND created_at < ?', begin_date, end_date
123
+ end
124
+ }
125
+ }
108
126
  end
109
127
  end
110
128
 
@@ -112,6 +130,23 @@ class YourModelDecorator < Tramway::Core::ApplicationDecorator
112
130
  end
113
131
  ```
114
132
 
133
+ **NOTES:**
134
+ * `collections` method must return array of scopes of `YourModel`. Every collection will be a tab in a list of your model in admin panel
135
+ * `list_filters` method returns hash of filters where:
136
+ * select_collection - collection which will be in the select of filter. It must be compatible with [options_for_select](https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select) method
137
+ * query - some Active Record query which be used as a filter of records
138
+
139
+ Filters naming:
140
+
141
+ ```yaml
142
+ en:
143
+ tramway:
144
+ admin:
145
+ filters:
146
+ model_name:
147
+ filter_name: You Filter
148
+ ```
149
+
115
150
  #### 10. Add inheritance to YourModel
116
151
 
117
152
  *app/models/your_model.rb*
@@ -138,6 +173,24 @@ class Admin::YourModelForm < Tramway::Core::ApplicationForm
138
173
  end
139
174
  end
140
175
  ```
176
+
177
+ ### 12. You can add search to your index page
178
+
179
+ Tramway use gem [PgSearch](https://github.com/Casecommons/pg_search`) as search engine
180
+
181
+ Just add `search` method to `YourModel` like this
182
+
183
+ ```ruby
184
+ search_by *attributes, **associations # `attributes` and `associations` should be the same syntax as in PgSearch
185
+ ```
186
+
187
+ Example:
188
+
189
+ ```ruby
190
+ class YourModel < Tramway::Core::ApplicationRecord
191
+ search_by :my_attribute, :another_attribute, my_association: [ :my_association_attribute, :another_my_association_attribute ]
192
+ ```
193
+
141
194
  #### 12. Run server `rails s`
142
195
  #### 13. Launch `localhost:3000/admin`
143
196
 
@@ -31,3 +31,14 @@ td.actions
31
31
  height: auto
32
32
  max-height: 30em
33
33
  overflow-x: hidden
34
+
35
+ .row-fluid.filters
36
+ width: 100%
37
+ display: flex
38
+
39
+ select
40
+ width: 100%
41
+
42
+ .submit
43
+ display: flex
44
+ flex-direction: row-reverse
@@ -33,6 +33,18 @@ module Tramway
33
33
  records = model_class.active.send(collection)
34
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
+ case decorator_class.list_filters[filter.to_sym][:type]
38
+ when :select
39
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, value) if value.present?
40
+ when :dates
41
+ begin_date = params[:list_filters][filter.to_sym][:begin_date]
42
+ end_date = params[:list_filters][filter.to_sym][:end_date]
43
+ if begin_date.present? && end_date.present?
44
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, begin_date, end_date) if value.present?
45
+ end
46
+ end
47
+ end
36
48
  hash.merge! collection => records.count
37
49
  end
38
50
  end
@@ -91,8 +103,10 @@ module Tramway
91
103
  # :tramway, :admin, :application_controller, :form_given, :model_not_included_to_tramway_admin,
92
104
  # model: params[:model]
93
105
  # )
94
- #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]})`"
95
- Tramway::Admin.forms.include? params[:form].underscore.sub(%r{^admin/}, '').sub(/_form$/, '')
106
+ # 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]})`"
107
+ if params[:form].present?
108
+ Tramway::Admin.forms.include? params[:form].underscore.sub(%r{^admin/}, '').sub(/_form$/, '')
109
+ end
96
110
  end
97
111
 
98
112
  def available_scope_given?
@@ -110,6 +124,7 @@ module Tramway
110
124
  def current_admin
111
125
  user = Tramway::User::User.find_by id: session[:admin_id]
112
126
  return false unless user
127
+
113
128
  Tramway::User::UserDecorator.decorate user
114
129
  end
115
130
 
@@ -121,7 +136,9 @@ module Tramway
121
136
  end
122
137
 
123
138
  def authenticate_admin!
124
- redirect_to '/admin/session/new' if !current_admin && !request.path.in?(['/admin/session/new', '/admin/session'])
139
+ if !current_admin && !request.path.in?(['/admin/session/new', '/admin/session'])
140
+ redirect_to '/admin/session/new'
141
+ end
125
142
  end
126
143
  end
127
144
  end
@@ -6,6 +6,18 @@ 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
+ params[:list_filters]&.each do |filter, value|
10
+ case decorator_class.list_filters[filter.to_sym][:type]
11
+ when :select
12
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, value) if value.present?
13
+ when :dates
14
+ begin_date = params[:list_filters][filter.to_sym][:begin_date]
15
+ end_date = params[:list_filters][filter.to_sym][:end_date]
16
+ if begin_date.present? && end_date.present?
17
+ records = decorator_class.list_filters[filter.to_sym][:query].call(records, begin_date, end_date) if value.present?
18
+ end
19
+ end
20
+ end
9
21
  records = records.send "#{current_admin.role}_scope", current_admin.id
10
22
  @records = decorator_class.decorate records.page params[:page]
11
23
  end
@@ -29,6 +29,7 @@ module Tramway
29
29
  def current_admin
30
30
  user = Tramway::User::User.find_by id: session[:admin_id]
31
31
  return false unless user
32
+
32
33
  Tramway::User::UserDecorator.decorate user
33
34
  end
34
35
  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
@@ -19,9 +19,9 @@
19
19
  = record.send attribute
20
20
  %td.actions
21
21
  .row
22
- &nbsp&nbsp&nbsp
22
+ &nbsp;&nbsp;&nbsp;
23
23
  = link_to fa_icon('pencil-alt'), edit_current_model_record_path(record.id), class: 'btn btn-warning btn-xs'
24
- &nbsp&nbsp
24
+ &nbsp;&nbsp;
25
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
26
  = fa_icon 'trash-alt'
27
27
  %br
@@ -1,12 +1,28 @@
1
- - if searchable_model?(model_class)
2
- .col-md-6
1
+ - if searchable_model?(model_class) || decorator_class(model_class).list_filters.any?
2
+ .col-md-8
3
3
  .search
4
- = form_tag records_path, method: :get do |f|
5
- .form-group.text
6
- .input-group
7
- = text_field_tag :search, params[:search], class: 'text form-control'
8
- = hidden_field_tag :model, params[:model]
9
- = hidden_field_tag :scope, params[:scope]
10
- = hidden_field_tag :filter, params[:filter]
11
- .input-group-append
12
- = submit_tag t('helpers.actions.search'), class: 'btn btn-primary'
4
+ = form_tag records_path, class: 'form-inline', method: :get do |f|
5
+ - if searchable_model?(model_class)
6
+ = text_field_tag :search, params[:search], class: 'text form-control'
7
+ = hidden_field_tag :model, params[:model]
8
+ = hidden_field_tag :scope, params[:scope]
9
+ = hidden_field_tag :filter, params[:filter]
10
+ - decorator_class(model_class).list_filters.each_slice(3) do |slice|
11
+ .row-fluid.filters
12
+ - slice.each do |filter|
13
+ .col-md-4
14
+ = label_tag t("admin.filters.#{model_class.to_s.underscore}.#{filter[0]}")
15
+ - case filter[1][:type]
16
+ - when :select
17
+ = select_tag "list_filters[#{filter[0]}]", build_options_for_select(filter[0], filter[1][:select_collection]), include_blank: true, class: 'form-control'
18
+ - when :dates
19
+ = text_field_tag "list_filters[#{filter[0]}][begin_date]", '', class: 'form-control', id: 'filter_datepicker_begin_date', value: params.dig(:list_filters, filter[0], :begin_date)
20
+ = text_field_tag "list_filters[#{filter[0]}][end_date]", '', class: 'form-control', id: 'filter_datepicker_end_date', value: params.dig(:list_filters, filter[0], :end_date)
21
+ :javascript
22
+ $(function () {
23
+ $('#filter_datepicker_begin_date').datepicker();
24
+ $('#filter_datepicker_end_date').datepicker();
25
+ });
26
+ .row-fluid.filters
27
+ .col-md-4.offset-md-8.submit
28
+ = submit_tag t('helpers.actions.search'), class: 'btn btn-primary'
@@ -5,7 +5,8 @@
5
5
  - tabs = get_collection
6
6
  .page-header
7
7
  .row
8
- .col-md-6
8
+ - search_render_show = searchable_model?(model_class) || decorator_class(model_class).list_filters.any?
9
+ %div{ class: "col-md-#{search_render_show ? 4 : 12}" }
9
10
  %h1
10
11
  = current_title
11
12
  = link_to fa_icon(:plus), new_current_model_record_path, class: 'btn btn-primary'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Admin
5
- VERSION = '1.30.0.1'
5
+ VERSION = '1.32.0.1'
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.30.0.1
4
+ version: 1.32.0.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: 2020-05-07 00:00:00.000000000 Z
11
+ date: 2020-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bootstrap-kaminari-views