Module: MiniForm::Model
- Defined in:
- lib/mini_form/model.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
- #attributes=(attributes) ⇒ Object (also: #assign_attributes)
- #initialize(attributes = {}) ⇒ Object
- #persisted? ⇒ Boolean
- #update(attributes = {}) ⇒ Object
- #update!(attributes = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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 |
Instance Method Details
#attributes ⇒ Object
55 56 57 |
# File 'lib/mini_form/model.rb', line 55 def attributes Hash[self.class.attribute_names.map { |name| [name, public_send(name)] }] end |
#attributes=(attributes) ⇒ Object Also known as: assign_attributes
43 44 45 46 47 48 49 50 51 |
# 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 |
#initialize(attributes = {}) ⇒ Object
35 36 37 |
# File 'lib/mini_form/model.rb', line 35 def initialize(attributes = {}) self.attributes = attributes end |
#persisted? ⇒ Boolean
39 40 41 |
# File 'lib/mini_form/model.rb', line 39 def persisted? false end |
#update(attributes = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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 = {}) ⇒ Object
74 75 76 77 |
# File 'lib/mini_form/model.rb', line 74 def update!(attributes = {}) raise InvalidForm, self unless update attributes self end |