Module: JsonApiToolbox::Postable
- Defined in:
- lib/postable.rb
Class Method Summary collapse
- .association_class(model, key) ⇒ Object
- .check_child_association(value, key, model, child_model) ⇒ Object
- .normalize_post(hash, model) ⇒ Object
Class Method Details
.association_class(model, key) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/postable.rb', line 19 def association_class(model, key) association = model.reflect_on_all_associations.find do |association_i| association_i.name.to_s == key.to_s end return false unless association class_name = (association..symbolize_keys[:class_name] || key) ActiveSupport::Inflector.singularize(class_name).camelize.constantize end |
.check_child_association(value, key, model, child_model) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/postable.rb', line 28 def check_child_association(value, key, model, child_model) if value.is_a?(Hash) && !model.has_attribute?(key) normalize_post(value, child_model) else value end end |
.normalize_post(hash, model) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/postable.rb', line 6 def normalize_post(hash, model) associations = model.reflect_on_all_associations.map(&:name) normalized_hash = hash.map do |key, value| child_model = association_class(model, key) if value.is_a? Hash key = associations.include?(key.to_sym) ? "#{key}_attributes" : key hash_value = check_child_association(value, key, model, child_model) [key, hash_value] end Hash[normalized_hash] end |