effective_resources 1.0.4 → 1.0.5

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
  SHA1:
3
- metadata.gz: 12e0851418c01ba4290cee861341751689d05963
4
- data.tar.gz: a9ee4c64ac47d1abac493d72eece30383d77fad4
3
+ metadata.gz: c2ca1a9b6a0534a0caad17123408d88fdeea90ac
4
+ data.tar.gz: 309cb6aaf4f19f681fdd73964a223116923292f2
5
5
  SHA512:
6
- metadata.gz: d27c675dc474aaf51403453d98ce32b83264cfe7437d4178f58fc45dafbeb20b454d6450235e775fd9bee049eb94db8444ab8979e88752e26f68a9ef7a1e224b
7
- data.tar.gz: 0a749056b57fdd205fba43497bff68bbfd61d0ed61617efea637902453b9974886064345a01e50cb388d375bd82abf2bf69769e48bad0cbdd37e5c684048b58b
6
+ metadata.gz: 1ec2cd136f7c917caa82dba82a140ef833f87a1e1aae1d060aa3f662de5092acbb2dc194dab2e5712cdd1edf74278d6449ded873bc89bf0140401778c8fad26f
7
+ data.tar.gz: fab0e5230b8ce493cb1355f4bca6aa90ef9c206abe266e73e8f1f9e1ea2e1c6919b7a846b7812fdc4d946aec01d96234ca283a9e0ea01c2e8e345e8bbfc6628d
@@ -72,7 +72,7 @@ module Effective
72
72
  effective_resource.nested_resources.each do |nested|
73
73
  if (nested_params = permitted_params_for(nested.klass)).present?
74
74
  nested_params.insert(nested_params.rindex { |obj| !obj.kind_of?(Hash)} + 1, :_destroy)
75
- permitted_params << { "#{nested.plural_name}_attributes".to_sym => nested_params }
75
+ permitted_params << { "#{nested.name}_attributes".to_sym => nested_params }
76
76
  end
77
77
  end
78
78
 
@@ -14,9 +14,7 @@ module Effective
14
14
 
15
15
  on = ons[params[:commit].to_s] || ons[action] || ons[commit[:action]]
16
16
 
17
- commit.reverse_merge!(on) if on.present?
18
-
19
- commit
17
+ on.present? ? commit.reverse_merge(on) : commit
20
18
  end
21
19
 
22
20
  # This calls the appropriate member action, probably save!, on the resource.
@@ -60,6 +60,7 @@ module EffectiveResourcesHelper
60
60
  raise 'expected first argument to be an ActiveRecord::Base object or Array of objects' unless resource.kind_of?(ActiveRecord::Base) || resource.kind_of?(Class) || resource.kind_of?(Array)
61
61
  raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
62
62
 
63
+ btn_class = atts.delete(:btn_class)
63
64
  locals = atts.delete(:locals) || {}
64
65
  partial = atts.delete(:partial)
65
66
  spacer_template = locals.delete(:spacer_template)
@@ -80,7 +81,7 @@ module EffectiveResourcesHelper
80
81
  partial = (partial.presence || 'effective/resource/actions') + '.html'
81
82
 
82
83
  # Assign Locals
83
- locals = { resource: resource, effective_resource: effective_resource, namespace: namespace, actions: actions }.compact.merge(locals)
84
+ locals = { resource: resource, effective_resource: effective_resource, namespace: namespace, actions: actions, btn_class: btn_class.to_s }.compact.merge(locals)
84
85
 
85
86
  # Render
86
87
  if resource.kind_of?(Array)
@@ -24,12 +24,12 @@ module EffectiveResourcesPrivateHelper
24
24
 
25
25
  # Replace resource name in any token strings
26
26
  if opts.key?('data-confirm')
27
- opts['data-confirm'].gsub!('@resource', (resource.to_s.presence || resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ')).to_s)
27
+ opts['data-confirm'] = opts['data-confirm'].gsub('@resource', (resource.to_s.presence || resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ')).to_s)
28
28
  end
29
29
 
30
30
  # Assign class
31
31
  opts[:class] ||= (
32
- if opts['data-method'] == 'delete'
32
+ if opts['data-method'].to_s == 'delete'
33
33
  'btn btn-danger'
34
34
  elsif h.length == 0
35
35
  'btn btn-primary'
@@ -34,7 +34,8 @@ module Effective
34
34
  when :duration ; :duration
35
35
  when :email ; :email
36
36
  when :integer ; :integer
37
- when :percentage ; :percentage
37
+ when :percent ; :percent
38
+ when :percentage ; :percent
38
39
  when :phone ; :phone
39
40
  when :price ; :price
40
41
  when :nil ; :nil
@@ -101,14 +102,8 @@ module Effective
101
102
  EffectiveRoles.roles.include?(value.to_sym) ? value : EffectiveRoles.roles_for(value)
102
103
  when :integer
103
104
  (value.kind_of?(String) ? value.gsub(/\D/, '') : value).to_i
104
- when :percentage
105
- # We want this to return 0.81 when we type 81% or 0.81 or 81.5
106
- if value.to_s.include?('.')
107
- value = value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f
108
- value >= 1.0 ? (value / 100.0) : value
109
- else
110
- (value.to_s.gsub(/\D/, '').to_f / 100.0)
111
- end
105
+ when :percent # Integer * 1000. Percentage to 3 digits.
106
+ value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 1000.0).round
112
107
  when :phone
113
108
  digits = value.to_s.gsub(/\D/, '').chars
114
109
  digits = (digits.first == '1' ? digits[1..10] : digits[0..9]) # Throw away a leading 1
@@ -120,8 +115,8 @@ module Effective
120
115
  (value.kind_of?(String) ? value.gsub(/\D/, '') : value).to_i
121
116
  when :nil
122
117
  value.presence
123
- when :price
124
- (value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 100.0)).to_i
118
+ when :price # Integer * 100. Number of cents.
119
+ value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 100.0).round
125
120
  when :string, :text, :email
126
121
  value.to_s
127
122
  when :belongs_to_polymorphic
@@ -36,6 +36,10 @@ module Effective
36
36
  # Effective::Resource.new('admin/posts').action_path(:edit, Post.last) => '/admin/posts/3/edit'
37
37
  # Will work for any action. Returns the real path
38
38
  def action_path(action, resource = nil, opts = {})
39
+ if klass.nil? && resource.present? && initialized_name.kind_of?(ActiveRecord::Reflection::BelongsToReflection)
40
+ return Effective::Resource.new(resource, namespace: namespace).action_path(action, resource, opts)
41
+ end
42
+
39
43
  return unless routes[action]
40
44
 
41
45
  if resource.kind_of?(Hash)
@@ -43,7 +47,7 @@ module Effective
43
47
  end
44
48
 
45
49
  # edge case: Effective::Resource.new('admin/comments').action_path(:new, @post)
46
- if resource.present? && !resource.kind_of?(klass)
50
+ if resource && klass && !resource.kind_of?(klass)
47
51
  if (bt = belongs_to(resource)).present? && instance.respond_to?("#{bt.name}=")
48
52
  return routes[action].format(klass.new(bt.name => resource)).presence
49
53
  end
@@ -45,7 +45,8 @@ module Effective
45
45
 
46
46
  def nested_resources
47
47
  return [] unless klass.respond_to?(:reflect_on_all_associations)
48
- klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] }
48
+ klass.reflect_on_all_associations(:has_many).select { |ass| ass.options[:autosave] } +
49
+ klass.reflect_on_all_associations(:has_one).select { |ass| ass.options[:autosave] }
49
50
  end
50
51
 
51
52
  def associated(name)
@@ -8,8 +8,16 @@ module Effective
8
8
  when :belongs_to
9
9
  { as: :select }.merge(search_form_field_collection(belongs_to(name)))
10
10
  when :belongs_to_polymorphic
11
- #{ as: :select, grouped: true, polymorphic: true, collection: nil}
12
- { as: :string }
11
+ constant_pluralized = name.to_s.upcase
12
+ constant = name.to_s.pluralize.upcase
13
+
14
+ if defined?("#{klass.name}::#{constant}")
15
+ { as: :select, polymorphic: true, collection: klass.const_get(constant) }
16
+ elsif defined?("#{klass.name}::#{constant_pluralized}")
17
+ { as: :select, polymorphic: true, collection: klass.const_get(constant_pluralized) }
18
+ else
19
+ { as: :string }
20
+ end
13
21
  when :has_and_belongs_to_many
14
22
  { as: :select }.merge(search_form_field_collection(has_and_belongs_to_many(name)))
15
23
  when :has_many
@@ -79,7 +79,9 @@ module Effective
79
79
  when :belongs_to_polymorphic
80
80
  (type, id) = term.split('_')
81
81
 
82
- if type.present? && id.present?
82
+ if term == 'nil'
83
+ relation.where(is_null("#{sql_column}_id")).where(is_null("#{sql_column}_type"))
84
+ elsif type.present? && id.present?
83
85
  relation.where("#{sql_column}_id = ?", id).where("#{sql_column}_type = ?", type)
84
86
  else
85
87
  id ||= Effective::Attribute.new(:integer).parse(term)
@@ -133,8 +135,8 @@ module Effective
133
135
  end
134
136
  when :integer
135
137
  relation.where("#{sql_column} = ?", term)
136
- when :percentage
137
- relation.where("#{sql_column} = ?", sql_type(name) == :integer ? (term * 100).to_i : term)
138
+ when :percent
139
+ relation.where("#{sql_column} = ?", term)
138
140
  when :price
139
141
  relation.where("#{sql_column} = ?", term)
140
142
  when :string, :text, :email
@@ -193,38 +195,54 @@ module Effective
193
195
  key = sql_column(klass.primary_key)
194
196
  values = relation.pluck(association.source_reflection.klass.primary_key).uniq.compact
195
197
 
196
- keys = klass.joins(association.name)
197
- .where(association.name => { association.source_reflection.klass.primary_key => values })
198
- .pluck(klass.primary_key)
198
+ keys = if value == 'nil'
199
+ klass.where.not(klass.primary_key => klass.joins(association.name)).pluck(klass.primary_key)
200
+ else
201
+ klass.joins(association.name)
202
+ .where(association.name => { association.source_reflection.klass.primary_key => values })
203
+ .pluck(klass.primary_key)
204
+ end
199
205
  elsif association.options[:through].present?
200
- key = sql_column(klass.primary_key)
206
+ scope = association.through_reflection.klass.all
201
207
 
202
208
  if association.source_reflection.options[:polymorphic]
203
209
  reflected_klass = association.klass
210
+ scope = scope.where(association.source_reflection.foreign_type => reflected_klass.name)
204
211
  else
205
212
  reflected_klass = association.source_reflection.klass
206
213
  end
207
214
 
208
- values = relation.pluck(reflected_klass.primary_key).uniq.compact
209
-
210
- scope = association.through_reflection.klass.where(association.source_reflection.foreign_key => values)
211
-
212
- if association.source_reflection.options[:polymorphic]
213
- scope = scope.where(association.source_reflection.foreign_type => reflected_klass.name)
214
- end
215
-
216
215
  if association.through_reflection.macro == :belongs_to
217
216
  key = association.through_reflection.foreign_key
218
- keys = scope.pluck(association.through_reflection.klass.primary_key)
217
+ pluck_key = association.through_reflection.klass.primary_key
219
218
  else
220
- keys = scope.pluck(association.through_reflection.foreign_key)
219
+ key = sql_column(klass.primary_key)
220
+ pluck_key = association.through_reflection.foreign_key
221
221
  end
222
+
223
+ if value == 'nil'
224
+ keys = klass.where.not(klass.primary_key => scope.pluck(pluck_key)).pluck(klass.primary_key)
225
+ else
226
+ keys = scope.where(association.source_reflection.foreign_key => relation).pluck(pluck_key)
227
+ end
228
+
222
229
  elsif association.macro == :has_many
223
230
  key = sql_column(klass.primary_key)
224
- keys = relation.pluck(association.foreign_key)
231
+
232
+ keys = if value == 'nil'
233
+ klass.where.not(klass.primary_key => resource.klass.pluck(association.foreign_key)).pluck(klass.primary_key)
234
+ else
235
+ relation.pluck(association.foreign_key)
236
+ end
237
+
225
238
  elsif association.macro == :has_one
226
239
  key = sql_column(klass.primary_key)
227
- keys = relation.pluck(association.foreign_key)
240
+
241
+ keys = if value == 'nil'
242
+ klass.where.not(klass.primary_key => resource.klass.pluck(association.foreign_key)).pluck(klass.primary_key)
243
+ else
244
+ relation.pluck(association.foreign_key)
245
+ end
228
246
  end
229
247
 
230
248
  "#{key} IN (#{(keys.uniq.compact.presence || [0]).join(',')})"
@@ -1,6 +1,6 @@
1
- = dropdown(variation: :dropleft) do
1
+ = dropdown(variation: :dropleft, btn_class: btn_class) do
2
2
  - permitted_resource_actions(resource, actions).each do |label, opts|
3
- = dropdown_link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
3
+ = dropdown_link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class).merge(btn_class: btn_class))
4
4
 
5
5
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
6
6
  = yield(resource) if block_given?
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.0.4'.freeze
2
+ VERSION = '1.0.5'.freeze
3
3
  end
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
4
+ version: 1.0.5
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-10-26 00:00:00.000000000 Z
11
+ date: 2018-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails