Class: Abyme::Model::AttributesBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/abyme/model.rb

Instance Method Summary collapse

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_attributesObject



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(authorized_attributes, nested_attributes)
  authorized_attributes += add_all_attributes
  authorized_attributes << nested_attributes unless nested_attributes.blank? || authorized_attributes.include?(nested_attributes)
  authorized_attributes
end

#build_attributesObject



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
  authorized_attributes = build_default_attributes
  if @permit && @attributes_list == :all_attributes
    authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
  elsif @permit
    @attributes_list << nested_attributes unless nested_attributes.blank? || @attributes_list.include?(nested_attributes)
    authorized_attributes += @attributes_list
  else
    authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
    authorized_attributes -= @attributes_list
  end
  authorized_attributes
end

#build_default_attributesObject



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

#destroy_allowed?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/abyme/model.rb', line 67

def destroy_allowed?
  Abyme::Model.instance_variable_get(:@allow_destroy).dig(@model, @association)
end