Module: Zena::Use::PropEval::ModelMethods

Included in:
Node
Defined in:
lib/zena/use/prop_eval.rb

Overview

VirtualClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zena/use/prop_eval.rb', line 42

def self.included(base)
  base.before_validation  :merge_prop_eval
  base.before_validation  :need_set__id
  base.before_save        :set__id
  # We do it once more to have 'zip'. If anyone knows a better
  # way to avoid doing this twice (before_validation and before_create) and
  # still manage to have the node.zip available in prop_eval...
  base.before_create      :merge_prop_eval
  base.alias_method_chain :rebuild_index_for_version, :prop_eval
  # So that we can use 'now' with prop_eval
  base.safe_method :now => {:class => Time, :method => 'Time.now'}
end

Instance Method Details

#merge_prop_eval(force_rebuild = false) ⇒ Object

Must happend after ‘change_klass’



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zena/use/prop_eval.rb', line 81

def merge_prop_eval(force_rebuild = false)
  return unless vclass_id
  return unless force_rebuild || prop.changed? || klass_changed?

  if code = vclass.prop_eval
    hash = safe_eval(code)
    if hash.kind_of?(Hash)
      # forces a check on valid properties
      self.attributes = hash
      true
    else
      errors.add(:base, "Invalid computed properties result (expected a Hash, found #{hash.class}).")
      false
    end
  end

rescue RubyLess::Error => err
  errors.add(:base, "Error during evaluation of #{klass} computed properties (#{err.message}).")
  return false # Will this properly halt the save chain ?
end

#need_set__idObject



66
67
68
69
70
71
72
73
# File 'lib/zena/use/prop_eval.rb', line 66

def need_set__id
  # Set DB identifier _id with latest title
  # This is not the best place to put this code, but it's hard to make sure it is only executed
  # in the correct order (after all properties are evaluated).

  @need_set__id = prop.title_changed?
  true
end

#rebuild_index_for_version_with_prop_eval(version) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/zena/use/prop_eval.rb', line 55

def rebuild_index_for_version_with_prop_eval(version)
  # Call other modules inserted before
  rebuild_index_for_version_without_prop_eval(version)
  merge_prop_eval(true)
  # Leaking from enrollable, but I am tired of alias method chaining and making sure
  # things are done in the correct order [GB].
  prepare_roles(true)
  # Only save properties, without changing updated_at date or other callbacks
  Zena::Db.set_attribute(version, 'properties', encode_properties(@properties)) if version.changed?
end

#set__idObject

TODO: decide if we need to keep this (Zena::Remote makes a much better console the MySQL console…)



76
77
78
# File 'lib/zena/use/prop_eval.rb', line 76

def set__id
  self._id = self.title if @need_set__id
end