Class: Yeti::Editor

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty, ActiveModel::Validations
Defined in:
lib/yeti/editor.rb

Defined Under Namespace

Classes: InvalidDate

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#contextObject (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)
  attribute_options.each do |name, opts|
    subclass.attribute name, opts
  end
  subclass.dont_translate_error_messages 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

#attributesObject



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_persistObject



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.attribute_options[key]
    attributes[key] = format_input_for_persist value, opts
  end
  attributes
end

#editedObject



27
28
29
# File 'lib/yeti/editor.rb', line 27

def edited
  @edited ||= self.class.new_object context
end

#errorsObject



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

#hashObject



99
100
101
# File 'lib/yeti/editor.rb', line 99

def hash
  id.hash
end

#mandatory?(column) ⇒ Boolean

Returns:

  • (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

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/yeti/editor.rb', line 75

def persist!
  raise NotImplementedError, "#{self.class}#persist!"
end

#persisted?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


79
80
81
# File 'lib/yeti/editor.rb', line 79

def without_error?
  errors.empty?
end