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
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 34 35 |
# File 'lib/mini_form/model.rb', line 8 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
57 58 59 |
# File 'lib/mini_form/model.rb', line 57 def attributes Hash[self.class.attribute_names.map { |name| [name, public_send(name)] }] end |
#attributes=(attributes) ⇒ Object Also known as: assign_attributes
45 46 47 48 49 50 51 52 53 |
# File 'lib/mini_form/model.rb', line 45 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
37 38 39 |
# File 'lib/mini_form/model.rb', line 37 def initialize(attributes = {}) self.attributes = attributes end |
#persisted? ⇒ Boolean
41 42 43 |
# File 'lib/mini_form/model.rb', line 41 def persisted? false end |
#update(attributes = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mini_form/model.rb', line 61 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
76 77 78 79 80 |
# File 'lib/mini_form/model.rb', line 76 def update!(attributes = {}) raise InvalidForm, self unless update attributes self end |