Module: Zena::Use::ScopeIndex::IndexMethods

Included in:
IdxProject
Defined in:
lib/zena/use/scope_index.rb

Overview

VirtualClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zena/use/scope_index.rb', line 83

def self.included(base)
  AVAILABLE_MODELS << base

  class << base
    attr_accessor :groups
  end

  base.class_eval do
    include RubyLess
    before_validation     :set_site_id
    validates_presence_of :node_id
  end

  groups = base.groups = {}
  base.column_names.each do |name|
    next if %{created_at updated_at id node_id site_id}.include?(name)
    if name =~ %r{\A([^_]+)_(.+)\Z}
      (groups[$1] ||= []) << $2
      unless $2 == 'id'
        base.safe_attribute name
      end
    end
  end
end

Instance Method Details

#set_site_idObject



160
161
162
# File 'lib/zena/use/scope_index.rb', line 160

def set_site_id
  self[:site_id] = current_site.id
end

#should_clear_group?(group_key, deleted_node) ⇒ Boolean

Return true if the indices from the given group should be cleared when the given node is deleted.

Returns:

  • (Boolean)


156
157
158
# File 'lib/zena/use/scope_index.rb', line 156

def should_clear_group?(group_key, deleted_node)
  deleted_node.id == self["#{group_key}_id"].to_i
end

#should_update_group?(group_key, node) ⇒ Boolean

Return true if the indices from the given group should be altered by the node.

Returns:

  • (Boolean)


150
151
152
# File 'lib/zena/use/scope_index.rb', line 150

def should_update_group?(group_key, node)
  node.id >= self["#{group_key}_id"].to_i
end

#update_after_destroy(deleted_node, keys) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/zena/use/scope_index.rb', line 132

def update_after_destroy(deleted_node, keys)
  groups = self.class.groups
  attrs  = {}
  keys.each do |group_key|
    next unless list = groups[group_key]
    next unless should_clear_group?(group_key, deleted_node)
    list.each do |key|
      attrs["#{group_key}_#{key}"] = nil
    end
  end

  if !attrs.empty?
    self.attributes = attrs
    save
  end
end

#update_with(node, keys, force_create = false) ⇒ Object

The given node has been updated in the owner’s project. Update index entries with this node’s content if necessary.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/zena/use/scope_index.rb', line 110

def update_with(node, keys, force_create = false)
  attrs = {}
  prop_column_names = node.schema.column_names
  groups = self.class.groups
  keys.each do |group_key|
    next unless list = groups[group_key]
    next unless should_update_group?(group_key, node)
    list.each do |key|
      if prop_column_names.include?(key)
        attrs["#{group_key}_#{key}"] = node.prop[key]
      elsif node.respond_to?(key)
        attrs["#{group_key}_#{key}"] = node.send(key)
      end
    end
  end

  if !attrs.empty? || force_create
    self.attributes = attrs
    save
  end
end