effective_resources 1.7.7 → 1.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43a2e065a385451ee58c4cf054e18ffb3b2d67618aef4c6e3a417b221b7a6839
4
- data.tar.gz: fa379b94d613fa510aa5261c44877a344dfff0ac6de0202a64c90d692b9157aa
3
+ metadata.gz: 7a4d2d6652da6f85e884ceb383ff012fda17863ad4b84c7cfaf5fec76a8632af
4
+ data.tar.gz: 155b5a31cc14908ea944978d940d550227bcb823e14bbc42e6f34108ece06c35
5
5
  SHA512:
6
- metadata.gz: a0ed3d10e90f30f5a7b4dd9d837fe21ca6d07c557850d1931ec97c839fbb39a78e11fa795331af247cc5fa929fccdf5aec3f6f05dfd8b68d35e3e0682de0c732
7
- data.tar.gz: 53d62815fc3e7fa7718fa1094b7fcb721d772631b1ec1899a9ad7a4f601705091b9efba194b59c4ebead8f0a260b9791b0b83cce9d4962a523361abe46697ef0
6
+ metadata.gz: 4227d0379585019dafae032ac3300cc98ceed87f81bfcb45a35ab9b10c5e15f18dd7e6420b1973bef3e1567dd8ac71d78049b6ec6711153899568ed0d3398fc3
7
+ data.tar.gz: 700279f4126eefafe9b14d06b4dea16b9cf49c1f2b21fd472a17a80e3e2863d2f5d6318f481d3f862653d95f753960f5a0fbc79802301e3879422cec89866df8
@@ -53,7 +53,7 @@ module Effective
53
53
 
54
54
  respond_to do |format|
55
55
  format.html { }
56
- format.js { render('new.js') }
56
+ format.js { render('new', formats: :js) }
57
57
  end
58
58
  end
59
59
 
@@ -95,7 +95,7 @@ module Effective
95
95
 
96
96
  respond_to do |format|
97
97
  format.html { }
98
- format.js { render('show.js') }
98
+ format.js { render('show', formats: :js) }
99
99
  end
100
100
 
101
101
  end
@@ -112,7 +112,7 @@ module Effective
112
112
 
113
113
  respond_to do |format|
114
114
  format.html { }
115
- format.js { render('edit.js') }
115
+ format.js { render('edit', formats: :js) }
116
116
  end
117
117
 
118
118
  end
@@ -176,7 +176,10 @@ module Effective
176
176
 
177
177
  respond_to do |format|
178
178
  format.html { }
179
- format.js { render(template_present?(action) ? action : 'member_action.js', locals: { action: action }) }
179
+ format.js do
180
+ template = template_present?(action) ? action : 'member_action'
181
+ render(template, formats: :js, locals: { action: action })
182
+ end
180
183
  end
181
184
 
182
185
  return
@@ -76,6 +76,7 @@ module Effective
76
76
  def resource_flash(status, resource, action, e: nil)
77
77
  submit = commit_action(action)
78
78
  message = submit[status].respond_to?(:call) ? instance_exec(&submit[status]) : submit[status]
79
+
79
80
  return message.gsub('@resource', resource.to_s) if message.present?
80
81
  return nil if message.blank? && submit.key?(status)
81
82
 
@@ -93,6 +94,9 @@ module Effective
93
94
 
94
95
  # Should return a new resource based on the passed one
95
96
  def duplicate_resource(resource)
97
+ return resource.duplicate if resource.respond_to?(:duplicate)
98
+ return resource.duplicate! if resource.respond_to?(:duplicate!)
99
+ return resource.deep_dup if resource.respond_to?(:deep_dup)
96
100
  resource.dup
97
101
  end
98
102
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Effective
2
4
  module FlashMessages
3
5
  extend ActiveSupport::Concern
@@ -6,7 +8,11 @@ module Effective
6
8
  def flash_success(resource, action = nil, name: nil)
7
9
  raise 'expected an ActiveRecord resource' unless (name || resource.class.respond_to?(:model_name))
8
10
 
9
- "Successfully #{action_verb(action)} #{name || resource}".html_safe
11
+ name ||= begin
12
+ resource.destroyed? ? resource.class.model_name.to_s.downcase.split('::').last : resource.to_s.presence
13
+ end
14
+
15
+ "Successfully #{action_verb(action)} #{name || 'resource'}".html_safe
10
16
  end
11
17
 
12
18
  # flash.now[:danger] = flash_danger(@post)
@@ -18,7 +24,9 @@ module Effective
18
24
 
19
25
  messages = flash_errors(resource, e: e)
20
26
 
21
- name ||= resource.to_s.presence
27
+ name ||= begin
28
+ resource.destroyed? ? resource.class.model_name.to_s.downcase.split('::').last : resource.to_s.presence
29
+ end
22
30
 
23
31
  ["Unable to #{action}", (" #{name}" if name), (": #{messages}" if messages)].compact.join.html_safe
24
32
  end
@@ -27,7 +35,10 @@ module Effective
27
35
  def flash_errors(resource, e: nil)
28
36
  raise 'expected an ActiveRecord resource' unless resource.respond_to?(:errors)
29
37
 
30
- messages = resource.errors.map do |attribute, message|
38
+ messages = resource.errors.map do |error|
39
+ attribute = error.respond_to?(:attribute) ? error.attribute : error.first
40
+ message = error.respond_to?(:message) ? error.message : error.last
41
+
31
42
  if message[0] == message[0].upcase # If the error begins with a capital letter
32
43
  message
33
44
  elsif attribute == :base
@@ -52,6 +63,8 @@ module Effective
52
63
  'deleted'
53
64
  elsif word == 'undo'
54
65
  'undid'
66
+ elsif word == 'run'
67
+ 'ran'
55
68
  elsif word.end_with?('e')
56
69
  action.sub(word, word + 'd')
57
70
  elsif ['a', 'i', 'o', 'u'].include?(word[-1])
@@ -151,16 +151,24 @@ module EffectiveResourcesHelper
151
151
  atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
152
152
 
153
153
  if lookup_context.template_exists?("form_#{action}", controller._prefixes, :partial)
154
- render "form_#{action}", atts
155
- elsif lookup_context.template_exists?('form', controller._prefixes, :partial)
156
- render 'form', atts
157
- elsif lookup_context.template_exists?('form', effective_resource.plural_name, :partial)
158
- render "#{effective_resource.plural_name}/form", atts
159
- elsif lookup_context.template_exists?('form', effective_resource.name, :partial)
160
- render "#{effective_resource.name}/form", atts
161
- else
162
- render 'form', atts # Will raise the regular error
154
+ return render("form_#{action}", atts)
155
+ end
156
+
157
+ if lookup_context.template_exists?('form', controller._prefixes, :partial)
158
+ return render('form', atts)
159
+ end
160
+
161
+ effective_resource.view_paths.each do |view_path|
162
+ if lookup_context.template_exists?("form_#{action}", [view_path], :partial)
163
+ return render(view_path + '/' + "form_#{action}", atts)
164
+ end
165
+
166
+ if lookup_context.template_exists?('form', [view_path], :partial)
167
+ return render(view_path + '/' + 'form', atts)
168
+ end
163
169
  end
170
+
171
+ render('form', atts) # Will raise the regular error
164
172
  end
165
173
 
166
174
  # Similar to render_resource_form
@@ -177,14 +185,16 @@ module EffectiveResourcesHelper
177
185
  atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
178
186
 
179
187
  if lookup_context.template_exists?(effective_resource.name, controller._prefixes, :partial)
180
- render(effective_resource.name, atts)
181
- elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.plural_name], :partial)
182
- render(effective_resource.plural_name + '/' + effective_resource.name, atts)
183
- elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.name], :partial)
184
- render(effective_resource.name + '/' + effective_resource.name, atts)
185
- else
186
- render(resource, atts) # Will raise the regular error
188
+ return render(effective_resource.name, atts)
187
189
  end
190
+
191
+ effective_resource.view_paths.each do |view_path|
192
+ if lookup_context.template_exists?(effective_resource.name, [view_path], :partial)
193
+ return render(view_path + '/' + effective_resource.name, atts)
194
+ end
195
+ end
196
+
197
+ render(resource, atts) # Will raise the regular error
188
198
  end
189
199
 
190
200
  # Tableize attributes
@@ -16,7 +16,9 @@ module ActsAsSlugged
16
16
  included do
17
17
  extend FinderMethods
18
18
 
19
- before_validation { self.slug ||= build_slug }
19
+ before_validation do
20
+ assign_attributes(slug: build_slug) if slug.blank?
21
+ end
20
22
 
21
23
  validates :slug,
22
24
  presence: true, uniqueness: true, exclusion: { in: excluded_slugs }, length: { maximum: 255 },
@@ -44,7 +46,7 @@ module ActsAsSlugged
44
46
 
45
47
  # Instance Methods
46
48
  def build_slug
47
- slug = self.to_s.parameterize.downcase[0, 250]
49
+ slug = to_s.parameterize.downcase[0, 250]
48
50
 
49
51
  if self.class.excluded_slugs.include?(slug)
50
52
  slug = "#{slug}-#{self.class.name.demodulize.parameterize}"
@@ -9,7 +9,7 @@ module Effective
9
9
 
10
10
  # This will have been set by init from crud_controller, or from a class and namespace
11
11
  def controller_path
12
- @controller_path ||= ([namespace, plural_name].compact * '/')
12
+ @controller_path ||= route_name #[namespace, plural_name].compact * '/')
13
13
  end
14
14
 
15
15
  def routes
@@ -20,7 +20,7 @@ module Effective
20
20
  # Check from controller_path. This is generally correct.
21
21
  engines.each do |engine|
22
22
  routes = engine.routes.routes.select do |route|
23
- controller_path == route.defaults[:controller] && !route.name.to_s.end_with?('root')
23
+ controller_path == route.defaults[:controller] && !(route.name || '').end_with?('root')
24
24
  end
25
25
 
26
26
  if routes.present?
@@ -29,20 +29,11 @@ module Effective
29
29
  end
30
30
 
31
31
  if routes.blank?
32
- matches = [
33
- [namespace, route_name.pluralize].compact.join('/'),
34
- [namespace, route_name].compact.join('/'),
35
- ['effective', namespace, route_name.pluralize].compact.join('/'),
36
- ['effective', namespace, route_name].compact.join('/'),
37
- [namespace, plural_name].compact.join('/'),
38
- [namespace, name].compact.join('/'),
39
- ['effective', namespace, plural_name].compact.join('/'),
40
- ['effective', namespace, name].compact.join('/')
41
- ]
32
+ matches = route_name_fallbacks()
42
33
 
43
34
  engines.each do |engine|
44
35
  routes = engine.routes.routes.select do |route|
45
- (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
36
+ (matches & [route.defaults[:controller]]).present? && !(route.name || '').end_with?('root')
46
37
  end
47
38
 
48
39
  if routes.present?
@@ -7,6 +7,7 @@ module Effective
7
7
  case (type || sql_type(name))
8
8
  when :belongs_to
9
9
  { as: :select }.merge(search_form_field_collection(belongs_to(name)))
10
+
10
11
  when :belongs_to_polymorphic
11
12
  constant_pluralized = name.to_s.upcase
12
13
  constant = name.to_s.pluralize.upcase
@@ -18,7 +19,7 @@ module Effective
18
19
  collection ||= (klass.const_get(constant_pluralized) rescue nil) if defined?("#{klass.name}::#{constant_pluralized}")
19
20
  end
20
21
 
21
- { as: :select, polymorphic: true, collection: collection }.compact
22
+ { as: :select, polymorphic: true, collection: (collection || []) }.compact
22
23
  when :has_and_belongs_to_many
23
24
  { as: :select }.merge(search_form_field_collection(has_and_belongs_to_many(name)))
24
25
  when :has_many
@@ -19,8 +19,27 @@ module Effective
19
19
  @initialized_name
20
20
  end
21
21
 
22
+ # There could be a few, this is the best guess.
22
23
  def route_name # 'post' initialized from the controller_path/initialized_name and not the class
23
- @route_name ||= (initialized_name.to_s.split(SPLIT).last || '').singularize.underscore
24
+ names = class_name.split('::')
25
+
26
+ if names.length > 1
27
+ Array(names[0]) + namespaces + Array(names[1..-1])
28
+ else
29
+ namespaces + names
30
+ end.compact.map(&:downcase).join('/').pluralize
31
+ end
32
+
33
+ def route_name_fallbacks
34
+ mod = class_name.split('::').first.downcase
35
+
36
+ matches = [
37
+ route_name.singularize,
38
+ [*namespace, plural_name].join('/'),
39
+ [*namespace, name].join('/'),
40
+ [mod, *namespace, plural_name].join('/'),
41
+ [mod, *namespace, name].join('/')
42
+ ]
24
43
  end
25
44
 
26
45
  def class_name # 'Effective::Post'
@@ -22,6 +22,22 @@ module Effective
22
22
  File.join('app/views', plural_name, "#{'_' if partial}#{action}.html.haml")
23
23
  end
24
24
 
25
+ # Used by render_resource_partial and render_resource_form to guess the view path
26
+ def view_paths
27
+ mod = class_name.split('::').first.downcase
28
+
29
+ [
30
+ [mod, *namespace, plural_name].join('/'),
31
+ [mod, *namespace, name].join('/'),
32
+ [*namespace, mod, plural_name].join('/'),
33
+ [*namespace, mod, name].join('/'),
34
+ [mod, plural_name].join('/'),
35
+ [mod, name].join('/'),
36
+ [*namespace, plural_name].join('/'),
37
+ [*namespace, name].join('/')
38
+ ]
39
+ end
40
+
25
41
  end
26
42
  end
27
43
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.7.7'.freeze
2
+ VERSION = '1.7.8'.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.7.7
4
+ version: 1.7.8
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: 2021-02-18 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails