Module: BasicModel

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/basic_model.rb,
lib/basic_model/version.rb

Constant Summary collapse

VERSION =
'0.4.0'

Instance Method Summary collapse

Instance Method Details

#initialize(attributes = nil, options = {}) {|_self| ... } ⇒ Object

Creates and optionally sets the attributes of the model with keys matching the attribute names. If ActiveModel::MassSecurityAssignment is included, attributes will be sanitized with the role given in the :as option. To bypass mass-assignment security you can use the :without_protection => true option.

Yields:

  • (_self)

Yield Parameters:

  • _self (BasicModel)

    the object that the method was called on



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/basic_model.rb', line 14

def initialize(attributes = nil, options = {})
  unless attributes.nil?
    # Sanitize the attributes if we're using ActiveModel::MassSecurityAssignment
    # Also account for the different APIs between 3.0 and 3.1
    if protected_methods.include? :sanitize_for_mass_assignment
      if method(:sanitize_for_mass_assignment).arity == 2
        attributes = sanitize_for_mass_assignment attributes, options[:as]
      else
        attributes = sanitize_for_mass_assignment attributes
      end
    end

    # Now set the attributes, ignoring any private methods
    attributes.each do |name, value|
      send "#{name}=", value if respond_to? "#{name}="
    end
  end

  yield self if block_given?
end

#persisted?Boolean

By default, BasicModels are not persisted in any way.

Returns:

  • (Boolean)


36
37
38
# File 'lib/basic_model.rb', line 36

def persisted?
  false
end