effective_resources 1.4.9 → 1.5.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: 78b99e4e97fc17bdd6d06ef1ab5c726b23a8697906123537da8d2b72c5ea83a7
4
- data.tar.gz: 040d8d9d90165d0582f4ac50390214ad942f5faf9a287782b103dd30048988c8
3
+ metadata.gz: 96ca5cf766ce1834be39c1cc4698eed91291824f5cc242fcd4daa5f287bcbe95
4
+ data.tar.gz: 3297962f09ef0490a3c53ccd3eb7d82858ed0a553a935e8ce264a7947c70fcdb
5
5
  SHA512:
6
- metadata.gz: dc405eda869d37fb5669365c7acfbbaab2af111802b57b3753372b2eb684e905de07e6ace209ec412016fd8a713f4ce125761b55985408b5085fcf24c8e679eb
7
- data.tar.gz: a289eb14cc256be32c392fdd6f9497cb03367bce59bc17c611a8dc6e8b5f2d72694e301826e3186abf82dd216773dd2a61d663db5b19f80d4c1d645ace341af7
6
+ metadata.gz: a88337e14e4d6df602ec530a07e57a48ac345fc25ade3c5ebaf34b9038c67decb76e17f58f7c431c0bd58e64cb589dfd0b4f4f3055b6082fb4f1b2e68f9f0ad2
7
+ data.tar.gz: cb339dd8e0a39d7e0c78e483a0d122c4b7de0d682f031ec91cf1d9a2184fe527e96278edbd92176d18564d5bb6c9f7edacd4f0336b4eb222682bed52153f991d
@@ -63,9 +63,11 @@ module Effective
63
63
  self.resource ||= resource_scope.new
64
64
  action = (commit_action[:action] == :save ? :create : commit_action[:action])
65
65
 
66
- resource.assign_attributes(send(resource_params_method_name))
66
+ resource.current_user ||= current_user if resource.respond_to?(:current_user=)
67
67
  resource.created_by ||= current_user if resource.respond_to?(:created_by=)
68
68
 
69
+ resource.assign_attributes(send(resource_params_method_name))
70
+
69
71
  EffectiveResources.authorize!(self, action, resource)
70
72
  @page_title ||= "New #{resource_name.titleize}"
71
73
 
@@ -119,6 +121,9 @@ module Effective
119
121
  EffectiveResources.authorize!(self, action, resource)
120
122
  @page_title ||= "Edit #{resource}"
121
123
 
124
+ resource.current_user ||= current_user if resource.respond_to?(:current_user=)
125
+ resource.updated_by ||= current_user if resource.respond_to?(:updated_by=)
126
+
122
127
  resource.assign_attributes(send(resource_params_method_name))
123
128
 
124
129
  if save_resource(resource, action)
@@ -79,54 +79,6 @@ module Effective
79
79
  end
80
80
  end
81
81
 
82
- # def respond_with_error(resource, action)
83
- # return if response.body.present?
84
-
85
- # flash.delete(:success)
86
- # flash.now[:danger] ||= resource_flash(:danger, resource, action)
87
-
88
- # respond_to do |format|
89
- # case action.to_sym
90
- # when :create
91
- # format.html { render :new }
92
- # when :update
93
- # format.html { render :edit }
94
- # when :destroy
95
- # format.html do
96
- # redirect_flash
97
- # redirect_to(resource_redirect_path(action))
98
- # end
99
- # else # member action
100
- # from_path = referer_redirect_path.to_s
101
-
102
- # format.html do
103
- # if resource_edit_path && from_path.end_with?(resource_edit_path)
104
- # @page_title ||= "Edit #{resource}"
105
- # render :edit
106
- # elsif resource_new_path && from_path.end_with?(resource_new_path)
107
- # @page_title ||= "New #{resource_name.titleize}"
108
- # render :new
109
- # elsif resource_action_path(action) && from_path.end_with?(resource_action_path(action)) && template_present?(action)
110
- # @page_title ||= "#{action.to_s.titleize} #{resource}"
111
- # render(action, locals: { action: action })
112
- # elsif resource_show_path && from_path.end_with?(resource_show_path)
113
- # @page_title ||= resource_name.titleize
114
- # render :show
115
- # else
116
- # @page_title ||= resource.to_s
117
- # redirect_flash
118
- # redirect_to(from_path.presence || resource_redirect_path(action))
119
- # end
120
- # end
121
- # end
122
-
123
- # format.js do
124
- # view = template_present?(action) ? action : :member_action
125
- # render(view, locals: { action: action }) # action.js.erb
126
- # end
127
- # end
128
- # end
129
-
130
82
  private
131
83
 
132
84
  def redirect_flash
@@ -4,7 +4,9 @@ module Effective
4
4
 
5
5
  # Based on the incoming params[:commit] or passed action. Merges all options.
6
6
  def commit_action(action = nil)
7
- config = (['create', 'update'].include?(params[:action]) ? self.class.submits : self.class.buttons)
7
+ #config = (['create', 'update'].include?(params[:action]) ? self.class.submits : self.class.buttons)
8
+
9
+ config = self.class.submits
8
10
  ons = self.class.ons
9
11
 
10
12
  commit = config[params[:commit].to_s]
@@ -146,6 +146,30 @@ module EffectiveResourcesHelper
146
146
  end
147
147
  end
148
148
 
149
+ # Similar to render_resource_form
150
+ def render_resource_partial(resource, atts = {})
151
+ unless resource.kind_of?(ActiveRecord::Base) || resource.class.ancestors.include?(ActiveModel::Model)
152
+ raise 'expected first argument to be an ActiveRecord or ActiveModel object'
153
+ end
154
+
155
+ raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
156
+
157
+ effective_resource = (atts.delete(:effective_resource) || find_effective_resource)
158
+
159
+ action = atts.delete(:action)
160
+ atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
161
+
162
+ if lookup_context.template_exists?(effective_resource.name, controller._prefixes, :partial)
163
+ render(effective_resource.name, atts)
164
+ elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.plural_name], :partial)
165
+ render(effective_resource.plural_name + '/' + effective_resource.name, atts)
166
+ elsif lookup_context.template_exists?(effective_resource.name, [effective_resource.name], :partial)
167
+ render(effective_resource.name + '/' + effective_resource.name, atts)
168
+ else
169
+ render(resource, atts) # Will raise the regular error
170
+ end
171
+ end
172
+
149
173
  # Tableize attributes
150
174
  # This is used by effective_orders, effective_logging, effective_trash and effective_mergery
151
175
  def tableize_hash(obj, table: 'table', th: true, sub_table: 'table', sub_th: true, flatten: true)
@@ -28,7 +28,7 @@
28
28
  module ActsAsArchived
29
29
  extend ActiveSupport::Concern
30
30
 
31
- module ActiveRecord
31
+ module Base
32
32
  def acts_as_archived(cascade: [], strategy: :archive)
33
33
 
34
34
  cascade = Array(cascade).compact
@@ -7,7 +7,7 @@
7
7
  module ActsAsSlugged
8
8
  extend ActiveSupport::Concern
9
9
 
10
- module ActiveRecord
10
+ module Base
11
11
  def acts_as_slugged(options = nil)
12
12
  include ::ActsAsSlugged
13
13
  end
@@ -8,8 +8,8 @@
8
8
  module ActsAsStatused
9
9
  extend ActiveSupport::Concern
10
10
 
11
- module ActiveRecord
12
- # acts_as_statuses :pending, :approved, :declined, option_key: :option_value
11
+ module Base
12
+ # acts_as_statused :pending, :approved, :declined, option_key: :option_value
13
13
  def acts_as_statused(*args)
14
14
  options = args.extract_options!
15
15
  statuses = Array(args).compact
@@ -6,7 +6,7 @@
6
6
  module ActsAsTokened
7
7
  extend ActiveSupport::Concern
8
8
 
9
- module ActiveRecord
9
+ module Base
10
10
  def acts_as_tokened(options = nil)
11
11
  include ::ActsAsTokened
12
12
  end
@@ -5,7 +5,7 @@
5
5
  module EffectiveResource
6
6
  extend ActiveSupport::Concern
7
7
 
8
- module ActiveRecord
8
+ module Base
9
9
  def effective_resource(options = nil, &block)
10
10
  return @_effective_resource unless block_given?
11
11
 
@@ -75,11 +75,11 @@ module Effective
75
75
  when :date, :datetime
76
76
  if (digits = value.to_s.scan(/(\d+)/).flatten).present?
77
77
  date = if digits.first.length == 4 # 2017-01-10
78
- Time.zone.local(*digits)
78
+ (Time.zone.local(*digits) rescue nil)
79
79
  else # 01/10/2016
80
80
  year = digits.find { |d| d.length == 4}
81
81
  digits = [year] + (digits - [year])
82
- Time.zone.local(*digits)
82
+ (Time.zone.local(*digits) rescue nil)
83
83
  end
84
84
 
85
85
  name.to_s.start_with?('end_') ? date.end_of_day : date
@@ -87,7 +87,7 @@ module Effective
87
87
  when :time
88
88
  if (digits = value.to_s.scan(/(\d+)/).flatten).present?
89
89
  now = Time.zone.now
90
- Time.zone.local(now.year, now.month, now.day, *digits)
90
+ (Time.zone.local(now.year, now.month, now.day, *digits) rescue nil)
91
91
  end
92
92
  when :decimal, :currency
93
93
  (value.kind_of?(String) ? value.gsub(/[^0-9|\-|\.]/, '') : value).to_f
@@ -8,7 +8,7 @@
8
8
  .col-4.text-right
9
9
  = render_resource_buttons(@resource, show: false)
10
10
 
11
- = render @resource
11
+ = render_resource_partial(@resource)
12
12
 
13
13
  .form-actions
14
14
  = link_to 'Continue', (resource.action_path(:index) || root_path), class: 'btn btn-primary'
@@ -1,6 +1,6 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveForm.remote_form_payload = "<%= j render(@resource) %>";
4
+ EffectiveForm.remote_form_payload = "<%= j render_resource_partial(@resource) %>";
5
5
  EffectiveForm.remote_form_commit = "<%= params[:commit] %>";
6
6
  EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -1,5 +1,4 @@
1
1
  require 'effective_resources/engine'
2
- require 'effective_resources/version'
3
2
 
4
3
  module EffectiveResources
5
4
 
@@ -3,9 +3,14 @@ module EffectiveResources
3
3
  engine_name 'effective_resources'
4
4
 
5
5
  config.autoload_paths += Dir[
6
- "#{config.root}/lib/",
7
6
  "#{config.root}/jobs/",
8
- "#{config.root}/app/models/validators/",
7
+ "#{config.root}/lib/validators/",
8
+ "#{config.root}/app/controllers/concerns/"
9
+ ]
10
+
11
+ config.eager_load_paths += Dir[
12
+ "#{config.root}/jobs/",
13
+ "#{config.root}/lib/validators/",
9
14
  "#{config.root}/app/controllers/concerns/"
10
15
  ]
11
16
 
@@ -17,11 +22,11 @@ module EffectiveResources
17
22
  # Include acts_as_addressable concern and allow any ActiveRecord object to call it
18
23
  initializer 'effective_resources.active_record' do |app|
19
24
  ActiveSupport.on_load :active_record do
20
- ActiveRecord::Base.extend(ActsAsArchived::ActiveRecord)
21
- ActiveRecord::Base.extend(ActsAsTokened::ActiveRecord)
22
- ActiveRecord::Base.extend(ActsAsSlugged::ActiveRecord)
23
- ActiveRecord::Base.extend(ActsAsStatused::ActiveRecord)
24
- ActiveRecord::Base.extend(EffectiveResource::ActiveRecord)
25
+ ActiveRecord::Base.extend(ActsAsArchived::Base)
26
+ ActiveRecord::Base.extend(ActsAsTokened::Base)
27
+ ActiveRecord::Base.extend(ActsAsSlugged::Base)
28
+ ActiveRecord::Base.extend(ActsAsStatused::Base)
29
+ ActiveRecord::Base.extend(EffectiveResource::Base)
25
30
  end
26
31
  end
27
32
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.4.9'.freeze
2
+ VERSION = '1.5.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.4.9
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
11
+ date: 2020-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -71,9 +71,6 @@ files:
71
71
  - app/models/effective/resources/paths.rb
72
72
  - app/models/effective/resources/relation.rb
73
73
  - app/models/effective/resources/sql.rb
74
- - app/models/validators/email_cc_validator.rb
75
- - app/models/validators/email_validator.rb
76
- - app/models/validators/url_validator.rb
77
74
  - app/views/application/_flash.html.haml
78
75
  - app/views/application/create.js.erb
79
76
  - app/views/application/destroy.js.erb
@@ -94,11 +91,14 @@ files:
94
91
  - lib/effective_resources/engine.rb
95
92
  - lib/effective_resources/version.rb
96
93
  - lib/generators/effective_resources/install_generator.rb
94
+ - lib/validators/email_cc_validator.rb
95
+ - lib/validators/email_validator.rb
96
+ - lib/validators/url_validator.rb
97
97
  homepage: https://github.com/code-and-effect/effective_resources
98
98
  licenses:
99
99
  - MIT
100
100
  metadata: {}
101
- post_install_message:
101
+ post_install_message:
102
102
  rdoc_options: []
103
103
  require_paths:
104
104
  - lib
@@ -113,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.0.3
117
- signing_key:
116
+ rubygems_version: 3.1.2
117
+ signing_key:
118
118
  specification_version: 4
119
119
  summary: Make any controller an effective resource controller.
120
120
  test_files: []