Class: ForestLiana::ResourceDeserializer
- Inherits:
-
Object
- Object
- ForestLiana::ResourceDeserializer
- Defined in:
- app/deserializers/forest_liana/resource_deserializer.rb
Instance Method Summary collapse
- #acts_as_taggable_attribute?(attr) ⇒ Boolean
- #carrierwave_attribute?(attr) ⇒ Boolean
- #column?(attribute) ⇒ Boolean
- #extract_acts_as_taggable ⇒ Object
- #extract_attributes ⇒ Object
- #extract_carrierwave ⇒ Object
- #extract_paperclip ⇒ Object
- #extract_relationships ⇒ Object
- #has_acts_as_taggable? ⇒ Boolean
-
#initialize(resource, params) ⇒ ResourceDeserializer
constructor
A new instance of ResourceDeserializer.
- #paperclip_handler?(attr) ⇒ Boolean
- #perform ⇒ Object
Constructor Details
#initialize(resource, params) ⇒ ResourceDeserializer
Returns a new instance of ResourceDeserializer.
6 7 8 9 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 6 def initialize(resource, params) @params = params.permit! if params.respond_to?(:permit!) @resource = resource end |
Instance Method Details
#acts_as_taggable_attribute?(attr) ⇒ Boolean
108 109 110 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 108 def acts_as_taggable_attribute?(attr) @resource.acts_as_taggable.to_a.include?(attr) end |
#carrierwave_attribute?(attr) ⇒ Boolean
104 105 106 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 104 def carrierwave_attribute?(attr) @resource.uploaders.include?(attr.try(:to_sym)) end |
#column?(attribute) ⇒ Boolean
100 101 102 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 100 def column?(attribute) @resource.columns.find {|x| x.name == attribute}.present? end |
#extract_acts_as_taggable ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 79 def extract_acts_as_taggable return unless has_acts_as_taggable? @params['data']['attributes'].each do |key, value| if acts_as_taggable_attribute?(key) @attributes["#{key.singularize}_list"] = value @attributes.delete(key) end end end |
#extract_attributes ⇒ Object
21 22 23 24 25 26 27 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 21 def extract_attributes if @params[:data][:attributes] @params['data']['attributes'].select {|attr| column?(attr)} else ActionController::Parameters.new() end end |
#extract_carrierwave ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 69 def extract_carrierwave return unless @resource.respond_to?(:uploaders) @params['data']['attributes'].each do |key, value| if value && carrierwave_attribute?(key) @attributes[key] = ForestLiana::Base64StringIO.new(value) end end end |
#extract_paperclip ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 57 def extract_paperclip return unless @resource.respond_to?(:attachment_definitions) paperclip_attr = @params['data']['attributes'] .select do |attr| !column?(attr) && paperclip_handler?(@params['data']['attributes'][attr]) end @attributes.merge!(paperclip_attr) if paperclip_attr end |
#extract_relationships ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 29 def extract_relationships if @params['data']['relationships'] @params['data']['relationships'].each do |name, relationship| data = relationship['data'] # Rails 3 requires a :sym argument for the reflect_on_association # call. association = @resource.reflect_on_association(name.try(:to_sym)) if [:has_one, :belongs_to].include?(association.try(:macro)) # TODO: refactor like this? #if data.blank? #@attributes[name] = nil #else #@attributes[name] = association.klass.find(data[:id]) #end # ActionController::Parameters do not inherit from Hash anymore # since Rails 5. if (data.is_a?(Hash) || data.is_a?(ActionController::Parameters)) && data[:id] @attributes[name] = association.klass.find(data[:id]) elsif data.blank? @attributes[name] = nil end end end end end |
#has_acts_as_taggable? ⇒ Boolean
112 113 114 115 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 112 def has_acts_as_taggable? @resource.respond_to?(:acts_as_taggable) && @resource.acts_as_taggable.try(:to_a) end |
#paperclip_handler?(attr) ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 91 def paperclip_handler?(attr) begin Paperclip.io_adapters.handler_for(attr) true rescue Paperclip::AdapterRegistry::NoHandlerError false end end |
#perform ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 11 def perform @attributes = extract_attributes extract_relationships extract_paperclip extract_carrierwave extract_acts_as_taggable @attributes end |