module MiniForm::Model::ClassMethods

Public Instance Methods

attribute_names() click to toggle source
# File lib/mini_form/model.rb, line 133
def attribute_names
  @attribute_names ||= []
end
attributes(*attributes, delegate: nil, prefix: nil, allow_nil: nil) click to toggle source
# File lib/mini_form/model.rb, line 142
def attributes(*attributes, delegate: nil, prefix: nil, allow_nil: nil)
  if prefix
    attribute_names.push(*attributes.map do |name|
      :"#{prefix == true ? delegate : prefix}_#{name}"
    end)
  else
    attribute_names.push(*attributes)
  end

  if delegate.nil?
    attr_accessor(*attributes)
  else
    delegate(*attributes, to: delegate, prefix: prefix, allow_nil: allow_nil)
    delegate(*attributes.map { |attr| "#{attr}=" }, to: delegate, prefix: prefix, allow_nil: allow_nil)
  end
end
inherited(base) click to toggle source
# File lib/mini_form/model.rb, line 123
def inherited(base)
  new_attribute_names = attribute_names.dup
  new_models_to_save  = models_to_save.dup

  base.instance_eval do
    @attribute_names = new_attribute_names
    @models_to_save  = new_models_to_save
  end
end
model(name, attributes: [], read: [], prefix: nil, allow_nil: nil, save: false) click to toggle source
# File lib/mini_form/model.rb, line 159
def model(name, attributes: [], read: [], prefix: nil, allow_nil: nil, save: false) # rubocop:disable ParameterLists
  attr_accessor name

  attributes(*attributes, delegate: name, prefix: prefix, allow_nil: allow_nil) unless attributes.empty?

  delegate(*read, to: name, prefix: prefix, allow_nil: nil)

  validates name, 'mini_form/nested' => true

  models_to_save << name if save
end
models_to_save() click to toggle source

:api: private

# File lib/mini_form/model.rb, line 138
def models_to_save
  @models_to_save ||= []
end