Class: Yeti::Editor

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

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.



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

def initialize(context, edited=nil)
  @context = context
  @edited = edited
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/yeti/editor.rb', line 6

def context
  @context
end

Class Method Details

.from_id(context, id) ⇒ Object



9
10
11
# File 'lib/yeti/editor.rb', line 9

def self.from_id(context, id)
  new context, (find_by_id context, id if id)
end

Instance Method Details

#attributesObject



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

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

#attributes=(attrs) ⇒ Object



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

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

#editedObject



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

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

#errorsObject



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

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

#mandatory?(column) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#persist!Object

Raises:

  • (NotImplementedError)


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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

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

#save(opts = {}) ⇒ Object



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

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



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

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

#without_error?Boolean

Returns:

  • (Boolean)


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

def without_error?
  errors.empty?
end