effective_resources 2.5.4 → 2.6.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: b61642f0df33f0c4ae152ce94fe4e8b455c05ef012d156bcfd3cfa49b6f99f2c
4
- data.tar.gz: 1c2333209641f7c555322808ed7f5980cd02620d206cd6538072a863494f11de
3
+ metadata.gz: '00988bb634b26b3acd5d90cf94a2cffbb55dc5d4c73db5afe6668d06860a3091'
4
+ data.tar.gz: d55a09844c23668b754c9ac1d43a6f087330620b32d642b7ceca1990f00a3134
5
5
  SHA512:
6
- metadata.gz: 0baedf6b1cfe7d25ad4f551cfc710b64b5253154dfed6a7b17d2d745cded561e2d1f516f973bbd9c165ace4747a7c48806e7e70dbfd4b13d7ede919155655500
7
- data.tar.gz: 97b02b7a8da7a7f9e81758cdffb3cc2f42d84a99aeb2840e69d05c8e80973b4d5170edabcb85b21a902c1be8a07464c65aca463304746884461e61fec6c9e980
6
+ metadata.gz: 452023c6b1e60a9317914409b9da54887ec6f686f49c762c7ea87c574ed77172e92519edc685800d297d87976eb936ace8ee3978f0be7f20602c201c0148d366
7
+ data.tar.gz: 65021dd0297c40f44d1aef163c8dcfbfa4c94aea720ec42290f164ae3f00950fcd79a1df3167eaac86ca1916c9a10f86ed8655d5e5c2788d1a3a0f8973a4bfa2
@@ -24,67 +24,6 @@ module ActsAsStatused
24
24
  end
25
25
  end
26
26
 
27
- module CanCan
28
- # The idea here is you can go forward but you can't go back.
29
- def acts_as_statused(klass, only: nil, except: nil)
30
- raise "klass does not implement acts_as_statused" unless klass.acts_as_statused?
31
-
32
- statuses = klass.const_get(:STATUSES)
33
- instance = klass.new
34
-
35
- only = Array(only).compact
36
- except = Array(except).compact
37
-
38
- statuses.each_with_index do |status, index|
39
- action = status_active_verb(status, instance)
40
-
41
- next if action.blank?
42
- next if only.present? && !only.include?(action)
43
- next if except.present? && except.include?(action)
44
-
45
- if index == 0
46
- can(action, klass) and next
47
- end
48
-
49
- if status == :approved && statuses.include?(:declined)
50
- if (position = statuses.index { |status| (status == :approved || status == :declined) }) > 0
51
- can(action, klass) { |obj| obj.public_send("#{statuses[position-1]}?") || obj.declined? }
52
- next
53
- end
54
- end
55
-
56
- if status == :declined && statuses.include?(:approved)
57
- if (position = statuses.index { |status| (status == :approved || status == :declined) }) > 0
58
- can(action, klass) { |obj| obj.public_send("#{statuses[position-1]}?") }
59
- next
60
- end
61
- end
62
-
63
- can(action, klass) { |obj| obj.public_send("#{statuses[index-1]}?") }
64
- end
65
- end
66
-
67
- private
68
-
69
- # requested -> request, approved -> approve, declined -> decline, pending -> pending
70
- def status_active_verb(status, instance)
71
- status = status.to_s.strip
72
-
73
- if status.end_with?('ied')
74
- action = status[0...-3] + 'y'
75
- return action.to_sym if instance.respond_to?(action + '!')
76
- end
77
-
78
- # ed, e, ing
79
- [-1, -2, -3].each do |index|
80
- action = status[0...index]
81
- return action.to_sym if instance.respond_to?(action + '!')
82
- end
83
-
84
- nil
85
- end
86
- end
87
-
88
27
  included do
89
28
  acts_as_statused_options = @acts_as_statused_options
90
29
 
@@ -103,38 +42,85 @@ module ActsAsStatused
103
42
  self.status ||= self.class.const_get(:STATUSES).first
104
43
 
105
44
  # Set an existing belongs_to automatically
106
- if respond_to?("#{status}_by=") && respond_to?("#{status}_by") && send("#{status}_by").blank?
45
+ if respond_to?("#{status}_by") && send("#{status}_by").blank?
107
46
  self.send("#{status}_by=", current_user)
108
47
  end
109
48
 
110
49
  # Set an existing timestamp automatically
111
- if respond_to?("#{status}_at=") && respond_to?("#{status}_at") && send("#{status}_at").blank?
50
+ if respond_to?("#{status}_at") && send("#{status}_at").blank?
112
51
  self.send("#{status}_at=", Time.zone.now)
113
52
  end
114
53
 
54
+ if current_user.present?
55
+ self.status_steps["#{status}_by_id".to_sym] ||= current_user.id
56
+ self.status_steps["#{status}_by_type".to_sym] ||= current_user.class.name
57
+ end
58
+
115
59
  self.status_steps["#{status}_at".to_sym] ||= Time.zone.now
116
- self.status_steps["#{status}_by".to_sym] ||= current_user.try(:id)
117
60
  end
118
61
 
119
62
  validates :status, presence: true, inclusion: { in: const_get(:STATUSES).map(&:to_s) }
120
63
 
121
64
  # Create an received scope and approved? method for each status
122
65
  acts_as_statused_options[:statuses].each do |sym|
66
+ sym_at = "#{sym}_at".to_sym
67
+ sym_by = "#{sym}_by".to_sym
68
+ sym_by_id = "#{sym}_by_id".to_sym
69
+ sym_by_type = "#{sym}_by_type".to_sym
70
+
123
71
  scope(sym, -> { where(status: sym.to_s) })
124
72
 
73
+ # approved?
125
74
  define_method("#{sym}?") { status == sym.to_s }
126
- define_method("was_#{sym}?") { send("#{sym}_at").present? }
127
75
 
128
- #unless has_attribute?("#{sym}_at")
129
- define_method("#{sym}_at") { status_steps["#{sym}_at".to_sym] }
130
- define_method("#{sym}_at=") { |value| status_steps["#{sym}_at".to_sym] = value }
131
- #end
76
+ # was_approved?
77
+ define_method("was_#{sym}?") { send(sym_at).present? }
78
+
79
+ # approved_at
80
+ define_method(sym_at) { self[sym_at.to_s] || status_steps[sym_at] }
81
+
82
+ # approved_by_id
83
+ define_method(sym_by_id) { self[sym_by_id.to_s] || status_steps[sym_by_id] }
84
+
85
+ # approved_by_type
86
+ define_method(sym_by_type) { self[sym_by_type.to_s] || status_steps[sym_by_type] }
87
+
88
+ # approved_by
89
+ define_method(sym_by) do
90
+ user = (super() if attributes.key?(sym_by_id.to_s))
91
+
92
+ user ||= begin
93
+ id = status_steps[sym_by_id]
94
+ klass = status_steps[sym_by_type]
95
+
96
+ klass.constantize.find(id) if id.present? && klass.present?
97
+ end
98
+ end
99
+
100
+ # approved_at=
101
+ define_method("#{sym_at}=") do |value|
102
+ super(value) if attributes.key?(sym_at.to_s)
103
+ status_steps[sym_at] = value
104
+ end
105
+
106
+ # approved_by_id=
107
+ define_method("#{sym_by_id}=") do |value|
108
+ super(value) if attributes.key?(sym_by_id.to_s)
109
+ status_steps[sym_by_id] = value
110
+ end
111
+
112
+ # approved_by_type=
113
+ define_method("#{sym_by_type}=") do |value|
114
+ super(value) if attributes.key?(sym_by_type.to_s)
115
+ status_steps[sym_by_type] = value
116
+ end
132
117
 
133
- #unless has_attribute?("#{sym}_by_id")
134
- define_method("#{sym}_by") { acts_as_statused_by_user(sym) }
135
- define_method("#{sym}_by_id") { status_steps["#{sym}_by".to_sym] }
136
- define_method("#{sym}_by_id=") { |value| status_steps["#{sym}_by".to_sym] = value }
137
- #end
118
+ # approved_by=
119
+ define_method("#{sym_by}=") do |value|
120
+ super(value) if attributes.key?(sym_by_id.to_s)
121
+ status_steps[sym_by_id] = value&.id
122
+ status_steps[sym_by_type] = value&.class&.name
123
+ end
138
124
 
139
125
  # approved!
140
126
  define_method("#{sym}!") do |atts = {}|
@@ -146,17 +132,18 @@ module ActsAsStatused
146
132
  define_method("un#{sym}!") do
147
133
  self.status = nil if (status == sym.to_s)
148
134
 
149
- status_steps.delete("#{sym}_at".to_sym)
150
- status_steps.delete("#{sym}_by".to_sym)
151
-
152
- if respond_to?("#{sym}_at=") && respond_to?("#{sym}_at") && send("#{sym}_at").present?
135
+ if respond_to?("#{sym}_at") && send("#{sym}_at").present?
153
136
  self.send("#{sym}_at=", nil)
154
137
  end
155
138
 
156
- if respond_to?("#{sym}_by=") && respond_to?("#{sym}_by") && send("#{sym}_by").present?
139
+ if respond_to?("#{sym}_by") && send("#{sym}_by").present?
157
140
  self.send("#{sym}_by=", nil)
158
141
  end
159
142
 
143
+ status_steps.delete(sym_at)
144
+ status_steps.delete(sym_by_id)
145
+ status_steps.delete(sym_by_type)
146
+
160
147
  true
161
148
  end
162
149
 
@@ -167,16 +154,4 @@ module ActsAsStatused
167
154
  def acts_as_statused?; true; end
168
155
  end
169
156
 
170
- private
171
-
172
- def acts_as_statused_by_user(status)
173
- return nil if status_steps["#{status}_by".to_sym].blank?
174
-
175
- @acts_as_statused_by_users ||= begin
176
- User.where(id: status_steps.map { |k, v| v.presence if k.to_s.end_with?('_by') }.compact).all.inject({}) { |h, user| h[user.id] = user; h }
177
- end
178
-
179
- @acts_as_statused_by_users[status_steps["#{status}_by".to_sym]]
180
- end
181
-
182
157
  end
@@ -281,7 +281,8 @@ module Effective
281
281
 
282
282
  # Normalize the term.
283
283
  # If you pass an email attribute it can return nil so we return the full value
284
- term = Attribute.new(as).parse(value, name: name) || value
284
+ term = Attribute.new(as).parse(value, name: name)
285
+ term = value if term.nil?
285
286
 
286
287
  # If using the joined syntax from datatables
287
288
  joined = (sql_column.to_s.split('.').first.to_s.include?(relation.arel_table.name) == false)
@@ -305,7 +306,14 @@ module Effective
305
306
  end
306
307
  )
307
308
 
309
+ if as == :date
310
+ term = term.to_date
311
+ end_at = end_at.to_date
312
+ end
313
+
308
314
  relation.where("#{sql_column} >= ? AND #{sql_column} <= ?", term, end_at)
315
+ elsif value.respond_to?(:strftime) && operation == :eq
316
+ relation.where(attribute.matches(value))
309
317
  end
310
318
 
311
319
  when :effective_obfuscation
@@ -50,7 +50,6 @@ module EffectiveResources
50
50
  end
51
51
 
52
52
  CanCan::Ability.include(ActsAsArchived::CanCan)
53
- CanCan::Ability.include(ActsAsStatused::CanCan)
54
53
  end
55
54
  end
56
55
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.5.4'.freeze
2
+ VERSION = '2.6.0'.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: 2.5.4
4
+ version: 2.6.0
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: 2023-03-28 00:00:00.000000000 Z
11
+ date: 2023-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails