effective_resources 1.0.3 → 1.0.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: 41ccd958aa2d034e20655b10bfe8a830bf162dad
4
- data.tar.gz: d56e278ea8440a596378c2e5f943c32cc5a4c1dc
3
+ metadata.gz: 12e0851418c01ba4290cee861341751689d05963
4
+ data.tar.gz: a9ee4c64ac47d1abac493d72eece30383d77fad4
5
5
  SHA512:
6
- metadata.gz: c2bd71876feb1b9437d2d2026f44e69f3aa93ff3a17a1b2c9e22b3c9c794a7128531321787e4fc6b5f3061bacb89e1186de8f2852dea56a47628da31dde44499
7
- data.tar.gz: 74dc40f978cb4af876f258a5c1acd32c2865c65b030a8516b40d620a4981ce6544016bb75e40833059ba82f9c6212c7ba8baf12f8bbce6268ccb465ef0d3d6a3
6
+ metadata.gz: d27c675dc474aaf51403453d98ce32b83264cfe7437d4178f58fc45dafbeb20b454d6450235e775fd9bee049eb94db8444ab8979e88752e26f68a9ef7a1e224b
7
+ data.tar.gz: 0a749056b57fdd205fba43497bff68bbfd61d0ed61617efea637902453b9974886064345a01e50cb388d375bd82abf2bf69769e48bad0cbdd37e5c684048b58b
@@ -65,30 +65,30 @@ module Effective
65
65
  Rails.logger.info 'Processed by Effective::CrudController#create'
66
66
 
67
67
  self.resource ||= resource_scope.new
68
- action = commit_action[:action]
68
+ action = (commit_action[:action] == :save ? :create : commit_action[:action])
69
69
 
70
70
  resource.assign_attributes(send(resource_params_method_name))
71
71
  resource.created_by = current_user if resource.respond_to?(:created_by=)
72
72
 
73
- EffectiveResources.authorize!(self, (action == :save ? :create : action), resource)
73
+ EffectiveResources.authorize!(self, action, resource)
74
74
  @page_title ||= "New #{resource_name.titleize}"
75
75
 
76
76
  respond_to do |format|
77
77
  if save_resource(resource, action)
78
- request.format = :html if specific_redirect_path?
78
+ request.format = :html if specific_redirect_path?(action)
79
79
 
80
80
  format.html do
81
- flash[:success] ||= resource_flash(:success, resource, (action == :save ? :create : action))
82
- redirect_to(resource_redirect_path)
81
+ flash[:success] ||= resource_flash(:success, resource, action)
82
+ redirect_to(resource_redirect_path(action))
83
83
  end
84
84
 
85
85
  format.js do
86
- flash.now[:success] ||= resource_flash(:success, resource, (action == :save ? :create : action))
86
+ flash.now[:success] ||= resource_flash(:success, resource, action)
87
87
  reload_resource # create.js.erb
88
88
  end
89
89
  else
90
90
  flash.delete(:success)
91
- flash.now[:danger] ||= resource_flash(:danger, resource, (action == :save ? :create : action))
91
+ flash.now[:danger] ||= resource_flash(:danger, resource, action)
92
92
 
93
93
  run_callbacks(:resource_render)
94
94
 
@@ -130,21 +130,20 @@ module Effective
130
130
  Rails.logger.info 'Processed by Effective::CrudController#update'
131
131
 
132
132
  self.resource ||= resource_scope.find(params[:id])
133
- action = commit_action[:action]
133
+ action = (commit_action[:action] == :save ? :update : commit_action[:action])
134
134
 
135
- EffectiveResources.authorize!(self, (action == :save ? :update : action), resource)
135
+ EffectiveResources.authorize!(self, action, resource)
136
136
  @page_title ||= "Edit #{resource}"
137
137
 
138
138
  resource.assign_attributes(send(resource_params_method_name))
139
- resource.current_user = current_user if resource.respond_to?(:current_user=)
140
139
 
141
140
  respond_to do |format|
142
141
  if save_resource(resource, action)
143
- request.format = :html if specific_redirect_path?
142
+ request.format = :html if specific_redirect_path?(action)
144
143
 
145
144
  format.html do
146
145
  flash[:success] ||= resource_flash(:success, resource, action)
147
- redirect_to(resource_redirect_path)
146
+ redirect_to(resource_redirect_path(action))
148
147
  end
149
148
 
150
149
  format.js do
@@ -216,8 +215,6 @@ module Effective
216
215
  to_assign = (send(resource_params_method_name) rescue {})
217
216
  resource.assign_attributes(to_assign) if to_assign.present? && to_assign.permitted?
218
217
 
219
- resource.current_user = current_user if resource.respond_to?(:current_user=)
220
-
221
218
  respond_to do |format|
222
219
  if save_resource(resource, action)
223
220
  request.format = :html if specific_redirect_path?(action)
@@ -7,8 +7,12 @@ module Effective
7
7
  _insert_callbacks(names, blk) { |name, options| set_callback(:resource_render, :before, name, options) }
8
8
  end
9
9
 
10
+ def before_save(*names, &blk)
11
+ _insert_callbacks(names, blk) { |name, options| set_callback(:resource_before_save, :after, name, options) }
12
+ end
13
+
10
14
  def after_save(*names, &blk)
11
- _insert_callbacks(names, blk) { |name, options| set_callback(:resource_save, :after, name, options) }
15
+ _insert_callbacks(names, blk) { |name, options| set_callback(:resource_after_save, :after, name, options) }
12
16
  end
13
17
 
14
18
  def after_error(*names, &blk)
@@ -35,6 +35,10 @@ module Effective
35
35
  permitted_params = effective_resource.permitted_attributes.select do |name, (_, atts)|
36
36
  if BLACKLIST.include?(name)
37
37
  false
38
+ elsif name == :token # has_secure_token
39
+ resource.respond_to?("regenerate_#{name}") == false
40
+ elsif name == :site_id # acts_as_site_specific
41
+ resource.class.respond_to?(:site_specific?) == false
38
42
  elsif atts.blank? || !atts.key?(:permitted)
39
43
  true # Default is true
40
44
  else
@@ -57,6 +61,8 @@ module Effective
57
61
  { k => [] }
58
62
  elsif datatype == :effective_address
59
63
  { k => EffectiveAddresses.permitted_params }
64
+ elsif datatype == :effective_assets
65
+ EffectiveAssets.permitted_params
60
66
  else
61
67
  k
62
68
  end
@@ -5,33 +5,40 @@ module Effective
5
5
  # Based on the incoming params[:commit] or passed action. Merges all options.
6
6
  def commit_action(action = nil)
7
7
  config = (['create', 'update'].include?(params[:action]) ? self.class.submits : self.class.buttons)
8
+ ons = self.class.ons
8
9
 
9
- commit = if action.present?
10
- config[action.to_s] || config.find { |_, v| v[:action] == action }.try(:last) || { action: action }
11
- else
12
- config[params[:commit].to_s] || config.find { |_, v| v[:action] == :save }.try(:last) || { action: :save }
13
- end
10
+ commit = config[params[:commit].to_s]
11
+ commit ||= config.find { |_, v| v[:action] == action }.try(:last)
12
+ commit ||= config.find { |_, v| v[:action] == :save }.try(:last) if [nil, :create, :update].include?(action)
13
+ commit ||= { action: (action || :save) }
14
+
15
+ on = ons[params[:commit].to_s] || ons[action] || ons[commit[:action]]
14
16
 
15
- commit.reverse_merge!(self.class.ons[commit[:action]]) if self.class.ons[commit[:action]]
17
+ commit.reverse_merge!(on) if on.present?
16
18
 
17
19
  commit
18
20
  end
19
21
 
20
22
  # This calls the appropriate member action, probably save!, on the resource.
21
23
  def save_resource(resource, action = :save, &block)
22
- raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
24
+ save_action = ([:create, :update].include?(action) ? :save : action)
25
+
26
+ raise "expected @#{resource_name} to respond to #{save_action}!" unless resource.respond_to?("#{save_action}!")
23
27
 
24
28
  resource.current_user ||= current_user if resource.respond_to?(:current_user=)
25
29
 
26
30
  ActiveRecord::Base.transaction do
27
31
  begin
28
- if resource.public_send("#{action}!") == false
32
+ run_callbacks(:resource_before_save)
33
+
34
+ if resource.public_send("#{save_action}!") == false
29
35
  raise("failed to #{action} #{resource}")
30
36
  end
31
37
 
32
38
  yield if block_given?
33
39
 
34
- run_callbacks(:resource_save)
40
+ run_callbacks(:resource_after_save)
41
+
35
42
  return true
36
43
  rescue => e
37
44
  Rails.logger.info "Failed to #{action}: #{e.message}" if Rails.env.development?
@@ -11,7 +11,7 @@ module Effective
11
11
  included do
12
12
  define_actions_from_routes
13
13
  define_permitted_params_from_model
14
- define_callbacks :resource_render, :resource_save, :resource_error
14
+ define_callbacks :resource_render, :resource_before_save, :resource_after_save, :resource_error
15
15
  end
16
16
 
17
17
  module ClassMethods
@@ -71,7 +71,7 @@ module EffectiveResourcesHelper
71
71
  actions ||= (resource.kind_of?(Class) ? effective_resource.resource_klass_actions : effective_resource.resource_actions)
72
72
 
73
73
  # Filter Actions
74
- action_keys = effective_resource.actions #actions.map { |_, v| v[:action] }
74
+ action_keys = effective_resource.actions
75
75
  raise "unknown action for #{effective_resource.name}: #{(atts.keys - action_keys).join(' ')}." if (atts.keys - action_keys).present?
76
76
  actions = actions.select { |_, v| atts[v[:action]].respond_to?(:call) ? instance_exec(&atts[v[:action]]) : (atts[v[:action]] != false) }
77
77
 
@@ -1,9 +1,9 @@
1
+ # frozen_sting_literals: true
2
+
1
3
  module EffectiveResourcesPrivateHelper
2
4
  REPLACE_PAGE_ACTIONS = {'update' => :edit, 'create' => :new}
3
5
 
4
- def permitted_resource_actions(resource, actions, effective_resource = nil)
5
- effective_resource ||= find_effective_resource
6
-
6
+ def permitted_resource_actions(resource, actions)
7
7
  page_action = REPLACE_PAGE_ACTIONS[params[:action]] || params[:action]&.to_sym || :save
8
8
 
9
9
  actions.select do |commit, args|
@@ -14,19 +14,24 @@ module EffectiveResourcesPrivateHelper
14
14
  (args.key?(:if) ? controller.instance_exec(&args[:if]) : true) &&
15
15
  (args.key?(:unless) ? !controller.instance_exec(&args[:unless]) : true) &&
16
16
  EffectiveResources.authorized?(controller, action, resource)
17
- end.transform_values.with_index do |opts, index|
18
- action = opts[:action]
17
+ end.inject({}) do |h, (commit, args)|
18
+ opts = args.except(:default, :only, :except, :if, :unless, :redirect, :success, :danger)
19
19
 
20
20
  # Transform data: { ... } hash into 'data-' keys
21
- if (data = opts.delete(:data))
22
- data.each { |k, v| opts["data-#{k}"] ||= v }
21
+ if opts.key?(:data)
22
+ opts.delete(:data).each { |k, v| opts["data-#{k}"] ||= v }
23
+ end
24
+
25
+ # Replace resource name in any token strings
26
+ if opts.key?('data-confirm')
27
+ opts['data-confirm'].gsub!('@resource', (resource.to_s.presence || resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ')).to_s)
23
28
  end
24
29
 
25
30
  # Assign class
26
31
  opts[:class] ||= (
27
- if opts['data-method'].to_s == 'delete'
32
+ if opts['data-method'] == 'delete'
28
33
  'btn btn-danger'
29
- elsif index == 0
34
+ elsif h.length == 0
30
35
  'btn btn-primary'
31
36
  elsif defined?(EffectiveBootstrap)
32
37
  'btn btn-secondary'
@@ -36,22 +41,16 @@ module EffectiveResourcesPrivateHelper
36
41
  )
37
42
 
38
43
  # Assign title
39
- unless action == :save
40
- opts[:title] ||= case action
41
- when :edit then "Edit #{resource}"
42
- when :show then "#{resource}"
43
- when :destroy then "Delete #{resource}"
44
- when :index then "All #{effective_resource.human_plural_name.titleize}"
45
- else "#{action.to_s.titleize} #{resource}"
46
- end
47
- end
48
-
49
- # Replace resource name in any token strings
50
- if opts['data-confirm']
51
- opts['data-confirm'].gsub!('@resource', (resource.to_s.presence || effective_resource.human_name).to_s)
44
+ opts[:title] ||= case opts[:action]
45
+ when :save then commit
46
+ when :edit then "Edit #{resource}"
47
+ when :show then "#{resource}"
48
+ when :destroy then "Delete #{resource}"
49
+ when :index then "All #{resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ').titleize.pluralize}"
50
+ else "#{opts[:action].to_s.titleize} #{resource}"
52
51
  end
53
52
 
54
- opts.except(:default, :only, :except, :if, :unless, :redirect, :success, :danger)
53
+ h[commit] = opts; h
55
54
  end
56
55
  end
57
56
 
@@ -20,7 +20,7 @@ module Effective
20
20
  h[ass.foreign_key.to_sym] = [:integer, :index => true]
21
21
 
22
22
  if ass.options[:polymorphic]
23
- h[ass.foreign_type.to_sym] = [:integer, :index => true]
23
+ h[ass.foreign_type.to_sym] = [:string, :index => true]
24
24
  end
25
25
 
26
26
  end; h
@@ -36,10 +36,15 @@ module Effective
36
36
  end
37
37
 
38
38
  def effective_addresses_attributes
39
- return {} unless instance.respond_to?(:effective_address_names)
39
+ return {} unless defined?(EffectiveAddresses) && instance.respond_to?(:effective_address_names)
40
40
  instance.effective_address_names.inject({}) { |h, name| h[name] = [:effective_address]; h }
41
41
  end
42
42
 
43
+ def effective_assets_attributes
44
+ return {} unless defined?(EffectiveAssets) && instance.respond_to?(:asset_boxes)
45
+ { effective_assets: [:effective_assets] }
46
+ end
47
+
43
48
  # All will include primary_key, created_at, updated_at and belongs_tos
44
49
  # This is the attributes as defined by the effective_resources do .. end block
45
50
  # { :name => [:string, { permitted: false }], ... }
@@ -52,6 +57,7 @@ module Effective
52
57
  .merge(has_manys_attributes)
53
58
  .merge(has_ones_attributes)
54
59
  .merge(effective_addresses_attributes)
60
+ .merge(effective_assets_attributes)
55
61
  .merge(atts)
56
62
  else # This is the migrator. This should match table_attributes
57
63
  belong_tos_attributes.merge(atts.reject { |_, v| v[0] == :permitted_param })
@@ -1,4 +1,4 @@
1
- - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
1
+ - permitted_resource_actions(resource, actions).each do |label, opts|
2
2
  = link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action))
3
3
 
4
4
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
@@ -1,5 +1,5 @@
1
1
  = dropdown(variation: :dropleft) do
2
- - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
2
+ - permitted_resource_actions(resource, actions).each do |label, opts|
3
3
  = dropdown_link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
4
4
 
5
5
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
@@ -1,4 +1,4 @@
1
- - permitted_resource_actions(resource, actions, effective_resource).each do |label, opts|
1
+ - permitted_resource_actions(resource, actions).each do |label, opts|
2
2
  = glyphicon_to(opts[:action], (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
3
3
 
4
4
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.0.3'.freeze
2
+ VERSION = '1.0.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: 1.0.3
4
+ version: 1.0.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: 2018-10-05 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails