module MiniForm::Model

Public Class Methods

included(base) click to toggle source
# File lib/mini_form/model.rb, line 6
def self.included(base)
  base.class_eval do
    include ActiveModel::Validations
    include ActiveModel::Validations::Callbacks
    include ActiveModel::Conversion

    extend ActiveModel::Naming
    extend ActiveModel::Callbacks

    extend ClassMethods

    define_model_callbacks :update
    define_model_callbacks :assignment

    # For backwards compatibility purpose
    define_model_callbacks :assigment

    before_update :before_update
    after_update :after_update

    before_assignment :before_assignment
    after_assignment :after_assignment

    # For backwards compatibility purpose
    before_assigment :before_assigment
    after_assigment :after_assigment
  end
end
new(attributes = {}) click to toggle source
# File lib/mini_form/model.rb, line 35
def initialize(attributes = {})
  self.attributes = attributes
end

Public Instance Methods

assign_attributes(attributes)
Alias for: attributes=
attributes() click to toggle source
# File lib/mini_form/model.rb, line 55
def attributes
  Hash[self.class.attribute_names.map { |name| [name, public_send(name)] }]
end
attributes=(attributes) click to toggle source
# File lib/mini_form/model.rb, line 43
def attributes=(attributes)
  run_callbacks :assignment do
    run_callbacks :assigment do
      attributes.slice(*self.class.attribute_names).each do |name, value|
        public_send "#{name}=", value
      end
    end
  end
end
Also aliased as: assign_attributes
persisted?() click to toggle source
# File lib/mini_form/model.rb, line 39
def persisted?
  false
end
update(attributes = {}) click to toggle source
# File lib/mini_form/model.rb, line 59
def update(attributes = {})
  self.attributes = attributes unless attributes.empty?

  return false unless valid?

  run_callbacks :update do
    transaction do
      save_models
      perform
    end
  end

  true
end
update!(attributes = {}) click to toggle source
# File lib/mini_form/model.rb, line 74
def update!(attributes = {})
  raise InvalidForm, self unless update attributes
  self
end

Private Instance Methods

after_assigment() click to toggle source
# File lib/mini_form/model.rb, line 118
def after_assigment
  # noop
end
after_assignment() click to toggle source
# File lib/mini_form/model.rb, line 110
def after_assignment
  # noop
end
after_update() click to toggle source
# File lib/mini_form/model.rb, line 102
def after_update
  # noop
end
before_assigment() click to toggle source
# File lib/mini_form/model.rb, line 114
def before_assigment
  # noop
end
before_assignment() click to toggle source
# File lib/mini_form/model.rb, line 106
def before_assignment
  # noop
end
before_update() click to toggle source
# File lib/mini_form/model.rb, line 98
def before_update
  # noop
end
perform() click to toggle source
# File lib/mini_form/model.rb, line 94
def perform
  # noop
end
save_models() click to toggle source

:api: private

# File lib/mini_form/model.rb, line 90
def save_models
  self.class.models_to_save.each { |model_name| public_send(model_name).save! }
end
transaction() { || ... } click to toggle source
# File lib/mini_form/model.rb, line 81
def transaction(&block)
  if defined? ActiveRecord
    ActiveRecord::Base.transaction(&block)
  else
    yield
  end
end