Class: ForestLiana::ResourceUpdater
- Inherits:
-
Object
- Object
- ForestLiana::ResourceUpdater
- Defined in:
- app/services/forest_liana/resource_updater.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#record ⇒ Object
Returns the value of attribute record.
Instance Method Summary collapse
- #has_strong_parameter ⇒ Object
-
#initialize(resource, params, forest_user) ⇒ ResourceUpdater
constructor
A new instance of ResourceUpdater.
- #perform ⇒ Object
- #resource_params ⇒ Object
Constructor Details
#initialize(resource, params, forest_user) ⇒ ResourceUpdater
Returns a new instance of ResourceUpdater.
6 7 8 9 10 11 |
# File 'app/services/forest_liana/resource_updater.rb', line 6 def initialize(resource, params, forest_user) @resource = resource @params = params @errors = nil @user = forest_user end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'app/services/forest_liana/resource_updater.rb', line 4 def errors @errors end |
#record ⇒ Object
Returns the value of attribute record.
3 4 5 |
# File 'app/services/forest_liana/resource_updater.rb', line 3 def record @record end |
Instance Method Details
#has_strong_parameter ⇒ Object
38 39 40 |
# File 'app/services/forest_liana/resource_updater.rb', line 38 def has_strong_parameter Rails::VERSION::MAJOR > 5 || @resource.instance_method(:update!).arity == 1 end |
#perform ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/services/forest_liana/resource_updater.rb', line 13 def perform begin collection_name = ForestLiana.name_for(@resource) scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, @user, collection_name, @params[:timezone]) @record = scoped_records.find(@params[:id]) if has_strong_parameter @record.update(resource_params) else @record.update(resource_params, without_protection: true) end rescue ActiveRecord::StatementInvalid => exception # NOTICE: SQLÂ request cannot be executed properly @errors = [{ detail: exception.cause.error }] rescue ForestLiana::Errors::SerializeAttributeBadFormat => exception @errors = [{ detail: exception. }] rescue => exception @errors = [{ detail: exception. }] end end |
#resource_params ⇒ Object
34 35 36 |
# File 'app/services/forest_liana/resource_updater.rb', line 34 def resource_params ResourceDeserializer.new(@resource, @params, false).perform end |