administrate-base_controller 0.3.0 → 0.7.0

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.
data/LICENSE.txt CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2019 Derk-Jan Karrenbeld
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Derk-Jan Karrenbeld
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
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 Administrate::BaseController` 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
 
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
8
- end
9
-
10
- task :default => :test
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -1,42 +1,42 @@
1
- lib = File.expand_path('../lib', __FILE__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require 'administrate/base_controller/version'
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = 'administrate-base_controller'
7
- spec.version = Administrate::BaseController::VERSION
8
- spec.authors = ['Derk-Jan Karrenbeld']
9
- spec.email = ['derk-jan@xpbytes.com']
10
-
11
- spec.summary = 'Extends the ApplicationController in Administrate'
12
- spec.homepage = 'https://github.com/XPBytes/administrate-base_controller'
13
- spec.license = 'MIT'
14
-
15
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
- # to allow pushing to a single host or delete this section to allow pushing to any host.
17
- if spec.respond_to?(:metadata)
18
- # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
19
-
20
- spec.metadata['homepage_uri'] = spec.homepage
21
- spec.metadata['source_code_uri'] = spec.homepage
22
- spec.metadata['changelog_uri'] = spec.homepage + '/CHANGELOG.md'
23
- else
24
- raise 'RubyGems 2.0 or newer is required to protect against ' \
25
- 'public gem pushes.'
26
- end
27
-
28
- # Specify which files should be added to the gem when it is released.
29
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
- end
33
- spec.bindir = 'exe'
34
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
- spec.require_paths = ['lib']
36
-
37
- spec.add_development_dependency 'administrate', '>= 0.11.0', '< 1'
38
- spec.add_development_dependency 'bundler', '~> 2.0'
39
- spec.add_development_dependency 'minitest', '~> 5.0'
40
- spec.add_development_dependency 'rake', '~> 10.0'
41
- spec.add_development_dependency 'rails'
42
- end
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'administrate/base_controller/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'administrate-base_controller'
7
+ spec.version = Administrate::BaseController::VERSION
8
+ spec.authors = ['Derk-Jan Karrenbeld']
9
+ spec.email = ['derk-jan@xpbytes.com']
10
+
11
+ spec.summary = 'Extends the ApplicationController in Administrate'
12
+ spec.homepage = 'https://github.com/XPBytes/administrate-base_controller'
13
+ spec.license = 'MIT'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
19
+
20
+ spec.metadata['homepage_uri'] = spec.homepage
21
+ spec.metadata['source_code_uri'] = spec.homepage
22
+ spec.metadata['changelog_uri'] = spec.homepage + '/CHANGELOG.md'
23
+ else
24
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
25
+ 'public gem pushes.'
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = 'exe'
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_development_dependency 'administrate', '~> 0.13.0'
38
+ spec.add_development_dependency 'bundler', '~> 2.0'
39
+ spec.add_development_dependency 'minitest', '~> 5.0'
40
+ spec.add_development_dependency 'rake', '~> 13.0'
41
+ spec.add_development_dependency 'rails', '~> 6.0'
42
+ end
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "administrate/default_order"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "administrate/default_order"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,119 +1,141 @@
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
4
+ module BaseController
5
+ def index
6
+ resources = index_resources
7
+ resources = apply_collection_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) }
14
+ end
15
+ end
29
16
 
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
17
+ def new
18
+ resource = new_resource
19
+ authorize_resource(resource)
20
+ render locals: { page: new_page(resource) }
21
+ end
36
22
 
37
- protected
23
+ def edit
24
+ render locals: { page: edit_page(requested_resource) }
25
+ end
38
26
 
39
- def search_term
40
- @search_term ||= params[:search].to_s.strip
41
- end
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
42
33
 
43
- def index_scoped_resource
44
- scoped_resource
45
- end
34
+ protected
46
35
 
47
- def index_resources
48
- Administrate::Search.new(index_scoped_resource, dashboard_class, search_term).run
49
- end
36
+ def search_term
37
+ @search_term ||= params[:search].to_s.strip
38
+ end
50
39
 
51
- def index_page
52
- Administrate::Page::Collection.new(dashboard, order: order)
53
- end
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
54
50
 
55
- def show_page(resource = requested_resource)
56
- Administrate::Page::Show.new(dashboard, resource)
57
- end
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
58
60
 
59
- def new_page(resource)
60
- Administrate::Page::Form.new(dashboard, resource)
61
- end
61
+ ##
62
+ # The page used for +index+.
63
+ #
64
+ def index_page
65
+ Administrate::Page::Collection.new(dashboard, order: order)
66
+ end
62
67
 
63
- def edit_page(resource)
64
- new_page(resource)
65
- end
68
+ ##
69
+ # The page used for +show+
70
+ #
71
+ def show_page(resource = requested_resource)
72
+ Administrate::Page::Show.new(dashboard, resource)
73
+ end
66
74
 
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
75
+ ##
76
+ # The page used for +new+
77
+ #
78
+ def new_page(resource)
79
+ Administrate::Page::Form.new(dashboard, resource)
80
+ end
71
81
 
72
- def resource_params
73
- permitted = params.require(resource_class.model_name.param_key)
74
- .permit(dashboard.permitted_attributes)
75
- .to_h
82
+ ##
83
+ # The page used for +edit+
84
+ #
85
+ def edit_page(resource)
86
+ new_page(resource)
87
+ end
76
88
 
77
- permitted.each_with_object(permitted) do |(k, v), result|
78
- result[k] = read_param(k, v)
79
- end
80
- end
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
81
93
 
82
- def read_param(_, data)
83
- if data.is_a?(ActionController::Parameters) && data[:type]
84
- return read_param_value(data)
85
- end
94
+ def resource_params
95
+ permitted = params.require(resource_class.model_name.param_key)
96
+ .permit(dashboard.permitted_attributes)
97
+ .to_h
86
98
 
87
- data
88
- end
99
+ permitted.each_with_object(permitted) do |(k, v), result|
100
+ result[k] = read_param(k, v)
101
+ end
102
+ end
89
103
 
90
- def render_index_json(resources)
91
- render json: resources
104
+ def read_param(_, data)
105
+ if data.is_a?(Hash) && data[:type]
106
+ if data[:type] == Administrate::Field::Polymorphic.to_s
107
+ return GlobalID::Locator.locate(data[:value])
92
108
  end
109
+ end
93
110
 
94
- def render_index_any(resources, format:)
95
- page = index_page
111
+ data
112
+ end
96
113
 
97
- render locals: {
98
- resources: resources,
99
- search_term: search_term,
100
- page: page,
101
- show_search_bar: show_search_bar?,
102
- }
103
- end
114
+ def render_index_json(resources)
115
+ render json: resources
116
+ end
104
117
 
105
- def render_show_json(resource = requested_resource)
106
- render json: resource
107
- end
118
+ def render_index_any(resources, format:)
119
+ page = index_page
108
120
 
109
- def render_show_any(resource = requested_resource, format:)
110
- render locals: { page: show_page(resource) }
111
- end
121
+ render locals: {
122
+ resources: resources,
123
+ search_term: search_term,
124
+ page: page,
125
+ show_search_bar: show_search_bar?,
126
+ }
127
+ end
112
128
 
113
- private
129
+ def render_show_json(resource = requested_resource)
130
+ render json: resource
131
+ end
114
132
 
115
- attr_writer :search_term
116
- end
133
+ def render_show_any(resource = requested_resource, format:)
134
+ render locals: { page: show_page(resource) }
117
135
  end
136
+
137
+ private
138
+
139
+ attr_writer :search_term
118
140
  end
119
141
  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