Class: ForestLiana::ResourceUpdater
- Inherits:
-
Object
- Object
- ForestLiana::ResourceUpdater
- Includes:
- RecordFindable
- 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.
8 9 10 11 12 13 |
# File 'app/services/forest_liana/resource_updater.rb', line 8 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.
6 7 8 |
# File 'app/services/forest_liana/resource_updater.rb', line 6 def errors @errors end |
#record ⇒ Object
Returns the value of attribute record.
5 6 7 |
# File 'app/services/forest_liana/resource_updater.rb', line 5 def record @record end |
Instance Method Details
#has_strong_parameter ⇒ Object
40 41 42 |
# File 'app/services/forest_liana/resource_updater.rb', line 40 def has_strong_parameter Rails::VERSION::MAJOR > 5 || @resource.instance_method(:update!).arity == 1 end |
#perform ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/services/forest_liana/resource_updater.rb', line 15 def perform begin collection_name = ForestLiana.name_for(@resource) scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, @user, collection_name, @params[:timezone]) @record = find_record(scoped_records, @resource, @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
36 37 38 |
# File 'app/services/forest_liana/resource_updater.rb', line 36 def resource_params ResourceDeserializer.new(@resource, @params, false).perform end |