effective_resources 1.6.6 → 1.7.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: 323b70f4a0eb470c2e96edf3651c83493763ad203659f03bb25832cf487812a1
4
- data.tar.gz: f997c5dcd62f4de1d49065c5fb3d5d882ea3eca30c47fe518bb6642dbd43e65a
3
+ metadata.gz: 9c2c2b66908d01a2f641dc34444882413040d6d9b4c403d2a3ec4041abed1822
4
+ data.tar.gz: 20497b49e1408e7c7128ad8f9a4ed601f76842d74f0fd165aa24645f9310a39d
5
5
  SHA512:
6
- metadata.gz: 4d78587039acac7fa236913c588946277481a5df90933141db13a7d447491233452f8dd6c3c96ebf7bff8dfd95ba217e56c65e826128d9674532331d6971422c
7
- data.tar.gz: daff8c0e7009dd67297f2e62a90744b7dcc67008f107543b53c53b24476ca8534284202aae98b0579e394fddbdbb498925b0294f47c95e70f9bbf8b551cab4a3
6
+ metadata.gz: 2c39301ac8c30eedab982905acfdc7bd0077fef8463d07f7bcf68f0861f3962b40c43d7fd7a85cd72b1da6a326d5d9ed3eeaa3de083a5153e289cf52c6b2723a
7
+ data.tar.gz: b3102a3d1e0d809350a41d60f9c517c2f48357838d58dc75e2c32a98686fe5bb5aec401a4e0f63e7cb40b542d9ffa255ccdd72687641d54450d2449e4481e963
@@ -11,13 +11,14 @@ module Effective
11
11
 
12
12
  included do
13
13
  define_actions_from_routes
14
- define_permitted_params_from_model
15
14
  define_callbacks :resource_render, :resource_before_save, :resource_after_save, :resource_after_commit, :resource_error
16
15
  end
17
16
 
18
17
  module ClassMethods
19
18
  include Effective::CrudController::Dsl
20
19
 
20
+ # This is used to define_actions_from_routes and for the buttons/submits/ons
21
+ # It doesn't really work with the resource_scope correctly but the routes are important here
21
22
  def effective_resource
22
23
  @_effective_resource ||= Effective::Resource.new(controller_path)
23
24
  end
@@ -32,17 +33,6 @@ module Effective
32
33
  define_method(action) { collection_action(action) }
33
34
  end
34
35
  end
35
-
36
- def define_permitted_params_from_model
37
- if effective_resource.model.present?
38
- define_method(:effective_resource_permitted_params) { resource_permitted_params } # save.rb
39
- end
40
-
41
- if effective_resource.active_model?
42
- define_method(:effective_resource_permitted_params) { resource_active_model_permitted_params } # save.rb
43
- end
44
- end
45
-
46
36
  end
47
37
 
48
38
  def resource # @thing
@@ -62,11 +52,29 @@ module Effective
62
52
  end
63
53
 
64
54
  def effective_resource
65
- self.class.effective_resource
55
+ @_effective_resource ||= begin
56
+ relation = instance_exec(&resource_scope_relation) if respond_to?(:resource_scope_relation)
57
+
58
+ if respond_to?(:resource_scope_relation) && !relation.kind_of?(ActiveRecord::Relation)
59
+ raise('resource_scope must return an ActiveRecord::Relation')
60
+ end
61
+
62
+ resource = Effective::Resource.new(controller_path, relation: relation)
63
+
64
+ unless resource.relation.kind_of?(ActiveRecord::Relation) || effective_resource.active_model?
65
+ raise("unable to build resource_scope for #{resource.klass || 'unknown klass'}. Please name your controller to match an existing model, or manually define a resource_scope.")
66
+ end
67
+
68
+ resource
69
+ end
66
70
  end
67
71
 
68
72
  private
69
73
 
74
+ def resource_scope
75
+ effective_resource.relation
76
+ end
77
+
70
78
  def resource_name # 'thing'
71
79
  effective_resource.name
72
80
  end
@@ -83,30 +91,6 @@ module Effective
83
91
  effective_resource.plural_name
84
92
  end
85
93
 
86
- # Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
87
- def resource_scope # Thing
88
- @_effective_resource_relation ||= (
89
- relation = case @_effective_resource_scope # If this was initialized by the resource_scope before_action
90
- when ActiveRecord::Relation
91
- @_effective_resource_scope
92
- when Hash
93
- effective_resource.klass.where(@_effective_resource_scope)
94
- when Symbol
95
- effective_resource.klass.send(@_effective_resource_scope)
96
- when nil
97
- effective_resource.klass.respond_to?(:all) ? effective_resource.klass.all : effective_resource.klass
98
- else
99
- raise "expected resource_scope method to return an ActiveRecord::Relation or Hash"
100
- end
101
-
102
- unless relation.kind_of?(ActiveRecord::Relation) || effective_resource.active_model?
103
- raise("unable to build resource_scope for #{effective_resource.klass || 'unknown klass'}. Please name your controller to match an existing model, or manually define a resource_scope.")
104
- end
105
-
106
- relation
107
- )
108
- end
109
-
110
94
  def resource_datatable_attributes
111
95
  resource_scope.where_values_hash.symbolize_keys
112
96
  end
@@ -128,7 +112,16 @@ module Effective
128
112
  end
129
113
 
130
114
  def resource_params_method_name
131
- ["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params', 'effective_resource_permitted_params', ('resource_permitted_params' if effective_resource.model.present?)].compact.find { |name| respond_to?(name, true) } || 'params'
115
+ ['permitted_params', "#{resource_name}_params", "#{resource_plural_name}_params"].each do |name|
116
+ return name if respond_to?(name, true)
117
+ end
118
+
119
+ # Built in ones
120
+ return 'resource_permitted_params' if effective_resource.model.present?
121
+ return 'resource_active_model_permitted_params' if effective_resource.active_model?
122
+
123
+ # Fallback
124
+ 'params'
132
125
  end
133
126
 
134
127
  end
@@ -79,9 +79,12 @@ module Effective
79
79
  def resource_scope(obj = nil, opts = {}, &block)
80
80
  raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
81
81
 
82
- instance_exec do
83
- before_action(opts) { @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj)) }
82
+ if block_given?
83
+ define_method(:resource_scope_relation) { return block }
84
+ else
85
+ define_method(:resource_scope_relation) { return obj }
84
86
  end
87
+
85
88
  end
86
89
 
87
90
  end
@@ -8,6 +8,7 @@ module Effective
8
8
  include Effective::WizardController::Actions
9
9
  include Effective::WizardController::BeforeActions
10
10
  include Effective::WizardController::Save
11
+ include Effective::WizardController::WickedOverrides
11
12
 
12
13
  included do
13
14
  raise("please install gem 'wicked' to use Effective::WizardController") unless defined?(Wicked)
@@ -54,9 +55,11 @@ module Effective
54
55
  effective_resource.klass.const_get(:WIZARD_STEPS).keys
55
56
  end
56
57
 
58
+ # It could be :new, :start
59
+ # Or resource, step
57
60
  def resource_wizard_path(resource, step)
58
- path_helper = effective_resource.action_path_helper(:show).to_s.sub('_path', '_build_path')
59
- public_send(path_helper, resource, step)
61
+ param = (resource.respond_to?(:to_param) ? resource.to_param : resource)
62
+ wizard_path(step, resource_name_id => param)
60
63
  end
61
64
 
62
65
  end
@@ -0,0 +1,23 @@
1
+ module Effective
2
+ module WizardController
3
+ module WickedOverrides
4
+
5
+ # Changes made here to work inside an effective rails engine
6
+ #
7
+ # https://github.com/zombocom/wicked/blob/main/lib/wicked/controller/concerns/path.rb
8
+ # https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb#L180
9
+ def wizard_path(goto_step = nil, options = {})
10
+ options = options.respond_to?(:to_h) ? options.to_h : options
11
+ options = { :controller => wicked_controller,
12
+ :action => 'show',
13
+ :id => goto_step || params[:id],
14
+ :only_path => true
15
+ }.merge options
16
+
17
+ merged_url_options = options.reverse_merge!(url_options)
18
+ effective_resource.url_helpers.url_for(merged_url_options)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -15,8 +15,8 @@ module Effective
15
15
  include Effective::Resources::Sql
16
16
 
17
17
  # post, Post, Admin::Post, admin::Post, admin/posts, admin/post, admin/effective::post
18
- def initialize(input, namespace: nil, &block)
19
- _initialize_input(input, namespace: namespace)
18
+ def initialize(input, namespace: nil, relation: nil, &block)
19
+ _initialize_input(input, namespace: namespace, relation: relation)
20
20
  _initialize_model(&block) if block_given?
21
21
  self
22
22
  end
@@ -12,8 +12,8 @@ module Effective
12
12
  def routes
13
13
  @routes ||= (
14
14
  matches = [
15
- [namespace, plural_name].compact.join('/'),
16
- [namespace, name].compact.join('/')
15
+ [namespace, route_name.pluralize].compact.join('/'),
16
+ [namespace, route_name].compact.join('/'),
17
17
  ]
18
18
 
19
19
  # Check main Rails app
@@ -21,9 +21,25 @@ module Effective
21
21
  (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
22
22
  end
23
23
 
24
+ if routes.blank?
25
+ matches = [
26
+ [namespace, plural_name].compact.join('/'),
27
+ [namespace, name].compact.join('/')
28
+ ]
29
+
30
+ # Check main Rails app
31
+ routes = Rails.application.routes.routes.select do |route|
32
+ (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
33
+ end
34
+ end
35
+
24
36
  # Check engine routes
25
37
  if routes.blank?
26
38
  matches = [
39
+ [namespace, route_name.pluralize].compact.join('/'),
40
+ [namespace, route_name].compact.join('/'),
41
+ ['effective', namespace, route_name.pluralize].compact.join('/'),
42
+ ['effective', namespace, route_name].compact.join('/'),
27
43
  [namespace, plural_name].compact.join('/'),
28
44
  [namespace, name].compact.join('/'),
29
45
  ['effective', namespace, plural_name].compact.join('/'),
@@ -35,7 +51,11 @@ module Effective
35
51
  (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
36
52
  end
37
53
 
38
- break if routes.present?
54
+ if routes.present?
55
+ @routes_app = engine
56
+ break
57
+ end
58
+
39
59
  end
40
60
  end
41
61
 
@@ -43,6 +63,14 @@ module Effective
43
63
  )
44
64
  end
45
65
 
66
+ def routes_app
67
+ @routes_app if routes.present?
68
+ end
69
+
70
+ def url_helpers
71
+ (routes_app || Rails.application).routes.url_helpers
72
+ end
73
+
46
74
  # Effective::Resource.new('admin/posts').action_path_helper(:edit) => 'edit_admin_posts_path'
47
75
  # This will return empty for create, update and destroy
48
76
  def action_path_helper(action)
@@ -4,9 +4,9 @@ module Effective
4
4
 
5
5
  private
6
6
 
7
- def _initialize_input(input, namespace: nil)
7
+ def _initialize_input(input, namespace: nil, relation: nil)
8
8
  @initialized_name = input
9
- @model_klass = _klass_by_input(input)
9
+ @model_klass = (relation ? _klass_by_input(relation) : _klass_by_input(input))
10
10
 
11
11
  # Consider namespaces
12
12
  if namespace
@@ -18,20 +18,24 @@ module Effective
18
18
  end
19
19
 
20
20
  # Consider relation
21
+ if relation.kind_of?(ActiveRecord::Relation)
22
+ @relation ||= relation
23
+ end
24
+
21
25
  if input.kind_of?(ActiveRecord::Relation)
22
26
  @relation ||= input
23
27
  end
24
28
 
25
29
  if input.kind_of?(ActiveRecord::Reflection::MacroReflection) && input.scope
26
- @relation ||= klass.where(nil).merge(input.scope)
30
+ @relation ||= @model_klass.where(nil).merge(input.scope)
27
31
  end
28
32
 
29
33
  # Consider instance
30
- if klass && input.instance_of?(klass)
34
+ if @model_klass && input.instance_of?(@model_klass)
31
35
  @instance ||= input
32
36
  end
33
37
 
34
- if klass && input.kind_of?(Array) && input.last.instance_of?(klass)
38
+ if @model_klass && input.kind_of?(Array) && input.last.instance_of?(@model_klass)
35
39
  @instance ||= input.last
36
40
  end
37
41
  end
@@ -19,6 +19,10 @@ module Effective
19
19
  @initialized_name
20
20
  end
21
21
 
22
+ 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
+ end
25
+
22
26
  def class_name # 'Effective::Post'
23
27
  @model_klass ? @model_klass.name : name.classify
24
28
  end
@@ -44,11 +48,11 @@ module Effective
44
48
  end
45
49
 
46
50
  def human_name
47
- class_name.gsub('::', ' ').underscore.gsub('_', ' ')
51
+ name.gsub('::', ' ').underscore.gsub('_', ' ')
48
52
  end
49
53
 
50
54
  def human_plural_name
51
- class_name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
55
+ name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
52
56
  end
53
57
 
54
58
  end
@@ -4,8 +4,10 @@ module Effective
4
4
  TARGET_LIST_LIMIT = 1500
5
5
  TARGET_KEYS_LIMIT = 30000
6
6
 
7
+ # This could be active_model? in which we just return the klass itself here
8
+ # This value ends up being crud_controller resource_scope()
7
9
  def relation
8
- @relation ||= klass.where(nil)
10
+ @relation ||= (klass.respond_to?(:where) ? klass.where(nil) : klass)
9
11
  end
10
12
 
11
13
  # When Effective::Resource is initialized with an ActiveRecord relation, the following
@@ -51,9 +51,11 @@ module EffectiveResources
51
51
  # Register the acts_as_archived routes concern
52
52
  # resources :things, concerns: :acts_as_archived
53
53
  initializer 'effective_resources.routes_concern' do |app|
54
- ActiveSupport.on_load :action_controller_base do
55
- ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
56
- end
54
+ ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
55
+
56
+ # Doesn't seem to work with the on_load in rails 6.0
57
+ #ActiveSupport.on_load :action_controller_base do
58
+ #end
57
59
  end
58
60
 
59
61
  # Register the flash_messages concern so that it can be called in ActionController
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.6.6'.freeze
2
+ VERSION = '1.7.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: 1.6.6
4
+ version: 1.7.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: 2020-12-31 00:00:00.000000000 Z
11
+ date: 2021-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -132,6 +132,7 @@ files:
132
132
  - app/controllers/concerns/effective/wizard_controller/actions.rb
133
133
  - app/controllers/concerns/effective/wizard_controller/before_actions.rb
134
134
  - app/controllers/concerns/effective/wizard_controller/save.rb
135
+ - app/controllers/concerns/effective/wizard_controller/wicked_overrides.rb
135
136
  - app/helpers/effective_resources_helper.rb
136
137
  - app/helpers/effective_resources_private_helper.rb
137
138
  - app/helpers/effective_resources_wizard_helper.rb