effective_resources 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/app/controllers/concerns/effective/crud_controller.rb +43 -5
- data/app/models/effective/resources/attributes.rb +8 -5
- data/app/models/effective/resources/init.rb +5 -1
- data/app/models/effective/resources/naming.rb +1 -1
- data/app/models/effective/resources/relation.rb +15 -4
- data/app/models/effective/resources/sql.rb +1 -0
- data/lib/effective_resources/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c0486c174dd667b0157db3c09422b990ae5145d
|
4
|
+
data.tar.gz: 6c066a0c0ad0b9bfeabece6aad26b005db39a269
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb3b59b6125d11e74dc7699b6b11f78c7e5731bee9c0b8fc86380317acca67284773d075caa0a6978f184b3adec73063293883665693667516edf9a303e4e327
|
7
|
+
data.tar.gz: af797ee70b12863d379b7a7e7a404353791bf1b76d08aa4e3dff22a80ba9839b8d27cc6ef75ad5fa8dfb372fb4aa9e9321f288baaac47258466f78dcf278e741
|
data/README.md
CHANGED
@@ -36,8 +36,15 @@ class PostsController < ApplicationController
|
|
36
36
|
|
37
37
|
protected
|
38
38
|
|
39
|
+
# If it's a Hash of attributes, the controller will call .where(attributes)
|
40
|
+
# and the attributes will be used to initialize the datatable on index
|
41
|
+
#
|
42
|
+
# If it's an ActiveRecord scope, or symbol, we initialize a datatable with {resource_scope: true}
|
43
|
+
# and leave it as a TODO for the datatable to do the right thin .
|
39
44
|
def post_scope
|
40
|
-
{client_id: current_user.client_id}
|
45
|
+
{client_id: current_user.client_id}
|
46
|
+
# Post.where(client_id: current_user.client_id)
|
47
|
+
# :approved
|
41
48
|
end
|
42
49
|
|
43
50
|
def post_params
|
@@ -12,10 +12,42 @@ module Effective
|
|
12
12
|
define_method(action) do
|
13
13
|
self.resource ||= resource_class.find(params[:id])
|
14
14
|
|
15
|
-
@page_title ||= resource.to_s
|
16
15
|
EffectiveResources.authorized?(self, action, resource)
|
16
|
+
|
17
|
+
if (request.post? || request.patch? || request.put?)
|
18
|
+
raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
|
19
|
+
|
20
|
+
begin
|
21
|
+
raise 'exception' unless resource.send("#{action}!")
|
22
|
+
|
23
|
+
flash[:success] = "Successfully #{action}#{action.to_s.end_with?('e') ? 'd' : 'ed'} #{resource_human_name}"
|
24
|
+
redirect_back fallback_location: resource_redirect_path
|
25
|
+
rescue => e
|
26
|
+
flash.now[:danger] = "Unable to #{action} #{resource_human_name}: #{resource.errors.full_messages.to_sentence.presence || e.message}"
|
27
|
+
|
28
|
+
referer = request.referer.to_s
|
29
|
+
|
30
|
+
if referer.end_with?(send(effective_resource.edit_path, resource))
|
31
|
+
@page_title ||= "Edit #{resource}"
|
32
|
+
render :edit
|
33
|
+
elsif referer.end_with?(send(effective_resource.new_path))
|
34
|
+
@page_title ||= "New #{resource_name.titleize}"
|
35
|
+
render :new
|
36
|
+
else
|
37
|
+
@page_title ||= resource.to_s
|
38
|
+
flash[:danger] = flash.now[:danger]
|
39
|
+
|
40
|
+
if referer.present? && (Rails.application.routes.recognize_path(URI(referer).path) rescue false)
|
41
|
+
redirect_back fallback_location: resource_redirect_path
|
42
|
+
else
|
43
|
+
redirect_to(resource_redirect_path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
17
48
|
end
|
18
49
|
end
|
50
|
+
|
19
51
|
end
|
20
52
|
|
21
53
|
def index
|
@@ -102,7 +134,7 @@ module Effective
|
|
102
134
|
when 'Save' ; send(effective_resource.edit_path, resource)
|
103
135
|
when 'Save and Continue' ; send(effective_resource.index_path)
|
104
136
|
when 'Save and Add New' ; send(effective_resource.new_path)
|
105
|
-
else send(effective_resource.show_path, resource)
|
137
|
+
else send((effective_resource.show_path(check: true) || effective_resource.edit_path), resource)
|
106
138
|
end
|
107
139
|
end
|
108
140
|
|
@@ -147,9 +179,14 @@ module Effective
|
|
147
179
|
effective_resource.klass
|
148
180
|
else
|
149
181
|
case (resource_scope = send(resource_scope_method_name))
|
150
|
-
when
|
151
|
-
|
152
|
-
when
|
182
|
+
when ActiveRecord::Relation
|
183
|
+
effective_resource.relation.merge(resource_scope)
|
184
|
+
when Hash
|
185
|
+
effective_resource.klass.where(resource_scope)
|
186
|
+
when Symbol
|
187
|
+
effective_resource.klass.send(resource_scope)
|
188
|
+
when nil
|
189
|
+
effective_resource.klass
|
153
190
|
else
|
154
191
|
raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
|
155
192
|
end
|
@@ -161,6 +198,7 @@ module Effective
|
|
161
198
|
return {} unless resource_scope_method_name.present?
|
162
199
|
|
163
200
|
case (resource_scope = send(resource_scope_method_name))
|
201
|
+
when ActiveRecord::Relation ; {resource_scope: true}
|
164
202
|
when Hash ; resource_scope
|
165
203
|
when Symbol ; {resource_scope: true}
|
166
204
|
when nil ; {}
|
@@ -11,16 +11,19 @@ module Effective
|
|
11
11
|
end
|
12
12
|
|
13
13
|
# All attributes from the klass, sorted as per attributes block.
|
14
|
-
# Does not include :id, :created_at, :updated_at
|
15
|
-
def klass_attributes
|
14
|
+
# Does not include :id, :created_at, :updated_at unless all is passed
|
15
|
+
def klass_attributes(all: false)
|
16
16
|
attributes = (klass.new().attributes rescue nil)
|
17
17
|
return [] unless attributes
|
18
18
|
|
19
|
-
|
19
|
+
names = attributes.keys - belong_tos.map { |reference| reference.foreign_key }
|
20
|
+
names = names - [klass.primary_key, 'created_at', 'updated_at'] unless all
|
21
|
+
|
22
|
+
attributes = names.map do |name|
|
20
23
|
if klass.respond_to?(:column_for_attribute) # Rails 4+
|
21
|
-
Effective::Attribute.new(
|
24
|
+
Effective::Attribute.new(name, klass.column_for_attribute(name).type)
|
22
25
|
else
|
23
|
-
Effective::Attribute.new(
|
26
|
+
Effective::Attribute.new(name, klass.columns_hash[name].type)
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
@@ -12,8 +12,10 @@ module Effective
|
|
12
12
|
_klass_by_name(input)
|
13
13
|
when ActiveRecord::Relation
|
14
14
|
input.klass
|
15
|
+
when (ActiveRecord::Reflection::AbstractReflection rescue :nil)
|
16
|
+
((input.klass rescue nil).presence || _klass_by_name(input.class_name)) unless input.options[:polymorphic]
|
15
17
|
when ActiveRecord::Reflection::MacroReflection
|
16
|
-
input.klass unless input.options[:polymorphic]
|
18
|
+
((input.klass rescue nil).presence || _klass_by_name(input.class_name)) unless input.options[:polymorphic]
|
17
19
|
when ActionDispatch::Journey::Route
|
18
20
|
_klass_by_name(input.defaults[:controller])
|
19
21
|
when Class
|
@@ -42,6 +44,8 @@ module Effective
|
|
42
44
|
return klass
|
43
45
|
end
|
44
46
|
end
|
47
|
+
|
48
|
+
nil
|
45
49
|
end
|
46
50
|
|
47
51
|
def _initialize_relation(input)
|
@@ -4,7 +4,7 @@ module Effective
|
|
4
4
|
SPLIT = /\/|::/ # / or ::
|
5
5
|
|
6
6
|
def name # 'post'
|
7
|
-
@name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.
|
7
|
+
@name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.underscore
|
8
8
|
end
|
9
9
|
|
10
10
|
def plural_name # 'posts'
|
@@ -186,11 +186,22 @@ module Effective
|
|
186
186
|
.pluck(klass.primary_key)
|
187
187
|
elsif association.macro == :has_many && association.options[:through].present?
|
188
188
|
key = sql_column(klass.primary_key)
|
189
|
-
values = relation.pluck(association.source_reflection.klass.primary_key).uniq.compact
|
190
189
|
|
191
|
-
|
192
|
-
|
193
|
-
|
190
|
+
if association.source_reflection.options[:polymorphic]
|
191
|
+
reflected_klass = association.klass
|
192
|
+
else
|
193
|
+
reflected_klass = association.source_reflection.klass
|
194
|
+
end
|
195
|
+
|
196
|
+
values = relation.pluck(reflected_klass.primary_key).uniq.compact
|
197
|
+
|
198
|
+
scope = association.through_reflection.klass.where(association.source_reflection.foreign_key => values)
|
199
|
+
|
200
|
+
if association.source_reflection.options[:polymorphic]
|
201
|
+
scope = scope.where(association.source_reflection.foreign_type => reflected_klass.name)
|
202
|
+
end
|
203
|
+
|
204
|
+
keys = scope.pluck(association.through_reflection.foreign_key)
|
194
205
|
elsif association.macro == :has_many
|
195
206
|
key = sql_column(klass.primary_key)
|
196
207
|
keys = relation.pluck(association.foreign_key)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: effective_resources
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Code and Effect
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|