godmin 0.10.3 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/CHANGELOG.md +8 -0
  4. data/README.md +146 -81
  5. data/app/assets/javascripts/godmin/batch-actions.js +18 -13
  6. data/app/assets/stylesheets/godmin/index.css.scss +15 -8
  7. data/app/views/godmin/resource/_batch_actions.html.erb +2 -4
  8. data/app/views/godmin/resource/_breadcrumb.html.erb +0 -3
  9. data/app/views/godmin/resource/_button_actions.html.erb +2 -2
  10. data/app/views/godmin/resource/_filters.html.erb +17 -18
  11. data/app/views/godmin/resource/_form.html.erb +1 -1
  12. data/app/views/godmin/resource/_pagination.html.erb +11 -11
  13. data/app/views/godmin/resource/_scopes.html.erb +4 -4
  14. data/app/views/godmin/resource/_table.html.erb +30 -30
  15. data/app/views/godmin/resource/index.html.erb +3 -10
  16. data/godmin.gemspec +4 -1
  17. data/lib/generators/godmin/authentication/templates/sessions_controller.rb +1 -1
  18. data/lib/generators/godmin/install/install_generator.rb +1 -1
  19. data/lib/generators/godmin/resource/resource_generator.rb +4 -0
  20. data/lib/generators/godmin/resource/templates/resource_controller.rb +1 -9
  21. data/lib/generators/godmin/resource/templates/resource_service.rb +8 -0
  22. data/lib/godmin.rb +4 -2
  23. data/lib/godmin/{application.rb → application_controller.rb} +1 -1
  24. data/lib/godmin/authentication.rb +1 -1
  25. data/lib/godmin/authentication/{sessions.rb → sessions_controller.rb} +1 -1
  26. data/lib/godmin/authorization.rb +1 -1
  27. data/lib/godmin/authorization/policy_finder.rb +3 -1
  28. data/lib/godmin/helpers/application.rb +10 -0
  29. data/lib/godmin/helpers/batch_actions.rb +7 -4
  30. data/lib/godmin/helpers/filters.rb +72 -73
  31. data/lib/godmin/helpers/tables.rb +1 -3
  32. data/lib/godmin/paginator.rb +47 -0
  33. data/lib/godmin/rails.rb +1 -6
  34. data/lib/godmin/resolver.rb +7 -2
  35. data/lib/godmin/resources/resource_controller.rb +170 -0
  36. data/lib/godmin/resources/resource_service.rb +82 -0
  37. data/lib/godmin/resources/resource_service/batch_actions.rb +38 -0
  38. data/lib/godmin/resources/resource_service/filters.rb +37 -0
  39. data/lib/godmin/resources/resource_service/ordering.rb +27 -0
  40. data/lib/godmin/resources/resource_service/pagination.rb +22 -0
  41. data/lib/godmin/resources/resource_service/scopes.rb +61 -0
  42. data/lib/godmin/version.rb +1 -1
  43. data/test/dummy/config/environments/production.rb +1 -1
  44. data/test/dummy/config/environments/test.rb +1 -1
  45. data/test/dummy/db/schema.rb +16 -0
  46. data/test/lib/godmin/helpers/filters_test.rb +26 -0
  47. data/test/lib/godmin/paginator_test.rb +84 -0
  48. data/test/lib/godmin/policy_finder_test.rb +35 -6
  49. data/test/lib/godmin/resolver_test.rb +6 -0
  50. data/test/lib/godmin/resources/resource_service/batch_actions_test.rb +45 -0
  51. data/test/lib/godmin/resources/resource_service/filters_test.rb +32 -0
  52. data/test/lib/godmin/resources/resource_service/ordering_test.rb +37 -0
  53. data/test/lib/godmin/resources/resource_service/pagination_test.rb +31 -0
  54. data/test/lib/godmin/resources/resource_service/scopes_test.rb +57 -0
  55. data/test/lib/godmin/resources/resource_service_test.rb +21 -0
  56. data/test/test_helper.rb +62 -0
  57. metadata +75 -17
  58. data/.hound.yml +0 -3
  59. data/lib/godmin/resource.rb +0 -177
  60. data/lib/godmin/resource/batch_actions.rb +0 -45
  61. data/lib/godmin/resource/filters.rb +0 -41
  62. data/lib/godmin/resource/ordering.rb +0 -25
  63. data/lib/godmin/resource/pagination.rb +0 -64
  64. data/lib/godmin/resource/scopes.rb +0 -54
  65. data/test/dummy/db/test.sqlite3 +0 -0
data/.hound.yml DELETED
@@ -1,3 +0,0 @@
1
- ruby:
2
- enabled: true
3
- config_file: .rubocop.yml
@@ -1,177 +0,0 @@
1
- require "godmin/helpers/batch_actions"
2
- require "godmin/helpers/filters"
3
- require "godmin/helpers/tables"
4
- require "godmin/resource/batch_actions"
5
- require "godmin/resource/filters"
6
- require "godmin/resource/ordering"
7
- require "godmin/resource/pagination"
8
- require "godmin/resource/scopes"
9
-
10
- module Godmin
11
- module Resource
12
- extend ActiveSupport::Concern
13
-
14
- included do
15
- helper Godmin::Helpers::BatchActions
16
- helper Godmin::Helpers::Filters
17
- helper Godmin::Helpers::Tables
18
-
19
- include Godmin::Resource::BatchActions
20
- include Godmin::Resource::Filters
21
- include Godmin::Resource::Scopes
22
- include Godmin::Resource::Ordering
23
- include Godmin::Resource::Pagination
24
-
25
- before_action :set_resource_class
26
- before_action :set_resources, only: :index
27
- before_action :set_resource, only: [:show, :new, :edit, :create, :update, :destroy]
28
-
29
- helper_method :attrs_for_index
30
- helper_method :attrs_for_form
31
- end
32
-
33
- def index
34
- respond_to do |format|
35
- format.html
36
- format.json { render json: @resources.to_json }
37
- end
38
- end
39
-
40
- def show
41
- respond_to do |format|
42
- format.html
43
- format.json { render json: @resource.to_json }
44
- end
45
- end
46
-
47
- def new; end
48
-
49
- def edit; end
50
-
51
- def create
52
- respond_to do |format|
53
- if @resource.save
54
- format.html { redirect_to redirect_after_create, notice: redirect_flash_message }
55
- format.json { render :show, status: :created, location: @resource }
56
- else
57
- format.html { render :edit }
58
- format.json { render json: @resource.errors, status: :unprocessable_entity }
59
- end
60
- end
61
- end
62
-
63
- def update
64
- respond_to do |format|
65
- if @resource.update(resource_params)
66
- format.html { redirect_to redirect_after_update, notice: redirect_flash_message }
67
- format.json { render :show, status: :ok, location: @resource }
68
- else
69
- format.html { render :edit }
70
- format.json { render json: @resource.errors, status: :unprocessable_entity }
71
- end
72
- end
73
- end
74
-
75
- def destroy
76
- @resource.destroy
77
-
78
- respond_to do |format|
79
- format.html { redirect_to redirect_after_destroy, notice: redirect_flash_message }
80
- format.json { head :no_content }
81
- end
82
- end
83
-
84
- # Gives the view access to the list of column names
85
- # to be printed in the index view
86
- def attrs_for_index
87
- []
88
- end
89
-
90
- # Gives the view access to the list of attributes
91
- # to be included in the default form
92
- def attrs_for_form
93
- []
94
- end
95
-
96
- protected
97
-
98
- def set_resource_class
99
- @resource_class ||= resource_class
100
- end
101
-
102
- def set_resources
103
- @resources ||= resources
104
- authorize(@resources) if authorization_enabled?
105
- end
106
-
107
- def set_resource
108
- @resource ||= resource
109
- authorize(@resource) if authorization_enabled?
110
- end
111
-
112
- def resource_class
113
- controller_name.classify.constantize
114
- end
115
-
116
- def resources_relation
117
- resource_class.all
118
- end
119
-
120
- def resources
121
- apply_pagination(
122
- apply_order(
123
- apply_filters(
124
- apply_scope(
125
- resources_relation
126
- )
127
- )
128
- )
129
- )
130
- end
131
-
132
- def resource
133
- if params[:id]
134
- find_resource(params[:id])
135
- else
136
- case action_name
137
- when "create"
138
- build_resource(resource_params)
139
- when "new"
140
- build_resource(nil)
141
- end
142
- end
143
- end
144
-
145
- def build_resource(params)
146
- resources_relation.new(params)
147
- end
148
-
149
- def find_resource(id)
150
- resources_relation.find(id)
151
- end
152
-
153
- def resource_params
154
- params.require(resource_class.name.underscore.parameterize("_").to_sym).permit(attrs_for_form)
155
- end
156
-
157
- def redirect_after_create
158
- redirect_after_save
159
- end
160
-
161
- def redirect_after_update
162
- redirect_after_save
163
- end
164
-
165
- def redirect_after_save
166
- @resource
167
- end
168
-
169
- def redirect_after_destroy
170
- resource_class.model_name.route_key.to_sym
171
- end
172
-
173
- def redirect_flash_message
174
- translate_scoped("flash.#{action_name}", resource: @resource.class.model_name.human)
175
- end
176
- end
177
- end
@@ -1,45 +0,0 @@
1
- module Godmin
2
- module Resource
3
- module BatchActions
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- helper_method :batch_action_map
8
- end
9
-
10
- def batch_action_map
11
- self.class.batch_action_map
12
- end
13
-
14
- def batch_action
15
- action = params[:batch_action][:action]
16
- item_ids = params[:batch_action][:items].keys.map(&:to_i)
17
-
18
- if batch_action_map.key?(action.to_sym)
19
- # Store the batched item ids so they can be highlighted later
20
- flash[:batch_actioned_ids] = item_ids
21
-
22
- # If the batch action returns false, it is because it has implemented
23
- # its own redirect. Therefore we return wihout redirecting.
24
- return unless send("batch_action_#{action}", resource_class.find(item_ids))
25
- end
26
-
27
- redirect_to :back
28
- end
29
-
30
- module ClassMethods
31
- def batch_action_map
32
- @batch_action_map ||= {}
33
- end
34
-
35
- def batch_action(attr, options = {})
36
- batch_action_map[attr] = {
37
- only: nil,
38
- except: nil,
39
- confirm: false
40
- }.merge(options)
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,41 +0,0 @@
1
- module Godmin
2
- module Resource
3
- module Filters
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- helper_method :filter_map
8
- end
9
-
10
- def filter_map
11
- self.class.filter_map
12
- end
13
-
14
- def apply_filters(resources)
15
- if params[:filter].present?
16
- params[:filter].each do |name, value|
17
- if filter_map.key?(name.to_sym) && value.present?
18
- resources = send("filter_#{name}", resources, value)
19
- end
20
- end
21
- end
22
- resources
23
- end
24
-
25
- module ClassMethods
26
- def filter_map
27
- @filter_map ||= {}
28
- end
29
-
30
- def filter(attr, options = {})
31
- filter_map[attr] = {
32
- as: :string,
33
- option_text: "to_s",
34
- option_value: "id",
35
- collection: nil
36
- }.merge(options)
37
- end
38
- end
39
- end
40
- end
41
- end
@@ -1,25 +0,0 @@
1
- module Godmin
2
- module Resource
3
- module Ordering
4
- extend ActiveSupport::Concern
5
-
6
- def apply_order(resources)
7
- if params[:order].present?
8
- resources.order("#{resource_class.table_name}.#{order_column} #{order_direction}")
9
- else
10
- resources
11
- end
12
- end
13
-
14
- protected
15
-
16
- def order_column
17
- params[:order].rpartition("_").first
18
- end
19
-
20
- def order_direction
21
- params[:order].rpartition("_").last
22
- end
23
- end
24
- end
25
- end
@@ -1,64 +0,0 @@
1
- module Godmin
2
- module Resource
3
- module Pagination
4
- extend ActiveSupport::Concern
5
-
6
- WINDOW_SIZE = 7
7
-
8
- included do
9
- helper_method :current_page
10
- helper_method :pages
11
- helper_method :total_pages
12
- helper_method :total_resources
13
- end
14
-
15
- def apply_pagination(resources)
16
- resources.limit(pagination_limit).offset(pagination_offset)
17
- end
18
-
19
- private
20
-
21
- def current_page
22
- params[:page].present? ? params[:page].to_i : 1
23
- end
24
-
25
- def pagination_limit
26
- self.class.per_page
27
- end
28
-
29
- def pagination_offset
30
- (current_page * self.class.per_page) - self.class.per_page
31
- end
32
-
33
- def pages
34
- @pages ||= begin
35
- pages = (1..total_pages).to_a
36
-
37
- return pages unless total_pages > WINDOW_SIZE
38
-
39
- if current_page < WINDOW_SIZE
40
- pages.slice(0, WINDOW_SIZE)
41
- elsif current_page > (total_pages - WINDOW_SIZE)
42
- pages.slice(-WINDOW_SIZE, WINDOW_SIZE)
43
- else
44
- pages.slice(pages.index(current_page) - (WINDOW_SIZE / 2), WINDOW_SIZE)
45
- end
46
- end
47
- end
48
-
49
- def total_pages
50
- @total_pages ||= (total_resources.to_f / self.class.per_page).ceil
51
- end
52
-
53
- def total_resources
54
- @total_resource ||= resources.limit(nil).offset(nil).count
55
- end
56
-
57
- module ClassMethods
58
- def per_page
59
- 25
60
- end
61
- end
62
- end
63
- end
64
- end
@@ -1,54 +0,0 @@
1
- module Godmin
2
- module Resource
3
- module Scopes
4
- extend ActiveSupport::Concern
5
-
6
- included do
7
- helper_method :scope_map
8
- helper_method :scope_count
9
- end
10
-
11
- def scope_map
12
- self.class.scope_map
13
- end
14
-
15
- def scope_count(scope)
16
- apply_filters(
17
- send("scope_#{scope}", resources_relation)
18
- ).count
19
- end
20
-
21
- def apply_scope(resources)
22
- params[:scope] = default_scope if params[:scope].blank?
23
-
24
- if params[:scope] && scope_map.key?(params[:scope].to_sym)
25
- send("scope_#{params[:scope]}", resources)
26
- else
27
- resources
28
- end
29
- end
30
-
31
- protected
32
-
33
- def default_scope
34
- scope = scope_map.find -> { scope_map.first } do |_key, value|
35
- value[:default] == true
36
- end
37
-
38
- scope ? scope[0].to_s : nil
39
- end
40
-
41
- module ClassMethods
42
- def scope_map
43
- @scope_map ||= {}
44
- end
45
-
46
- def scope(attr, options = {})
47
- scope_map[attr] = {
48
- default: false
49
- }.merge(options)
50
- end
51
- end
52
- end
53
- end
54
- end
File without changes