Class: Abyme::Model::AttributesBuilder
- Inherits:
-
Object
- Object
- Abyme::Model::AttributesBuilder
- Defined in:
- lib/abyme/model.rb
Instance Method Summary collapse
- #add_all_attributes ⇒ Object
- #build_all_attributes(authorized_attributes, nested_attributes) ⇒ Object
- #build_attributes ⇒ Object
- #build_default_attributes ⇒ Object
- #destroy_allowed? ⇒ Boolean
-
#initialize(model, association, attributes, permit = true, association_class_name = nil) ⇒ AttributesBuilder
constructor
A new instance of AttributesBuilder.
Constructor Details
#initialize(model, association, attributes, permit = true, association_class_name = nil) ⇒ AttributesBuilder
Returns a new instance of AttributesBuilder.
44 45 46 47 48 49 50 |
# File 'lib/abyme/model.rb', line 44 def initialize(model, association, attributes, permit = true, association_class_name = nil) @model = model @association = association @attributes_list = attributes @permit = permit @association_class = association_class_name&.safe_constantize || @association.to_s.classify.constantize end |
Instance Method Details
#add_all_attributes ⇒ Object
71 72 73 |
# File 'lib/abyme/model.rb', line 71 def add_all_attributes @association_class.column_names.map(&:to_sym).reject { |attr| [:id, :created_at, :updated_at].include?(attr) } end |
#build_all_attributes(authorized_attributes, nested_attributes) ⇒ Object
75 76 77 78 79 |
# File 'lib/abyme/model.rb', line 75 def build_all_attributes(, nested_attributes) += add_all_attributes << nested_attributes unless nested_attributes.blank? || .include?(nested_attributes) end |
#build_attributes ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/abyme/model.rb', line 52 def build_attributes nested_attributes = @association_class.abyme_attributes if @association_class.respond_to? :abyme_attributes = build_default_attributes if @permit && @attributes_list == :all_attributes = build_all_attributes(, nested_attributes) elsif @permit @attributes_list << nested_attributes unless nested_attributes.blank? || @attributes_list.include?(nested_attributes) += @attributes_list else = build_all_attributes(, nested_attributes) -= @attributes_list end end |
#build_default_attributes ⇒ Object
81 82 83 84 85 |
# File 'lib/abyme/model.rb', line 81 def build_default_attributes attributes = [:id] attributes << :_destroy if destroy_allowed? attributes end |