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.



14
15
16
17
# File 'lib/yeti/editor.rb', line 14

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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



77
78
79
80
81
82
83
84
# File 'lib/yeti/editor.rb', line 77

def ==(other)
  other.equal?(self) || (
    persisted? &&
    other.instance_of?(self.class) &&
    other.persisted? &&
    other.id==id
  )
end

#attributesObject



39
40
41
42
43
44
45
# File 'lib/yeti/editor.rb', line 39

def attributes
  attributes = {}
  self.class.attributes.each do |key|
    attributes[key] = send key
  end
  attributes
end

#attributes=(attrs) ⇒ Object



47
48
49
50
51
# File 'lib/yeti/editor.rb', line 47

def attributes=(attrs)
  self.class.attributes.each do |key|
    self.send "#{key}=", attrs[key] if attrs.has_key? key
  end
end

#attributes_for_persistObject



91
92
93
94
95
96
97
98
99
# File 'lib/yeti/editor.rb', line 91

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



19
20
21
# File 'lib/yeti/editor.rb', line 19

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

#errorsObject



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

def errors
  @errors ||= ::Yeti::Errors.new(
    self,
    untranslated: self.class.untranslated?
  )
end

#hashObject



87
88
89
# File 'lib/yeti/editor.rb', line 87

def hash
  id.hash
end

#mandatory?(column) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/yeti/editor.rb', line 71

def mandatory?(column)
  self.class.validators_on(column).any? do |validator|
    validator.kind_of? ::ActiveModel::Validations::PresenceValidator
  end
end

#persist!Object

Raises:

  • (NotImplementedError)


63
64
65
# File 'lib/yeti/editor.rb', line 63

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

#persisted?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/yeti/editor.rb', line 23

def persisted?
  edited ? edited.persisted? : false
end

#save(opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/yeti/editor.rb', line 53

def save(opts={})
  if opts.fetch :validate, true
    return false unless valid?
  end
  persist!
  @previously_changed = changes
  changed_attributes.clear
  true
end

#update_attributes(attrs) ⇒ Object



34
35
36
37
# File 'lib/yeti/editor.rb', line 34

def update_attributes(attrs)
  self.attributes = attrs
  save
end

#without_error?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/yeti/editor.rb', line 67

def without_error?
  errors.empty?
end