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
@@ -28,6 +28,12 @@ module Godmin
28
28
  end
29
29
  end
30
30
 
31
+ def test_engine_resolver_template_paths_when_prefix_contains_godmin
32
+ namespaced_as "namespace" do
33
+ assert_equal [], EngineResolver.new("controller_name").template_paths("godmin/namespace/prefix", false)
34
+ end
35
+ end
36
+
31
37
  def test_godmin_resolver_template_paths
32
38
  namespaced_as "namespace" do
33
39
  assert_equal [
@@ -0,0 +1,45 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ module ResourceService
5
+ class BatchActionsTest < ActiveSupport::TestCase
6
+ def setup
7
+ resource_class = Class.new do
8
+ def self.find(ids)
9
+ ids
10
+ end
11
+ end
12
+
13
+ @article_service = ArticleService.new(resource_class: resource_class)
14
+ end
15
+
16
+ def test_batch_action
17
+ assert @article_service.batch_action(:unpublish, [:foo, :bar])
18
+ assert_equal [:foo, :bar], @article_service.called_methods[:batch_actions][:unpublish]
19
+ end
20
+
21
+ def test_batch_action_when_it_does_not_exist
22
+ assert_not @article_service.batch_action(:foobar, [:foo, :bar])
23
+ assert_equal nil, @article_service.called_methods[:batch_actions][:foobar]
24
+ end
25
+
26
+ def test_batch_action_exists
27
+ assert @article_service.batch_action?(:unpublish)
28
+ end
29
+
30
+ def test_batch_action_does_not_exist
31
+ assert_not @article_service.batch_action?(:foobar)
32
+ end
33
+
34
+ def test_batch_action_map_with_default_options
35
+ expected_batch_action_map = { only: nil, except: nil, confirm: false }
36
+ assert_equal expected_batch_action_map, @article_service.batch_action_map[:unpublish]
37
+ end
38
+
39
+ def test_batch_action_map_with_custom_options
40
+ expected_batch_action_map = { only: :unpublished, except: :published, confirm: true }
41
+ assert_equal expected_batch_action_map, @article_service.batch_action_map[:publish]
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,32 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ module ResourceService
5
+ class FiltersTest < ActiveSupport::TestCase
6
+ def setup
7
+ @article_service = ArticleService.new
8
+ end
9
+
10
+ def test_calls_one_filter
11
+ @article_service.apply_filters({ title: "foobar" }, :resources)
12
+ assert_equal [:resources, "foobar"], @article_service.called_methods[:filters][:title]
13
+ end
14
+
15
+ def test_calls_multiple_filters
16
+ @article_service.apply_filters({ title: "foobar", country: "Sweden" }, :resources)
17
+ assert_equal [:resources, "foobar"], @article_service.called_methods[:filters][:title]
18
+ assert_equal [:resources, "Sweden"], @article_service.called_methods[:filters][:country]
19
+ end
20
+
21
+ def test_filter_map_with_default_options
22
+ expected_filter_map = { as: :string, option_text: "to_s", option_value: "id", collection: nil }
23
+ assert_equal expected_filter_map, @article_service.filter_map[:title]
24
+ end
25
+
26
+ def test_filter_map_with_custom_options
27
+ expected_filter_map = { as: :select, option_text: "to_s", option_value: "id", collection: %w(Sweden Canada) }
28
+ assert_equal expected_filter_map, @article_service.filter_map[:country]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ module ResourceService
5
+ class OrderingTest < ActiveSupport::TestCase
6
+ def setup
7
+ resource_class = Class.new do
8
+ def self.table_name
9
+ "articles"
10
+ end
11
+ end
12
+
13
+ @resources_class = Class.new do
14
+ attr_reader :order_param
15
+
16
+ def order(order_param)
17
+ @order_param = order_param
18
+ end
19
+ end
20
+
21
+ @article_service = ArticleService.new(resource_class: resource_class)
22
+ end
23
+
24
+ def test_apply_order
25
+ resources = @resources_class.new
26
+ @article_service.apply_order("title_desc", resources)
27
+ assert_equal "articles.title desc", resources.order_param
28
+ end
29
+
30
+ def test_apply_order_without_order
31
+ resources = @resources_class.new
32
+ @article_service.apply_order("", resources)
33
+ assert_equal nil, resources.order_param
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ module ResourceService
5
+ class PaginationTest < ActiveSupport::TestCase
6
+ def setup
7
+ @article_service = ArticleService.new
8
+
9
+ resources_class = Class.new do
10
+ def limit(_limit_param)
11
+ self
12
+ end
13
+
14
+ def offset(_offset_param)
15
+ self
16
+ end
17
+ end
18
+
19
+ @resources = resources_class.new
20
+ end
21
+
22
+ def test_paginator_is_set_correctly
23
+ @article_service.apply_pagination(1, @resources)
24
+
25
+ assert_kind_of Paginator, @article_service.paginator
26
+ assert_equal 1, @article_service.paginator.current_page
27
+ assert_equal 25, @article_service.paginator.per_page
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ module ResourceService
5
+ class ScopesTest < ActiveSupport::TestCase
6
+ class NoScopesService
7
+ include Godmin::Resources::ResourceService
8
+ end
9
+
10
+ def setup
11
+ @article_service = ArticleService.new
12
+ end
13
+
14
+ def test_returns_resources_when_no_scopes_are_defined
15
+ @foo_thing = NoScopesService.new
16
+ assert_equal :resources, @foo_thing.apply_scope("", :resources)
17
+ end
18
+
19
+ def test_calls_default_scope
20
+ @article_service.apply_scope("", :resources)
21
+ assert_equal :resources, @article_service.called_methods[:scopes][:unpublished]
22
+ end
23
+
24
+ def test_calls_non_default_scope
25
+ @article_service.apply_scope("published", :resources)
26
+ assert_equal :resources, @article_service.called_methods[:scopes][:published]
27
+ end
28
+
29
+ def test_calls_unimplemented_scope
30
+ assert_raises NotImplementedError do
31
+ @article_service.apply_scope("trashed", :resources)
32
+ end
33
+ end
34
+
35
+ def test_current_scope
36
+ @article_service.apply_scope("", :resources)
37
+ assert_equal "unpublished", @article_service.scope
38
+ end
39
+
40
+ def test_currently_scoped_by
41
+ @article_service.apply_scope("", :resources)
42
+ assert @article_service.scoped_by?("unpublished")
43
+ assert_not @article_service.scoped_by?("published")
44
+ end
45
+
46
+ def test_scope_count
47
+ assert_equal 2, @article_service.scope_count("unpublished")
48
+ assert_equal 1, @article_service.scope_count("published")
49
+ end
50
+
51
+ def test_scope_map
52
+ assert_equal({ default: true }, @article_service.scope_map[:unpublished])
53
+ assert_equal({ default: false }, @article_service.scope_map[:published])
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ module Godmin
4
+ class ResourceServiceTest < ActiveSupport::TestCase
5
+ def setup
6
+ @article_service = ArticleService.new
7
+ end
8
+
9
+ def test_resource_class
10
+ assert_equal Article, @article_service.resource_class
11
+ end
12
+
13
+ def test_attrs_for_index
14
+ assert_equal [:id, :title, :country], @article_service.attrs_for_index
15
+ end
16
+
17
+ def test_attrs_for_form
18
+ assert_equal [:id, :title, :country, :body], @article_service.attrs_for_form
19
+ end
20
+ end
21
+ end
data/test/test_helper.rb CHANGED
@@ -3,6 +3,11 @@ ENV["RAILS_ENV"] = "test"
3
3
 
4
4
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
5
  require "rails/test_help"
6
+ require "minitest/reporters"
7
+
8
+ Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(
9
+ color: true
10
+ )]
6
11
 
7
12
  Rails.backtrace_cleaner.remove_silencers!
8
13
 
@@ -19,3 +24,60 @@ def namespaced_as(namespace)
19
24
  yield
20
25
  Godmin.namespace = nil
21
26
  end
27
+
28
+ # module Godmin
29
+ class Article
30
+ end
31
+
32
+ class ArticleService
33
+ include Godmin::Resources::ResourceService
34
+
35
+ mattr_accessor :called_methods do
36
+ { scopes: {}, filters: {}, batch_actions: {} }
37
+ end
38
+
39
+ attrs_for_index :id, :title, :country
40
+ attrs_for_form :id, :title, :country, :body
41
+
42
+ scope :unpublished, default: true
43
+ scope :published
44
+
45
+ filter :title
46
+ filter :country, as: :select, collection: %w(Sweden Canada)
47
+
48
+ batch_action :unpublish
49
+ batch_action :publish, confirm: true, only: :unpublished, except: :published
50
+
51
+ def resources_relation
52
+ [:foo, :bar, :baz]
53
+ end
54
+
55
+ def scope_unpublished(resources)
56
+ called_methods[:scopes][:unpublished] = resources
57
+ resources.slice(1, 3)
58
+ end
59
+
60
+ def scope_published(resources)
61
+ called_methods[:scopes][:published] = resources
62
+ resources.slice(0, 1)
63
+ end
64
+
65
+ def filter_title(resources, value)
66
+ called_methods[:filters][:title] = [resources, value]
67
+ resources
68
+ end
69
+
70
+ def filter_country(resources, value)
71
+ called_methods[:filters][:country] = [resources, value]
72
+ resources
73
+ end
74
+
75
+ def batch_action_unpublish(resources)
76
+ called_methods[:batch_actions][:unpublish] = resources
77
+ end
78
+
79
+ def batch_action_publish(resources)
80
+ called_methods[:batch_actions][:publish] = resources
81
+ end
82
+ end
83
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: godmin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.3
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Ljungblad
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-02-18 00:00:00.000000000 Z
13
+ date: 2015-04-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bcrypt
@@ -122,14 +122,14 @@ dependencies:
122
122
  requirements:
123
123
  - - "~>"
124
124
  - !ruby/object:Gem::Version
125
- version: 0.11.2
125
+ version: 0.12.0
126
126
  type: :runtime
127
127
  prerelease: false
128
128
  version_requirements: !ruby/object:Gem::Requirement
129
129
  requirements:
130
130
  - - "~>"
131
131
  - !ruby/object:Gem::Version
132
- version: 0.11.2
132
+ version: 0.12.0
133
133
  - !ruby/object:Gem::Dependency
134
134
  name: sqlite3
135
135
  requirement: !ruby/object:Gem::Requirement
@@ -144,6 +144,48 @@ dependencies:
144
144
  - - ">="
145
145
  - !ruby/object:Gem::Version
146
146
  version: '0'
147
+ - !ruby/object:Gem::Dependency
148
+ name: minitest
149
+ requirement: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ type: :development
155
+ prerelease: false
156
+ version_requirements: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ - !ruby/object:Gem::Dependency
162
+ name: minitest-reporters
163
+ requirement: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ type: :development
169
+ prerelease: false
170
+ version_requirements: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ - !ruby/object:Gem::Dependency
176
+ name: pry
177
+ requirement: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '0'
147
189
  description: Godmin is an admin framework for Rails 4+
148
190
  email:
149
191
  - info@varvet.se
@@ -152,7 +194,6 @@ extensions: []
152
194
  extra_rdoc_files: []
153
195
  files:
154
196
  - ".gitignore"
155
- - ".hound.yml"
156
197
  - ".rubocop.yml"
157
198
  - ".travis.yml"
158
199
  - CHANGELOG.md
@@ -200,10 +241,11 @@ files:
200
241
  - lib/generators/godmin/policy/templates/policy.rb
201
242
  - lib/generators/godmin/resource/resource_generator.rb
202
243
  - lib/generators/godmin/resource/templates/resource_controller.rb
244
+ - lib/generators/godmin/resource/templates/resource_service.rb
203
245
  - lib/godmin.rb
204
- - lib/godmin/application.rb
246
+ - lib/godmin/application_controller.rb
205
247
  - lib/godmin/authentication.rb
206
- - lib/godmin/authentication/sessions.rb
248
+ - lib/godmin/authentication/sessions_controller.rb
207
249
  - lib/godmin/authentication/user.rb
208
250
  - lib/godmin/authorization.rb
209
251
  - lib/godmin/authorization/policy.rb
@@ -217,14 +259,16 @@ files:
217
259
  - lib/godmin/helpers/forms.rb
218
260
  - lib/godmin/helpers/tables.rb
219
261
  - lib/godmin/helpers/translations.rb
262
+ - lib/godmin/paginator.rb
220
263
  - lib/godmin/rails.rb
221
264
  - lib/godmin/resolver.rb
222
- - lib/godmin/resource.rb
223
- - lib/godmin/resource/batch_actions.rb
224
- - lib/godmin/resource/filters.rb
225
- - lib/godmin/resource/ordering.rb
226
- - lib/godmin/resource/pagination.rb
227
- - lib/godmin/resource/scopes.rb
265
+ - lib/godmin/resources/resource_controller.rb
266
+ - lib/godmin/resources/resource_service.rb
267
+ - lib/godmin/resources/resource_service/batch_actions.rb
268
+ - lib/godmin/resources/resource_service/filters.rb
269
+ - lib/godmin/resources/resource_service/ordering.rb
270
+ - lib/godmin/resources/resource_service/pagination.rb
271
+ - lib/godmin/resources/resource_service/scopes.rb
228
272
  - lib/godmin/version.rb
229
273
  - lib/tasks/godmin_tasks.rake
230
274
  - screenshot.png
@@ -260,18 +304,25 @@ files:
260
304
  - test/dummy/config/initializers/wrap_parameters.rb
261
305
  - test/dummy/config/locales/en.yml
262
306
  - test/dummy/config/routes.rb
263
- - test/dummy/db/test.sqlite3
307
+ - test/dummy/db/schema.rb
264
308
  - test/dummy/lib/assets/.keep
265
309
  - test/dummy/log/.keep
266
- - test/dummy/log/development.log
267
310
  - test/dummy/public/404.html
268
311
  - test/dummy/public/422.html
269
312
  - test/dummy/public/500.html
270
313
  - test/dummy/public/favicon.ico
271
314
  - test/godmin_test.rb
272
315
  - test/integration/navigation_test.rb
316
+ - test/lib/godmin/helpers/filters_test.rb
317
+ - test/lib/godmin/paginator_test.rb
273
318
  - test/lib/godmin/policy_finder_test.rb
274
319
  - test/lib/godmin/resolver_test.rb
320
+ - test/lib/godmin/resources/resource_service/batch_actions_test.rb
321
+ - test/lib/godmin/resources/resource_service/filters_test.rb
322
+ - test/lib/godmin/resources/resource_service/ordering_test.rb
323
+ - test/lib/godmin/resources/resource_service/pagination_test.rb
324
+ - test/lib/godmin/resources/resource_service/scopes_test.rb
325
+ - test/lib/godmin/resources/resource_service_test.rb
275
326
  - test/test_helper.rb
276
327
  - vendor/assets/javascripts/bootstrap-datetimepicker.js
277
328
  - vendor/assets/stylesheets/bootstrap-datetimepicker.css
@@ -332,17 +383,24 @@ test_files:
332
383
  - test/dummy/config/initializers/wrap_parameters.rb
333
384
  - test/dummy/config/locales/en.yml
334
385
  - test/dummy/config/routes.rb
335
- - test/dummy/db/test.sqlite3
386
+ - test/dummy/db/schema.rb
336
387
  - test/dummy/lib/assets/.keep
337
388
  - test/dummy/log/.keep
338
- - test/dummy/log/development.log
339
389
  - test/dummy/public/404.html
340
390
  - test/dummy/public/422.html
341
391
  - test/dummy/public/500.html
342
392
  - test/dummy/public/favicon.ico
343
393
  - test/godmin_test.rb
344
394
  - test/integration/navigation_test.rb
395
+ - test/lib/godmin/helpers/filters_test.rb
396
+ - test/lib/godmin/paginator_test.rb
345
397
  - test/lib/godmin/policy_finder_test.rb
346
398
  - test/lib/godmin/resolver_test.rb
399
+ - test/lib/godmin/resources/resource_service/batch_actions_test.rb
400
+ - test/lib/godmin/resources/resource_service/filters_test.rb
401
+ - test/lib/godmin/resources/resource_service/ordering_test.rb
402
+ - test/lib/godmin/resources/resource_service/pagination_test.rb
403
+ - test/lib/godmin/resources/resource_service/scopes_test.rb
404
+ - test/lib/godmin/resources/resource_service_test.rb
347
405
  - test/test_helper.rb
348
406
  has_rdoc: