Class: Yeti::Editor
- Inherits:
-
Object
- Object
- Yeti::Editor
- Includes:
- ActiveModel::Dirty, ActiveModel::Validations
- Defined in:
- lib/yeti/editor.rb
Defined Under Namespace
Classes: InvalidDate
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
- #attributes_for_persist ⇒ Object
- #edited ⇒ Object
- #errors ⇒ Object
- #hash ⇒ Object
-
#initialize(context, edited = nil) ⇒ Editor
constructor
A new instance of Editor.
- #mandatory?(column) ⇒ Boolean
- #persist! ⇒ Object
- #persisted? ⇒ Boolean
- #save(opts = {}) ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
- #without_error? ⇒ Boolean
Constructor Details
#initialize(context, edited = nil) ⇒ Editor
Returns a new instance of Editor.
22 23 24 25 |
# File 'lib/yeti/editor.rb', line 22 def initialize(context, edited=nil) @context = context @edited = edited end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/yeti/editor.rb', line 7 def context @context end |
Class Method Details
.from_id(context, id) ⇒ Object
10 11 12 |
# File 'lib/yeti/editor.rb', line 10 def self.from_id(context, id) new context, (find_by_id context, id if id) end |
.inherited(subclass) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/yeti/editor.rb', line 14 def self.inherited(subclass) .each do |name, opts| subclass.attribute name, opts end subclass. if untranslated? super end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
89 90 91 92 93 94 95 96 |
# File 'lib/yeti/editor.rb', line 89 def ==(other) other.equal?(self) || ( persisted? && other.instance_of?(self.class) && other.persisted? && other.id==id ) end |
#attributes ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/yeti/editor.rb', line 47 def attributes attributes = {} self.class.attributes.each do |key| attributes[key] = send key end attributes end |
#attributes=(attrs) ⇒ Object
55 56 57 58 59 |
# File 'lib/yeti/editor.rb', line 55 def attributes=(attrs) self.class.attributes.each do |key| self.send "#{key}=", attrs[key] if attrs.has_key? key end end |
#attributes_for_persist ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/yeti/editor.rb', line 103 def attributes_for_persist attributes = {} self.class.attributes.each do |key| value = send key opts = self.class.[key] attributes[key] = format_input_for_persist value, opts end attributes end |
#edited ⇒ Object
27 28 29 |
# File 'lib/yeti/editor.rb', line 27 def edited @edited ||= self.class.new_object context end |
#errors ⇒ Object
35 36 37 38 39 40 |
# File 'lib/yeti/editor.rb', line 35 def errors @errors ||= ::Yeti::Errors.new( self, untranslated: self.class.untranslated? ) end |
#hash ⇒ Object
99 100 101 |
# File 'lib/yeti/editor.rb', line 99 def hash id.hash end |
#mandatory?(column) ⇒ Boolean
83 84 85 86 87 |
# File 'lib/yeti/editor.rb', line 83 def mandatory?(column) self.class.validators_on(column).any? do |validator| validator.kind_of? ::ActiveModel::Validations::PresenceValidator end end |
#persist! ⇒ Object
75 76 77 |
# File 'lib/yeti/editor.rb', line 75 def persist! raise NotImplementedError, "#{self.class}#persist!" end |
#persisted? ⇒ Boolean
31 32 33 |
# File 'lib/yeti/editor.rb', line 31 def persisted? edited ? edited.persisted? : false end |
#save(opts = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/yeti/editor.rb', line 61 def save(opts={}) if opts.fetch :validate, true return false unless valid? end persist! @previously_changed = changes if respond_to? :changes_applied changes_applied else changed_attributes.clear end true end |
#update_attributes(attrs = {}) ⇒ Object
42 43 44 45 |
# File 'lib/yeti/editor.rb', line 42 def update_attributes(attrs={}) self.attributes = attrs save end |
#without_error? ⇒ Boolean
79 80 81 |
# File 'lib/yeti/editor.rb', line 79 def without_error? errors.empty? end |