effective_resources 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/app/controllers/concerns/effective/crud_controller/actions.rb +13 -3
- data/app/controllers/concerns/effective/crud_controller/permitted_params.rb +3 -1
- data/app/models/effective/model_reader.rb +4 -1
- data/app/models/effective/resources/associations.rb +17 -5
- data/app/models/effective/resources/attributes.rb +41 -8
- data/app/models/effective/resources/controller.rb +2 -2
- data/app/models/effective/resources/model.rb +1 -1
- 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: c677c888d40faa747d421ffbeecbf1452e5328db
|
4
|
+
data.tar.gz: 2bf38b93293b53d35a86f1e9c551b1cfec07489f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab530180ab92b4e5a866b34d38941d0234e73564c4291b02b661f675eb399d4bbd5fcf4445b6db935734da154a36f1a120255fe05839fcc7b814d92784a61cf2
|
7
|
+
data.tar.gz: dd14ce62821ee55c08ded91a33fca6a57a17ecf3a857d9aad138b7586b8590a0304252f652dc7ebb519799836dc3c100699e9b640bf42b014e74d7a45c11aa9e
|
data/MIT-LICENSE
CHANGED
@@ -23,10 +23,20 @@ module Effective
|
|
23
23
|
|
24
24
|
self.resource ||= resource_scope.new
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
# Assign any passed params
|
27
|
+
to_assign = if params[:_datatable_id].present?
|
28
|
+
inline_datatable = EffectiveDatatables.find(params[:_datatable_id])
|
29
|
+
inline_datatable.view = view_context
|
30
|
+
inline_datatable.attributes
|
31
|
+
elsif params.present?
|
32
|
+
params.to_unsafe_h.except(:controller, :action, :id, :duplicate_id)
|
33
|
+
end
|
34
|
+
|
35
|
+
if to_assign.present?
|
36
|
+
resource.assign_attributes(to_assign.select { |k, v| resource.respond_to?("#{k}=") })
|
37
|
+
end
|
29
38
|
|
39
|
+
# Duplicate if possible
|
30
40
|
if params[:duplicate_id]
|
31
41
|
duplicate = resource_scope.find(params[:duplicate_id])
|
32
42
|
EffectiveResources.authorize!(self, :show, duplicate)
|
@@ -48,7 +48,9 @@ module Effective
|
|
48
48
|
(effective_resource.namespaces & Array(permitted).map(&:to_s)).present?
|
49
49
|
end
|
50
50
|
end
|
51
|
-
end
|
51
|
+
end
|
52
|
+
|
53
|
+
permitted_params = permitted_params.map { |k, v| v.first == :array ? { k => [] } : k }
|
52
54
|
|
53
55
|
# Recursively add any accepts_nested_resources
|
54
56
|
effective_resource.nested_resources.each do |nested|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Effective
|
2
2
|
class ModelReader
|
3
|
-
DATATYPES = [:binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text]
|
3
|
+
DATATYPES = [:binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text, :permitted_param]
|
4
4
|
|
5
5
|
attr_reader :attributes
|
6
6
|
|
@@ -18,6 +18,9 @@ module Effective
|
|
18
18
|
return
|
19
19
|
end
|
20
20
|
|
21
|
+
# Not really an attribute, just a permitted param.
|
22
|
+
args.unshift(:permitted_param) if args.first.kind_of?(Hash) && args.first.key?(:permitted)
|
23
|
+
|
21
24
|
unless DATATYPES.include?(args.first)
|
22
25
|
raise "expected first argument to be a datatype. Try name :string"
|
23
26
|
end
|
@@ -8,7 +8,7 @@ module Effective
|
|
8
8
|
|
9
9
|
def belong_tos
|
10
10
|
return [] unless klass.respond_to?(:reflect_on_all_associations)
|
11
|
-
|
11
|
+
klass.reflect_on_all_associations(:belongs_to)
|
12
12
|
end
|
13
13
|
|
14
14
|
# author_id, post_id
|
@@ -16,24 +16,36 @@ module Effective
|
|
16
16
|
belong_tos.map { |ass| (ass.options[:foreign_key] || "#{ass.name}_id").to_sym }
|
17
17
|
end
|
18
18
|
|
19
|
+
def has_anys
|
20
|
+
(has_ones + has_manys + has_and_belongs_to_manys)
|
21
|
+
end
|
22
|
+
|
23
|
+
def has_manys_ids
|
24
|
+
(has_manys + has_and_belongs_to_manys).map { |ass| "#{ass.plural_name.singularize}_ids".to_sym }
|
25
|
+
end
|
26
|
+
|
19
27
|
def has_ones
|
20
28
|
return [] unless klass.respond_to?(:reflect_on_all_associations)
|
21
|
-
|
29
|
+
klass.reflect_on_all_associations(:has_one)
|
30
|
+
end
|
31
|
+
|
32
|
+
def has_ones_ids
|
33
|
+
has_ones.map { |ass| "#{ass.name}_id".to_sym }
|
22
34
|
end
|
23
35
|
|
24
36
|
def has_manys
|
25
37
|
return [] unless klass.respond_to?(:reflect_on_all_associations)
|
26
|
-
|
38
|
+
klass.reflect_on_all_associations(:has_many).reject { |ass| ass.options[:autosave] }
|
27
39
|
end
|
28
40
|
|
29
41
|
def has_and_belongs_to_manys
|
30
42
|
return [] unless klass.respond_to?(:reflect_on_all_associations)
|
31
|
-
|
43
|
+
klass.reflect_on_all_associations(:has_and_belongs_to_many)
|
32
44
|
end
|
33
45
|
|
34
46
|
def nested_resources
|
35
47
|
return [] unless klass.respond_to?(:reflect_on_all_associations)
|
36
|
-
|
48
|
+
klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] }
|
37
49
|
end
|
38
50
|
|
39
51
|
def associated(name)
|
@@ -8,31 +8,64 @@ module Effective
|
|
8
8
|
(klass_attributes.presence || model_attributes.presence)
|
9
9
|
end
|
10
10
|
|
11
|
+
def primary_key_attribute
|
12
|
+
{klass.primary_key.to_sym => [:integer]}
|
13
|
+
end
|
14
|
+
|
11
15
|
# The attributes for each belongs_to
|
12
16
|
# { :client_id => [:integer], ... }
|
13
17
|
def belong_tos_attributes
|
14
18
|
belong_tos.inject({}) do |h, ass|
|
15
19
|
unless ass.foreign_key == 'site_id' && ass.respond_to?(:acts_as_site_specific)
|
16
|
-
h[ass.foreign_key.to_sym] = [:integer]
|
20
|
+
h[ass.foreign_key.to_sym] = [:integer, :index => true]
|
17
21
|
end; h
|
18
22
|
end
|
19
23
|
end
|
20
24
|
|
25
|
+
def has_manys_attributes
|
26
|
+
has_manys_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
|
27
|
+
end
|
28
|
+
|
29
|
+
# All will include primary_key, created_at, updated_at and belongs_tos
|
21
30
|
# This is the attributes as defined by the effective_resources do .. end block
|
22
31
|
# { :name => [:string, { permitted: false }], ... }
|
23
|
-
def model_attributes
|
24
|
-
model ? model.attributes : {}
|
32
|
+
def model_attributes(all: false)
|
33
|
+
atts = (model ? model.attributes : {})
|
34
|
+
|
35
|
+
if all # Probably being called by permitted_attributes
|
36
|
+
primary_key_attribute.merge(belong_tos_attributes).merge(has_manys_attributes).merge(atts)
|
37
|
+
else # This is the migrator. This should match table_attributes
|
38
|
+
belong_tos_attributes.merge(atts.reject { |_, v| v[0] == :permitted_param })
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# All table attributes. includes primary_key and belongs_tos
|
43
|
+
def table_attributes
|
44
|
+
attributes = (klass.new().attributes rescue nil)
|
45
|
+
return {} unless attributes
|
46
|
+
|
47
|
+
(attributes.keys - [klass.primary_key]).inject({}) do |h, name|
|
48
|
+
if klass.respond_to?(:column_for_attribute) # Rails 4+
|
49
|
+
h[name.to_sym] = [klass.column_for_attribute(name).type]
|
50
|
+
else
|
51
|
+
h[name.to_sym] = [klass.columns_hash[name].type]
|
52
|
+
end; h
|
53
|
+
end
|
25
54
|
end
|
26
55
|
|
27
56
|
# Used by effective_crud_controller to generate the permitted params
|
28
57
|
def permitted_attributes
|
29
|
-
id = {klass.primary_key.to_sym => [:integer]}
|
30
|
-
bts = belong_tos_ids.inject({}) { |h, ass| h[ass] = [:integer]; h }
|
31
|
-
|
32
|
-
|
58
|
+
# id = {klass.primary_key.to_sym => [:integer]}
|
59
|
+
# bts = belong_tos_ids.inject({}) { |h, ass| h[ass] = [:integer]; h }
|
60
|
+
# has_manys = has_manys_ids.inject({}) { |h, ass| h[ass] = [:array]; h }
|
61
|
+
# has_manys.each { |k, _| has_manys[k] = model_attributes[k] if model_attributes.key?(k) }
|
62
|
+
# Does not include nested, as they are added recursively elsewhere
|
63
|
+
# id.merge(bts).merge(model_attributes).merge(has_manys)
|
64
|
+
|
65
|
+
model_attributes(all: true)
|
33
66
|
end
|
34
67
|
|
35
|
-
# All attributes from the klass, sorted as per attributes block.
|
68
|
+
# All attributes from the klass, sorted as per model attributes block.
|
36
69
|
# Does not include :id, :created_at, :updated_at unless all is passed
|
37
70
|
def klass_attributes(all: false)
|
38
71
|
attributes = (klass.new().attributes rescue nil)
|
@@ -13,11 +13,11 @@ module Effective
|
|
13
13
|
end
|
14
14
|
|
15
15
|
if actions.find { |a| a == :index }
|
16
|
-
submits['Continue'] = { action: :save, redirect: :index, default: true }
|
16
|
+
submits['Continue'] = { action: :save, redirect: :index, default: true, unless: -> { params[:_datatable_id] } }
|
17
17
|
end
|
18
18
|
|
19
19
|
if actions.find { |a| a == :new }
|
20
|
-
submits['Add New'] = { action: :save, redirect: :new, default: true }
|
20
|
+
submits['Add New'] = { action: :save, redirect: :new, default: true, unless: -> { params[:_datatable_id] } }
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -7,7 +7,7 @@ module Effective
|
|
7
7
|
@model = ModelReader.new(&block)
|
8
8
|
|
9
9
|
# If effective_developer is in live mode, this will cause it to refresh the class
|
10
|
-
ActiveSupport.run_load_hooks(:effective_resource,
|
10
|
+
ActiveSupport.run_load_hooks(:effective_resource, self)
|
11
11
|
end
|
12
12
|
|
13
13
|
def model
|
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: 1.0.
|
4
|
+
version: 1.0.1
|
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: 2018-09-
|
11
|
+
date: 2018-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|