effective_resources 0.3.3 → 0.3.4

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: 092de6a5657f044dc7d24ef202f01b47b666a2f7
4
- data.tar.gz: cb227dbf96b28597e2d8d9e05823895dd383874a
3
+ metadata.gz: 80f8bb76be22a155d74952c74d008382e1ba7cd7
4
+ data.tar.gz: 06fa5f406c156ed2dbe2ef413e3f0f5a57a6214c
5
5
  SHA512:
6
- metadata.gz: 1bccc81b7dc7044112946de7037065d0db21256bf8cd573f9f76fd8cec53d7ea1973018a57df48128459c7282dd1d0fa424ea8ac3707b9597683388728a88a92
7
- data.tar.gz: f06f6e0235e2e40dc429c42e1c8b274403457038baa2f1dfc6c2f73508db0d6f79acc9246fec35fb79f76f6d8100576dcac52aa8a61cd7ef5b8931dd22ff6182
6
+ metadata.gz: 6a3aab4b276571bdf76024ea8b381173b640fb748c06126d565f84f3fdf92c62177fd5ac61f13dce78cff23dce3fbc25799ecae87273cc5347a8925fcc778e60
7
+ data.tar.gz: 42ed251bf078e6b173639083e8b68bc5b2fa0955b3bba97e3214c9330e9d0a284d3664304acadf1242916a7f09665f95850ea401a527b15cb4def8ffcc7f2c9b
@@ -92,7 +92,7 @@ module Effective
92
92
  value.to_s
93
93
  when :belongs_to_polymorphic
94
94
  value.to_s
95
- when :belongs_to, :has_many, :has_and_belongs_to_many, :has_one, :resource # Returns an Array of ints, an Int or String
95
+ when :belongs_to, :has_many, :has_and_belongs_to_many, :has_one, :resource, :effective_addresses # Returns an Array of ints, an Int or String
96
96
  if value.kind_of?(Integer) || value.kind_of?(Array)
97
97
  value
98
98
  else
@@ -109,7 +109,7 @@ module Effective
109
109
  end
110
110
  end
111
111
  else
112
- raise 'unsupported type'
112
+ raise "unsupported type #{type}"
113
113
  end
114
114
  end
115
115
 
@@ -12,8 +12,8 @@ module Effective
12
12
  include Effective::Resources::Sql
13
13
 
14
14
  # post, Post, Admin::Post, admin::Post, admin/posts, admin/post, admin/effective::post
15
- def initialize(input)
16
- _initialize(input)
15
+ def initialize(input, namespace: nil)
16
+ _initialize(input, namespace: namespace)
17
17
  end
18
18
 
19
19
  def to_s
@@ -33,7 +33,7 @@ module Effective
33
33
 
34
34
  def associated(name)
35
35
  name = (name.to_s.end_with?('_id') ? name.to_s[0...-3] : name).to_sym
36
- klass.reflect_on_all_associations.find { |ass| ass.name == name }
36
+ klass.reflect_on_all_associations.find { |ass| ass.name == name } || effective_addresses(name)
37
37
  end
38
38
 
39
39
  def belongs_to(name)
@@ -61,6 +61,18 @@ module Effective
61
61
  has_ones.find { |ass| ass.name == name }
62
62
  end
63
63
 
64
+ def effective_addresses(name)
65
+ return unless defined?(EffectiveAddresses) && has_many(:addresses).try(:klass) == Effective::Address
66
+
67
+ name = name.to_s.downcase
68
+ return unless name.end_with?('_address') || name.end_with?('_addresses')
69
+
70
+ category = name.split('_').reject { |name| name == 'address' || name == 'addresses' }.join('_')
71
+ return unless category.present?
72
+
73
+ Effective::Address.where(category: category).where(addressable_type: class_name)
74
+ end
75
+
64
76
  def nested_resource(name)
65
77
  name = name.to_sym
66
78
  nested_resources.find { |ass| ass.name == name }
@@ -23,7 +23,7 @@ module Effective
23
23
  when :effective_obfuscation
24
24
  { as: :effective_obfuscation }
25
25
  when :boolean
26
- { as: :boolean, collection: [['true', true], ['false', false]] }
26
+ { as: :boolean, collection: [['Yes', true], ['No', false]] }
27
27
  when :datetime
28
28
  { as: :datetime }
29
29
  when :date
@@ -4,41 +4,42 @@ module Effective
4
4
 
5
5
  private
6
6
 
7
- def _initialize(obj)
8
- @input_name = _initialize_input_name(obj)
9
- _initialize_names(@input_name)
7
+ def _initialize(input, namespace: nil)
8
+ @namespaces = (namespace.kind_of?(String) ? namespace.split('/') : Array(namespace)) if namespace
10
9
 
11
- @relation = _initialize_relation(obj)
12
- @instance = obj if (klass && obj.instance_of?(klass))
13
- end
14
-
15
- def _initialize_input_name(input)
16
- case input
17
- when String ; input.start_with?('/') ? input[1..-1] : input
18
- when Symbol ; input
19
- when Class ; input.name
10
+ @model_klass = case input
11
+ when String, Symbol
12
+ _klass_by_name(input)
20
13
  when ActiveRecord::Relation
21
14
  input.klass
22
15
  when ActiveRecord::Reflection::AbstractReflection
23
- @model_klass = input.klass unless input.options[:polymorphic]
24
- input.name
16
+ input.klass unless input.options[:polymorphic]
25
17
  when ActionDispatch::Journey::Route
26
- input.defaults[:controller]
18
+ _klass_by_name(input.defaults[:controller])
19
+ when Class
20
+ input
27
21
  when nil ; raise 'expected a string or class'
28
- else ; input.class.name
29
- end.to_s.underscore
22
+ else ; _klass_by_name(input.class.name)
23
+ end
24
+
25
+ @relation = _initialize_relation(input)
26
+ @instance = input if (klass && input.instance_of?(klass))
30
27
  end
31
28
 
32
- def _initialize_names(input)
29
+ def _klass_by_name(input)
30
+ input = input.to_s
31
+ input = input[1..-1] if input.start_with?('/')
32
+
33
33
  names = input.split('/')
34
34
 
35
35
  0.upto(names.length-1) do |index|
36
36
  class_name = (names[index..-1].map { |name| name.classify } * '::')
37
+ klass = (class_name.safe_constantize rescue nil)
37
38
 
38
- if (@model_klass ||= class_name.safe_constantize).present?
39
- @class_name = class_name
39
+ if klass.present?
40
40
  @namespaces = names[0...index]
41
- break
41
+ @model_klass = klass
42
+ return klass
42
43
  end
43
44
  end
44
45
  end
@@ -4,7 +4,7 @@ module Effective
4
4
  SPLIT = /\/|::/ # / or ::
5
5
 
6
6
  def name # 'post'
7
- @name ||= @input_name.split(SPLIT).last.singularize
7
+ @name ||= (klass.try(:name).to_s.split(SPLIT).last || '').singularize.downcase
8
8
  end
9
9
 
10
10
  def plural_name # 'posts'
@@ -12,7 +12,7 @@ module Effective
12
12
  end
13
13
 
14
14
  def class_name # 'Effective::Post'
15
- @model_klass.try(:name) || @class_name || ''
15
+ @model_klass.try(:name).to_s
16
16
  end
17
17
 
18
18
  def class_path # 'effective'
@@ -20,7 +20,7 @@ module Effective
20
20
  end
21
21
 
22
22
  def namespaced_class_name # 'Admin::Effective::Post'
23
- (namespaces.to_a.map { |name| name.classify } + [class_name]) * '::'
23
+ (Array(namespaces).map { |name| name.classify } + [class_name]) * '::'
24
24
  end
25
25
 
26
26
  def namespace # 'admin/things'
@@ -31,6 +31,10 @@ module Effective
31
31
  relation
32
32
  .order(order_by_associated_conditions(association, sort: sort, direction: direction))
33
33
  .order("#{sql_column(klass.primary_key)} #{sql_direction}")
34
+ when :effective_addresses
35
+ relation
36
+ .order(order_by_associated_conditions(associated(:addresses), sort: sort, direction: direction))
37
+ .order("#{sql_column(klass.primary_key)} #{sql_direction}")
34
38
  when :effective_roles
35
39
  relation.order("#{sql_column(:roles_mask)} #{sql_direction}")
36
40
  when :string, :text
@@ -57,6 +61,10 @@ module Effective
57
61
 
58
62
  term = Effective::Attribute.new(sql_type, klass: (association.try(:klass) rescue nil) || klass).parse(value, name: name)
59
63
 
64
+ if term == 'nil' && ![:has_and_belongs_to_many, :has_many, :has_one, :belongs_to_polymorphic, :effective_roles].include?(sql_type)
65
+ return relation.where("#{sql_column} IS NULL")
66
+ end
67
+
60
68
  case sql_type
61
69
  when :belongs_to, :has_and_belongs_to_many, :has_many, :has_one
62
70
  relation.where(search_by_associated_conditions(association, term, fuzzy: fuzzy))
@@ -70,7 +78,7 @@ module Effective
70
78
  relation.where("#{sql_column} = ? OR #{sql_column.sub('_id', '_type')} = ?", id, (type || term))
71
79
  end
72
80
  when :effective_addresses
73
- raise 'not yet implemented'
81
+ relation.where(id: Effective::Resource.new(association).search_any(value, fuzzy: fuzzy).pluck(:addressable_id))
74
82
  when :effective_obfuscation
75
83
  # If value == term, it's an invalid deobfuscated id
76
84
  relation.where("#{sql_column} = ?", (value == term ? 0 : term))
@@ -124,7 +132,7 @@ module Effective
124
132
  relation.where("#{sql_column} = ?", term)
125
133
  end
126
134
  else
127
- raise 'unsupported sql type'
135
+ raise "unsupported sql type #{sql_type}"
128
136
  end
129
137
  end
130
138
 
@@ -200,7 +208,7 @@ module Effective
200
208
  # Order the target model for its matching records / keys
201
209
  sort_column = (sort unless sort == true) || resource.sort_column
202
210
 
203
- relation = resource.relation.order("#{resource.sql_column(sort_column)} #{sql_direction(direction)}")
211
+ relation = resource.relation.reorder(nil).order("#{resource.sql_column(sort_column)} #{sql_direction(direction)}")
204
212
 
205
213
  if association.options[:as] # polymorphic
206
214
  relation = relation.where(association.type => klass.name)
@@ -20,7 +20,7 @@ module Effective
20
20
  end
21
21
 
22
22
  def max_id
23
- @max_id ||= klass.maximum(klass.primary_key).to_i
23
+ @max_id ||= klass.unscoped.maximum(klass.primary_key).to_i
24
24
  end
25
25
 
26
26
  def sql_column(name)
@@ -52,6 +52,8 @@ module Effective
52
52
  :effective_obfuscation
53
53
  elsif name == 'roles' && defined?(EffectiveRoles) && klass.respond_to?(:with_role)
54
54
  :effective_roles
55
+ elsif (name.include?('_address') || name.include?('_addresses')) && defined?(EffectiveAddresses) && (klass.new rescue nil).respond_to?(:effective_addresses)
56
+ :effective_addresses
55
57
  elsif (column = column(name))
56
58
  column.type
57
59
  elsif name.ends_with?('_id')
@@ -64,7 +66,7 @@ module Effective
64
66
  # This tries to figure out the column we should order this collection by.
65
67
  # Whatever would match up with the .to_s
66
68
  def sort_column
67
- ['name', 'title', 'label', 'first_name', 'subject', 'description', 'email'].each do |name|
69
+ ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'description'].each do |name|
68
70
  return name if column_names.include?(name)
69
71
  end
70
72
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.3.4'.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: 0.3.3
4
+ version: 0.3.4
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-05 00:00:00.000000000 Z
11
+ date: 2017-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails