Module: EffectiveResources
- Includes:
- EffectiveGem
- Defined in:
- lib/effective_resources.rb,
lib/effective_resources/engine.rb,
lib/effective_resources/version.rb,
lib/generators/effective_resources/install_generator.rb
Defined Under Namespace
Modules: Generators
Classes: Engine
Constant Summary
collapse
- MAILER_SUBJECT_PROC =
Proc.new { |action, subject, resource, opts = {}| subject }
- VERSION =
'2.42.0'.freeze
EffectiveGem::EXCLUDED_GETTERS
Class Method Summary
collapse
-
.advance_date(date, business_days: 1, holidays: [:us, :observed]) ⇒ Object
-
.authorize!(controller, action, resource) ⇒ Object
-
.authorized?(controller, action, resource) ⇒ Boolean
-
.best(name) ⇒ Object
This looks up the best class give the name If the Tenant is present, use those classes first.
-
.business_day?(date, holidays: [:us, :observed]) ⇒ Boolean
-
.cache_key(*keys) ⇒ Object
-
.clone_blob(blob, options = {}) ⇒ Object
-
.config_keys ⇒ Object
-
.default_submits ⇒ Object
-
.deliver_method ⇒ Object
Mailer Settings These serve as the default mailer settings for all effective_* gems They can be overriden on a per-gem basis.
-
.et(resource, attribute = nil) ⇒ Object
-
.etd(resource, attribute = nil) ⇒ Object
-
.ets(resource, attribute = nil) ⇒ Object
-
.etsd(resource, attribute = nil) ⇒ Object
-
.falsey?(value) ⇒ Boolean
-
.mailer_admin ⇒ Object
-
.mailer_froms ⇒ Object
-
.mailer_layout ⇒ Object
-
.mailer_sender ⇒ Object
-
.mailer_subject ⇒ Object
-
.mailer_subject_prefix_hint ⇒ Object
This is used by the effective_email_templates form.
-
.masked_email(resource) ⇒ Object
-
.normalize_page(value) ⇒ Object
-
.parent_mailer_class ⇒ Object
-
.replace_nested_attributes(attributes) ⇒ Object
-
.send_error(exception, **tags) ⇒ Object
-
.serialize_with_coder? ⇒ Boolean
We use the newer syntax for serialize calls in rails 7.1 but it changes behaviour.
-
.transaction(resource = nil, &block) ⇒ Object
-
.truthy?(value) ⇒ Boolean
-
.validate_page!(value, collection_count:, per_page:) ⇒ Object
-
.with_resource_enumerator(&block) ⇒ Object
Used by streaming CSV export in datatables.
Class Method Details
.advance_date(date, business_days: 1, holidays: [:us, :observed]) ⇒ Object
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/effective_resources.rb', line 157
def self.advance_date(date, business_days: 1, holidays: [:us, :observed])
raise('business_days must be an integer <= 365') unless business_days.kind_of?(Integer) && business_days <= 365
business_days.times do
loop do
date = date + 1.day
break if business_day?(date, holidays: holidays)
end
end
date
end
|
.authorize!(controller, action, resource) ⇒ Object
40
41
42
|
# File 'lib/effective_resources.rb', line 40
def self.authorize!(controller, action, resource)
raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
end
|
.authorized?(controller, action, resource) ⇒ Boolean
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/effective_resources.rb', line 27
def self.authorized?(controller, action, resource)
@exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact
return !!authorization_method unless authorization_method.respond_to?(:call)
controller = controller.controller if controller.respond_to?(:controller)
begin
!!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
rescue *@exceptions
false
end
end
|
.best(name) ⇒ Object
This looks up the best class give the name
If the Tenant is present, use those classes first.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/effective_resources.rb', line 108
def self.best(name)
klass = if defined?(Tenant)
('::' + Tenant.module_name + '::' + name).safe_constantize ||
('::' + Tenant.module_name + '::Effective::' + name).safe_constantize
end
klass ||= begin
('::' + name).safe_constantize ||
('::Effective::' + name).safe_constantize
end
raise("unable to find best #{name}") if klass.blank?
klass
end
|
.business_day?(date, holidays: [:us, :observed]) ⇒ Boolean
170
171
172
173
|
# File 'lib/effective_resources.rb', line 170
def self.business_day?(date, holidays: [:us, :observed])
require 'holidays' unless defined?(Holidays)
date.wday != 0 && date.wday != 6 && Holidays.on(date, *holidays).blank?
end
|
.cache_key(*keys) ⇒ Object
274
275
276
277
278
279
280
|
# File 'lib/effective_resources.rb', line 274
def self.cache_key(*keys)
if defined?(Tenant)
[Tenant.current] + Array(keys).flatten
else
keys.flatten
end
end
|
.clone_blob(blob, options = {}) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# File 'lib/effective_resources.rb', line 176
def self.clone_blob(blob, options = {})
raise('expected an ActiveStorage::Blob') unless blob.kind_of?(ActiveStorage::Blob)
atts = {
filename: blob.filename,
byte_size: blob.byte_size,
checksum: blob.checksum,
content_type: blob.content_type,
metadata: blob.metadata,
}.merge(options)
service = blob.service
duplicate = ActiveStorage::Blob.create_before_direct_upload!(**atts)
case service.class.name
when 'ActiveStorage::Service::S3Service', 'ActiveStorage::Service::S3NoDeleteService'
bucket = service.bucket
object = bucket.object(blob.key)
object.copy_to(bucket.object(duplicate.key))
when 'ActiveStorage::Service::DiskService'
path = service.path_for(blob.key)
duplicate_path = service.path_for(duplicate.key)
FileUtils.mkdir_p(File.dirname(duplicate_path))
FileUtils.ln(path, duplicate_path) if File.exist?(path)
else
raise "unknown storage service #{service.class.name}"
end
duplicate
end
|
.config_keys ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/effective_resources.rb', line 12
def self.config_keys
[
:authorization_method, :default_submits,
:parent_mailer, :deliver_method, :mailer_layout, :mailer_sender, :mailer_froms, :mailer_admin, :mailer_subject,
:action_button_class, :new_action_button_class, :delete_action_button_class, :button_class
]
end
|
.default_submits ⇒ Object
44
45
46
|
# File 'lib/effective_resources.rb', line 44
def self.default_submits
(['Save', 'Continue', 'Add New'] & Array(config.default_submits)).inject({}) { |h, v| h[v] = true; h }
end
|
.deliver_method ⇒ Object
Mailer Settings
These serve as the default mailer settings for all effective_* gems
They can be overriden on a per-gem basis.
51
52
53
54
55
56
|
# File 'lib/effective_resources.rb', line 51
def self.deliver_method
return config[:deliver_method] if config[:deliver_method].present?
rails = Rails.application.config
(rails.respond_to?(:active_job) && rails.active_job.queue_adapter) ? :deliver_later : :deliver_now
end
|
.et(resource, attribute = nil) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/effective_resources.rb', line 227
def self.et(resource, attribute = nil)
if resource.respond_to?(:datatable_name)
resource.datatable_name
elsif resource.respond_to?(:model_name) == false value = I18n.t(resource)
raise StandardError.new("Missing translation: #{resource}") if value.include?(resource) || value.include?("Missing translation:")
value
elsif attribute.blank?
resource.model_name.human
elsif resource.respond_to?(:human_attribute_name)
resource.human_attribute_name(attribute)
else
resource.class.human_attribute_name(attribute)
end
end
|
.etd(resource, attribute = nil) ⇒ Object
243
244
245
|
# File 'lib/effective_resources.rb', line 243
def self.etd(resource, attribute = nil)
et(resource, attribute).downcase
end
|
.ets(resource, attribute = nil) ⇒ Object
247
248
249
|
# File 'lib/effective_resources.rb', line 247
def self.ets(resource, attribute = nil)
et(resource, attribute).pluralize
end
|
.etsd(resource, attribute = nil) ⇒ Object
251
252
253
|
# File 'lib/effective_resources.rb', line 251
def self.etsd(resource, attribute = nil)
et(resource, attribute).pluralize.downcase
end
|
.falsey?(value) ⇒ Boolean
152
153
154
155
|
# File 'lib/effective_resources.rb', line 152
def self.falsey?(value)
return true if (value == false || value == 'false')
!truthy?(value)
end
|
.mailer_admin ⇒ Object
71
72
73
|
# File 'lib/effective_resources.rb', line 71
def self.mailer_admin
config[:mailer_admin] || raise('effective resources mailer_admin missing. Add it to config/initializers/effective_resources.rb')
end
|
.mailer_froms ⇒ Object
79
80
81
|
# File 'lib/effective_resources.rb', line 79
def self.mailer_froms
config[:mailer_froms].presence || [mailer_sender]
end
|
.mailer_layout ⇒ Object
63
64
65
|
# File 'lib/effective_resources.rb', line 63
def self.mailer_layout
config[:mailer_layout] || 'mailer'
end
|
.mailer_sender ⇒ Object
75
76
77
|
# File 'lib/effective_resources.rb', line 75
def self.mailer_sender
config[:mailer_sender] || raise('effective resources mailer_sender missing. Add it to config/initializers/effective_resources.rb')
end
|
.mailer_subject ⇒ Object
67
68
69
|
# File 'lib/effective_resources.rb', line 67
def self.mailer_subject
config[:mailer_subject] || MAILER_SUBJECT_PROC
end
|
.mailer_subject_prefix_hint ⇒ Object
This is used by the effective_email_templates form
84
85
86
|
# File 'lib/effective_resources.rb', line 84
def self.mailer_subject_prefix_hint
instance_exec(:mailer_subject_blank_prefix, nil, nil, nil, &mailer_subject) if mailer_subject.respond_to?(:call)
end
|
.masked_email(resource) ⇒ Object
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/effective_resources.rb', line 282
def self.masked_email(resource)
public_email = resource.try(:public_email)
return public_email if public_email.present?
email = resource.email
return email if email.blank?
local, domain = email.split('@')
masked_local = local[0..1] + '*' * (local.length - 2)
masked_domain = domain.split('.').first[0..1] + '*' * (domain.split('.').first.length - 2) + '.' + domain.split('.').last
"#{masked_local}@#{masked_domain}"
end
|
.normalize_page(value) ⇒ Object
90
91
92
93
94
|
# File 'lib/effective_resources.rb', line 90
def self.normalize_page(value)
return 1 if value.nil? || value == ''
return value if value.is_a?(Integer) && value.positive?
return value.to_i if value.is_a?(String) && value.match?(/\A[1-9]\d*\z/)
end
|
.parent_mailer_class ⇒ Object
58
59
60
61
|
# File 'lib/effective_resources.rb', line 58
def self.parent_mailer_class
return config[:parent_mailer].constantize if config[:parent_mailer].present?
'::ApplicationMailer'.safe_constantize || 'ActionMailer::Base'.constantize
end
|
.replace_nested_attributes(attributes) ⇒ Object
220
221
222
223
224
|
# File 'lib/effective_resources.rb', line 220
def self.replace_nested_attributes(attributes)
attributes.reject { |k, values| truthy?(values[:_destroy]) }.inject({}) do |h, (key, values)|
h[key] = values.reject { |k, v| k == 'id' || k == '_destroy' }; h
end
end
|
.send_error(exception, **tags) ⇒ Object
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/effective_resources.rb', line 255
def self.send_error(exception, **tags)
if defined?(Tenant)
tags = { tenant: Tenant.current || 'none' }.merge(tags)
end
tags = tags.transform_values(&:to_s)
if defined?(ExceptionNotifier)
ExceptionNotifier.notify_exception(exception, data: tags)
end
if defined?(Appsignal)
Appsignal.send_error(exception) do
Appsignal.add_tags(tags)
Appsignal.add_custom_data(tags)
end
end
end
|
.serialize_with_coder? ⇒ Boolean
We use the newer syntax for serialize calls in rails 7.1 but it changes behaviour
23
24
25
|
# File 'lib/effective_resources.rb', line 23
def self.serialize_with_coder?
Gem::Version.new(Gem.loaded_specs['rails'].version.to_s) >= Gem::Version.new('7.1')
end
|
.transaction(resource = nil, &block) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/effective_resources.rb', line 207
def self.transaction(resource = nil, &block)
raise('expected a block') unless block_given?
if resource.respond_to?(:transaction)
resource.transaction { yield }
elsif resource.class.respond_to?(:transaction)
resource.class.transaction { yield }
else
ActiveRecord::Base.transaction { yield }
end
end
|
.truthy?(value) ⇒ Boolean
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/effective_resources.rb', line 139
def self.truthy?(value)
return true if (value == true || value == 'true')
return true if value.kind_of?(String) && ['yes', 'true'].include?(value.downcase)
return false if value.kind_of?(String) && ['no', 'false'].include?(value.downcase)
if defined?(::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES) ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
else
::ActiveRecord::Type::Boolean.new.cast(value) || value.to_s.downcase == 'yes'
end
end
|
.validate_page!(value, collection_count:, per_page:) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/effective_resources.rb', line 96
def self.validate_page!(value, collection_count:, per_page:)
page = normalize_page(value)
raise ActiveRecord::RecordNotFound, "Page #{value.inspect} is invalid" unless page
last = [(collection_count.to_f / per_page).ceil, 1].max
raise ActiveRecord::RecordNotFound, "Page #{page} does not exist" if page > last
page
end
|
.with_resource_enumerator(&block) ⇒ Object
Used by streaming CSV export in datatables
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/effective_resources.rb', line 125
def self.with_resource_enumerator(&block)
raise('expected a block') unless block_given?
tenant = Tenant.current if defined?(Tenant)
if tenant
Enumerator.new do |enumerator|
Tenant.as(tenant) { yield(enumerator) }
end
else
Enumerator.new { |enumerator| yield(enumerator) }
end
end
|