administrate-base_controller 0.2.0 → 0.3.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: 21f4165f46349b93a8dc74edf92e906db55833b900d32ec1a7e19e33e14dcc50
4
- data.tar.gz: 36f6d49ffb71ae71268324341600f106db12f1cffc51a845105ca892742cf109
3
+ metadata.gz: c3d86a463b8de9a3ac555327f413d273ec996e1f5381c47deb98732166777a29
4
+ data.tar.gz: 360287eb7fc408e9d7044ac2238997cb4cb647cd8b78486265661213b5bb484e
5
5
  SHA512:
6
- metadata.gz: f1dd8d6a755aeef649810168e200a1f44eddb407441044f58e8e57b9c55079df50ca7b5de45292dbf2c7303b2a5b521a8098cb1c3901aa6e1de2d24b0a70ff77
7
- data.tar.gz: 2f9c350f7122e5a54a2f5e065ae9ddaa7d2c8f4dd45194c88a134547185d070c71c2c8e49e498f2697a0a9332b0cf5605e62f868cc800ef245db6639fd8c771d
6
+ metadata.gz: d52269f57a761867001fc0f550cbee434b70a8ee3eeac9fae9ceea6fb29d0311770192465b352867b818cd99608a27ad3e4e3b566b3edba7bd408693ed2881d2
7
+ data.tar.gz: c31ae9246857a214d329572ae3d72ec65e0f0f505a108096faaf07159786f9d2180c5b516db2044e519a82bb27ccbb97e19b160ffb76e717bdef7efa27f043ac
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - Add json rendering for index and show paths
6
+ - Add `render_index_json(resources)` to easily override json responses on the index route
7
+ - Add `render_index_any(resources, format:)` to easily override non-json responses on the index route
8
+ - Add `render_show_json(resource)` to easily override json responses on the show route
9
+ - Add `render_show_any(resource, format:)` to easily override non-json responses on the show route
10
+
3
11
  ## 0.2.0
4
12
 
5
13
  Load on after_initialize hook
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- administrate-base_controller (0.2.0)
4
+ administrate-base_controller (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -41,6 +41,10 @@ You get the following _protected_ methods for free, which you may override:
41
41
  | `authorize_resource` | `show_action?(action_name.to_sym, resource)` | Allows you to change how resources are authorized |
42
42
  | `resource_params` | Calls `read_param(k, v)` for each instead of `transform_values` | Allows you to change how params are read |
43
43
  | `read_param` | Calls `read_param_value` if applicable | Allows you to change how a param is read based on its key |
44
+ | `render_index_json` | `json: resources.to_json` | Easily override json responses on the index route without changing the resources logic |
45
+ | `render_index_any` | `locals: resources, page, ...` | Easily override any responses on the index route without changing the resources logic |
46
+ | `render_show_json` | `json: resource.to_json` | Easily override json responses on the show route without changing the resources logic |
47
+ | `render_show_any` | `locals: resource, page, ...` | Easily override any responses on the show route without changing the resources logic |
44
48
 
45
49
  ```ruby
46
50
  module Admin
@@ -60,7 +64,7 @@ module Admin
60
64
  def current_ability
61
65
  @_current_ability ||= Ability.new(current_admin_user)
62
66
  end
63
-
67
+
64
68
  def show_action?(action, resource)
65
69
  current_ability.can?(action.to_sym, resource)
66
70
  end
@@ -73,13 +77,13 @@ Additionally you can correctly hide resources the `current_ability` can not `act
73
77
  ```ruby
74
78
  module Admin
75
79
  class ApplicationController < Administrate::ApplicationController
76
-
80
+
77
81
  # ...
78
-
82
+
79
83
  def scoped_resource
80
84
  super.accessible_by(current_ability, action_name.to_sym)
81
85
  end
82
- end
86
+ end
83
87
  end
84
88
  ```
85
89
 
@@ -91,8 +95,8 @@ This works without this gem, but this gem allows you to changed `scoped_resource
91
95
  module Admin
92
96
  class BookController < ::Admin::ApplicationController
93
97
  def find_resource(param)
94
- scoped_resource.friendly.find(param)
95
- end
98
+ scoped_resource.friendly.find(param)
99
+ end
96
100
  end
97
101
  end
98
102
  ```
@@ -107,7 +111,7 @@ module Admin
107
111
  class BookController < ::Admin::ApplicationController
108
112
  def index_scoped_resource
109
113
  super.where(author: Current.author)
110
- end
114
+ end
111
115
  end
112
116
  end
113
117
  ```
@@ -124,7 +128,7 @@ module Admin
124
128
  class BookController < ::Admin::ApplicationController
125
129
  def new_resource
126
130
  resource_class.new(author: Current.author)
127
- end
131
+ end
128
132
  end
129
133
  end
130
134
  ```
@@ -138,12 +142,12 @@ field, which contents have been serialized, you can overwrite `read_param`:
138
142
  module Admin
139
143
  class BookController < ::Admin::ApplicationController
140
144
  JSON_FIELDS = %w[options content].freeze
141
-
145
+
142
146
  def read_param(key, value)
143
147
  return Oj.load(value) if JSON_FIELDS.include?(String(key))
144
148
  super(key, value)
145
149
  end
146
- end
150
+ end
147
151
  end
148
152
  ```
149
153
 
@@ -152,21 +156,21 @@ Alternatively you can use the [`administrate-serialized_fields`](https://github.
152
156
  ## Related
153
157
 
154
158
  - [`Administrate`](https://github.com/thoughtbot/administrate): A Rails engine that helps you put together a super-flexible admin dashboard.
155
- <!-- - [`Administrate::BaseController`](https://github.com/XPBytes/administrate-base_controller): A set of application controller improvements. -->
156
159
 
157
160
  ### Concerns
158
161
 
159
- - [`Administrate::DefaultOrder`](https://github.com/XPBytes/administrate-default_order): Sets the default order for a resource in a administrate controller.
160
- - [`Administrate::SerializedFields`](https://github.com/XPBytes/administrate-serialized_fields): Automatically deserialize administrate fields on form submit.
162
+ - [`Administrate::DefaultOrder`](https://github.com/XPBytes/administrate-default_order): :1234: Sets the default order for a resource in a administrate controller.
163
+ - [`Administrate::SerializedFields`](https://github.com/XPBytes/administrate-serialized_fields): :ab: Automatically deserialize administrate fields on form submit.
161
164
 
162
165
  ### Fields
163
166
 
164
- - [`Administrate::Field::Code`](https://github.com/XPBytes/administrate-field-code): A `text` field that shows code.
165
- - [`Administrate::Field::Hyperlink`](https://github.com/XPBytes/administrate-field-hyperlink): A `string` field that is shows a hyperlink.
166
- - [`Adminisrtate::Field::JsonEditor`](https://github.com/XPBytes/administrate-field-json_editor): A `text` field that shows a [JSON editor](https://github.com/josdejong/jsoneditor).
167
- - [`Administrate::Field::ScopedBelongsTo`](https://github.com/XPBytes/administrate-field-scoped_belongs_to): A `belongs_to` field that yields itself to the scope `lambda`.
168
- - [`Administrate::Field::ScopedHasMany`](https://github.com/XPBytes/administrate-field-scoped_has_many): A `has_many` field that yields itself to the scope `lambda`.
169
- - [`Administrate::Field::TimeAgo`](https://github.com/XPBytes/administrate-field-time_ago): A `date_time` field that shows its data as `time_ago` since.
167
+ - [`Administrate::Field::Code`](https://github.com/XPBytes/administrate-field-code): :pencil: A `text` field that shows code.
168
+ - [`Administrate::Field::Hyperlink`](https://github.com/XPBytes/administrate-field-hyperlink): :pencil: A `string` field that is shows a hyperlink.
169
+ - [`Adminisrtate::Field::JsonEditor`](https://github.com/XPBytes/administrate-field-json_editor): :pencil: A `text` field that shows a [JSON editor](https://github.com/josdejong/jsoneditor).
170
+ - [`Administrate::Field::LazyBelongsTo`](https://github.com/XPBytes/administrate-field-lazy_belongs_to): :pencil: An input field that lazy loads `belongs_to` values.
171
+ - [`Administrate::Field::ScopedBelongsTo`](https://github.com/XPBytes/administrate-field-scoped_belongs_to): :pencil: A `belongs_to` field that yields itself to the scope `lambda`.
172
+ - [`Administrate::Field::ScopedHasMany`](https://github.com/XPBytes/administrate-field-scoped_has_many): :pencil: A `has_many` field that yields itself to the scope `lambda`.
173
+ - [`Administrate::Field::TimeAgo`](https://github.com/XPBytes/administrate-field-time_ago): :pencil: A `date_time` field that shows its data as `time_ago` since.
170
174
 
171
175
  ## Development
172
176
 
@@ -10,14 +10,11 @@ module Administrate
10
10
  resources = apply_resource_includes(resources)
11
11
  resources = order.apply(resources)
12
12
  resources = resources.page(params[:page]).per(records_per_page)
13
- page = index_page
14
13
 
15
- render locals: {
16
- resources: resources,
17
- search_term: search_term,
18
- page: page,
19
- show_search_bar: show_search_bar?,
20
- }
14
+ respond_to do |format|
15
+ format.json { render_index_json(resources) }
16
+ format.any { render_index_any(resources, format: format) }
17
+ end
21
18
  end
22
19
 
23
20
  def new
@@ -31,7 +28,10 @@ module Administrate
31
28
  end
32
29
 
33
30
  def show
34
- render locals: { page: show_page }
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
35
  end
36
36
 
37
37
  protected
@@ -52,8 +52,8 @@ module Administrate
52
52
  Administrate::Page::Collection.new(dashboard, order: order)
53
53
  end
54
54
 
55
- def show_page
56
- Administrate::Page::Show.new(dashboard, requested_resource)
55
+ def show_page(resource = requested_resource)
56
+ Administrate::Page::Show.new(dashboard, resource)
57
57
  end
58
58
 
59
59
  def new_page(resource)
@@ -87,6 +87,29 @@ module Administrate
87
87
  data
88
88
  end
89
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
+
90
113
  private
91
114
 
92
115
  attr_writer :search_term
@@ -1,5 +1,5 @@
1
1
  module Administrate
2
2
  module BaseController
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: administrate-base_controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.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-15 00:00:00.000000000 Z
11
+ date: 2019-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: administrate