2017-07-16 13:11:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-03-02 23:20:17 -05:00
|
|
|
module ActiveModel
|
2013-12-25 08:20:44 -05:00
|
|
|
# == Active \Model \Basic \Model
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
2021-04-21 14:54:16 -04:00
|
|
|
# Allows implementing models similar to <tt>ActiveRecord::Base</tt>.
|
|
|
|
# Includes <tt>ActiveModel::API</tt> for the required interface for an
|
|
|
|
# object to interact with Action Pack and Action View, but can be
|
|
|
|
# extended with other functionalities.
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
|
|
|
# A minimal implementation could be:
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :name, :age
|
|
|
|
# end
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# person = Person.new(name: 'bob', age: '18')
|
2014-06-13 12:56:55 -04:00
|
|
|
# person.name # => "bob"
|
|
|
|
# person.age # => "18"
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
2021-04-21 14:54:16 -04:00
|
|
|
# If for some reason you need to run code on <tt>initialize</tt>, make
|
2012-06-23 16:12:33 -04:00
|
|
|
# sure you call +super+ if you want the attributes hash initialization to
|
|
|
|
# happen.
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :id, :name, :omg
|
|
|
|
#
|
2012-04-14 03:35:44 -04:00
|
|
|
# def initialize(attributes={})
|
2012-03-03 02:19:39 -05:00
|
|
|
# super
|
|
|
|
# @omg ||= true
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# person = Person.new(id: 1, name: 'bob')
|
2012-03-03 02:19:39 -05:00
|
|
|
# person.omg # => true
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# For more detailed information on other functionalities available, please
|
|
|
|
# refer to the specific modules included in <tt>ActiveModel::Model</tt>
|
|
|
|
# (see below).
|
2012-03-02 23:20:17 -05:00
|
|
|
module Model
|
2014-08-24 17:51:46 -04:00
|
|
|
extend ActiveSupport::Concern
|
2021-04-21 14:54:16 -04:00
|
|
|
include ActiveModel::API
|
2012-03-02 23:20:17 -05:00
|
|
|
end
|
|
|
|
end
|