administrate-base_controller 0.3.0 → 0.4.0

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: c3d86a463b8de9a3ac555327f413d273ec996e1f5381c47deb98732166777a29
4
- data.tar.gz: 360287eb7fc408e9d7044ac2238997cb4cb647cd8b78486265661213b5bb484e
3
+ metadata.gz: 7fde5133218cd1044607f58848e8d3ef6348832d66578578de5dd0cedd04b112
4
+ data.tar.gz: 60c1aedb3a89c6113e583b5c1ad2803895eb32594e2983e99eb511e5179c8219
5
5
  SHA512:
6
- metadata.gz: d52269f57a761867001fc0f550cbee434b70a8ee3eeac9fae9ceea6fb29d0311770192465b352867b818cd99608a27ad3e4e3b566b3edba7bd408693ed2881d2
7
- data.tar.gz: c31ae9246857a214d329572ae3d72ec65e0f0f505a108096faaf07159786f9d2180c5b516db2044e519a82bb27ccbb97e19b160ffb76e717bdef7efa27f043ac
6
+ metadata.gz: afd6361c383b4b3a515a3025da5015b8e52644dc72aa267033063d812a6b9cc08a80188cf3d8a257323c0fb4b8bcf307511a805db9517ee22ef5f76fabc80a90
7
+ data.tar.gz: 2924d3caa5a94a0d71d51aa8561aa9281da010ffb078a1d8cce05dc52ba2bfb164946f65ddb8ece0fe82331b15c3e94ea48cb715892f429be667ec9e251e63d0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ - Extract all methods into a module `BaseController`
6
+ - Remove default `after_initialize` evaluation. You may require `administrate/base_controller/engine` instead
7
+ - Add some annotations to some of the methods
8
+
3
9
  ## 0.3.0
4
10
 
5
11
  - Add json rendering for index and show paths
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- administrate-base_controller (0.3.0)
4
+ administrate-base_controller (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -173,7 +173,7 @@ PLATFORMS
173
173
  x64-mingw32
174
174
 
175
175
  DEPENDENCIES
176
- administrate (>= 0.11.0, < 1)
176
+ administrate (~> 0.11.0)
177
177
  administrate-base_controller!
178
178
  bundler (~> 2.0)
179
179
  minitest (~> 5.0)
data/README.md CHANGED
@@ -24,7 +24,9 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- When you require this gem, the base controller functionality will be added to the `administrate/application_controller`.
27
+ When you require this gem, you can `include Adminstrate::BaseControll` in your admin `ApplicationController`.
28
+ Alternatively require `administrate/base_controller/engine` instead (e.g. in your Gemfile). The base controller
29
+ functionality will be added to the `administrate/application_controller`.
28
30
 
29
31
  You get the following _protected_ methods for free, which you may override:
30
32
 
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
35
  spec.require_paths = ['lib']
36
36
 
37
- spec.add_development_dependency 'administrate', '>= 0.11.0', '< 1'
37
+ spec.add_development_dependency 'administrate', '~> 0.11.0'
38
38
  spec.add_development_dependency 'bundler', '~> 2.0'
39
39
  spec.add_development_dependency 'minitest', '~> 5.0'
40
40
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -1,119 +1,139 @@
1
1
  require 'administrate/base_controller/version'
2
- require 'administrate/engine'
3
2
 
4
3
  module Administrate
5
- class Engine < Rails::Engine
6
- config.after_initialize do
7
- ::Administrate::ApplicationController.class_eval do
8
- def index
9
- resources = index_resources
10
- resources = apply_resource_includes(resources)
11
- resources = order.apply(resources)
12
- resources = resources.page(params[:page]).per(records_per_page)
13
-
14
- respond_to do |format|
15
- format.json { render_index_json(resources) }
16
- format.any { render_index_any(resources, format: format) }
17
- end
18
- end
19
-
20
- def new
21
- resource = new_resource
22
- authorize_resource(resource)
23
- render locals: { page: new_page(resource) }
24
- end
25
-
26
- def edit
27
- render locals: { page: edit_page(requested_resource) }
28
- end
29
-
30
- def show
31
- respond_to do |format|
32
- format.json { render_show_json(requested_resource) }
33
- format.any { render_show_any(requested_resource, format: format) }
34
- end
35
- end
36
-
37
- protected
38
-
39
- def search_term
40
- @search_term ||= params[:search].to_s.strip
41
- end
42
-
43
- def index_scoped_resource
44
- scoped_resource
45
- end
46
-
47
- def index_resources
48
- Administrate::Search.new(index_scoped_resource, dashboard_class, search_term).run
49
- end
50
-
51
- def index_page
52
- Administrate::Page::Collection.new(dashboard, order: order)
53
- end
54
-
55
- def show_page(resource = requested_resource)
56
- Administrate::Page::Show.new(dashboard, resource)
57
- end
58
-
59
- def new_page(resource)
60
- Administrate::Page::Form.new(dashboard, resource)
61
- end
62
-
63
- def edit_page(resource)
64
- new_page(resource)
65
- end
66
-
67
- def authorize_resource(resource)
68
- return if show_action?(action_name.to_sym, resource)
69
- raise "That's not a valid action."
70
- end
71
-
72
- def resource_params
73
- permitted = params.require(resource_class.model_name.param_key)
74
- .permit(dashboard.permitted_attributes)
75
- .to_h
76
-
77
- permitted.each_with_object(permitted) do |(k, v), result|
78
- result[k] = read_param(k, v)
79
- end
80
- end
81
-
82
- def read_param(_, data)
83
- if data.is_a?(ActionController::Parameters) && data[:type]
84
- return read_param_value(data)
85
- end
86
-
87
- data
88
- end
89
-
90
- def render_index_json(resources)
91
- render json: resources
92
- end
93
-
94
- def render_index_any(resources, format:)
95
- page = index_page
96
-
97
- render locals: {
98
- resources: resources,
99
- search_term: search_term,
100
- page: page,
101
- show_search_bar: show_search_bar?,
102
- }
103
- end
104
-
105
- def render_show_json(resource = requested_resource)
106
- render json: resource
107
- end
108
-
109
- def render_show_any(resource = requested_resource, format:)
110
- render locals: { page: show_page(resource) }
111
- end
112
-
113
- private
114
-
115
- attr_writer :search_term
4
+ module BaseController
5
+ def index
6
+ resources = index_resources
7
+ resources = apply_resource_includes(resources)
8
+ resources = order.apply(resources)
9
+ resources = resources.page(params[:page]).per(records_per_page)
10
+
11
+ respond_to do |format|
12
+ format.json { render_index_json(resources) }
13
+ format.any { render_index_any(resources, format: format) }
116
14
  end
117
15
  end
16
+
17
+ def new
18
+ resource = new_resource
19
+ authorize_resource(resource)
20
+ render locals: { page: new_page(resource) }
21
+ end
22
+
23
+ def edit
24
+ render locals: { page: edit_page(requested_resource) }
25
+ end
26
+
27
+ def show
28
+ respond_to do |format|
29
+ format.json { render_show_json(requested_resource) }
30
+ format.any { render_show_any(requested_resource, format: format) }
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def search_term
37
+ @search_term ||= params[:search].to_s.strip
38
+ end
39
+
40
+ ##
41
+ # The resource scope only used for +index+.
42
+ #
43
+ # By default this calls +scoped_resource+ but it's common to overwrite this in controllers that would like a scoped
44
+ # view on the index page only. This way a subset of resources is shown, based on some state such as the path or a
45
+ # cookie or the current user, but +find_resource+ still works as intended when accessing the show path.
46
+ #
47
+ def index_scoped_resource
48
+ scoped_resource
49
+ end
50
+
51
+ ##
52
+ # Wraps the +index_scoped_resource+.
53
+ #
54
+ # By default this constructs the +Administrate::Search+ decorator, wraps the resources and runs it with the
55
+ # +dashboard_class+ and +search_term+. Overwrite this to turn off search.
56
+ #
57
+ def index_resources
58
+ Administrate::Search.new(index_scoped_resource, dashboard_class, search_term).run
59
+ end
60
+
61
+ ##
62
+ # The page used for +index+.
63
+ #
64
+ def index_page
65
+ Administrate::Page::Collection.new(dashboard, order: order)
66
+ end
67
+
68
+ ##
69
+ # The page used for +show+
70
+ #
71
+ def show_page(resource = requested_resource)
72
+ Administrate::Page::Show.new(dashboard, resource)
73
+ end
74
+
75
+ ##
76
+ # The page used for +new+
77
+ #
78
+ def new_page(resource)
79
+ Administrate::Page::Form.new(dashboard, resource)
80
+ end
81
+
82
+ ##
83
+ # The page used for +edit+
84
+ #
85
+ def edit_page(resource)
86
+ new_page(resource)
87
+ end
88
+
89
+ def authorize_resource(resource)
90
+ return if show_action?(action_name.to_sym, resource)
91
+ raise "That's not a valid action."
92
+ end
93
+
94
+ def resource_params
95
+ permitted = params.require(resource_class.model_name.param_key)
96
+ .permit(dashboard.permitted_attributes)
97
+ .to_h
98
+
99
+ permitted.each_with_object(permitted) do |(k, v), result|
100
+ result[k] = read_param(k, v)
101
+ end
102
+ end
103
+
104
+ def read_param(_, data)
105
+ if data.is_a?(ActionController::Parameters) && data[:type]
106
+ return read_param_value(data)
107
+ end
108
+
109
+ data
110
+ end
111
+
112
+ def render_index_json(resources)
113
+ render json: resources
114
+ end
115
+
116
+ def render_index_any(resources, format:)
117
+ page = index_page
118
+
119
+ render locals: {
120
+ resources: resources,
121
+ search_term: search_term,
122
+ page: page,
123
+ show_search_bar: show_search_bar?,
124
+ }
125
+ end
126
+
127
+ def render_show_json(resource = requested_resource)
128
+ render json: resource
129
+ end
130
+
131
+ def render_show_any(resource = requested_resource, format:)
132
+ render locals: { page: show_page(resource) }
133
+ end
134
+
135
+ private
136
+
137
+ attr_writer :search_term
118
138
  end
119
139
  end
@@ -0,0 +1,14 @@
1
+ require 'administrate/base_controller/version'
2
+ require 'administrate/engine'
3
+
4
+ require_relative '../base_controller'
5
+
6
+ module Administrate
7
+ class Engine < Rails::Engine
8
+ config.after_initialize do
9
+ ::Administrate::ApplicationController.class_eval do
10
+ include BaseController
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module Administrate
2
2
  module BaseController
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate-base_controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2019-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.11.0
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '1'
23
20
  type: :development
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.11.0
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '1'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: bundler
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -112,6 +106,7 @@ files:
112
106
  - bin/console
113
107
  - bin/setup
114
108
  - lib/administrate/base_controller.rb
109
+ - lib/administrate/base_controller/engine.rb
115
110
  - lib/administrate/base_controller/version.rb
116
111
  homepage: https://github.com/XPBytes/administrate-base_controller
117
112
  licenses: