module Peta
class Item < ActiveRecord::Base
self.abstract_class = true
+ attr :boosters
+
+ # Dynamic ClassMethods
+
+ def self._valid_encode_columns
+ self.my_manifest.valid_encode_columns
+ end
+
+ def self.load_manifest
+ return nil unless self.my_manifest
+ r = self._valid_encode_columns
+ define_singleton_method("valid_encode_columns") do
+ r
+ end
+ r = self.my_manifest.child_models
+ define_singleton_method("child_models") do
+ r
+ end
+ r = self.my_manifest.child_element_names
+ define_singleton_method("child_element_names") do
+ r
+ end
+ # Instance Methods
+ end
+
# ClassMethods
# class_name
# table_name
before_validation :valid_encode
+ def self.my_peta
+ Manifest.manifest.items[self.item_name]
+ end
+
+ def self.my_manifest
+ Manifest.manifest.models[self.item_name]
+ end
+
def self.singular
self.to_s
end
false
end
+ def self.element?
+ false
+ end
+
+ def self.root?
+ false
+ end
+
+ def self.parent_model
+ nil
+ end
+
def self.path_name with_engine = false
self.plural.underscore
end
- def self.valid_encode_columns
- []
+ def self.find_boost_name column_name
+ self.my_peta.find_boost_name column_name
+ end
+
+ def self.extend_column? column_name
+ self.my_peta.extend_column? column_name
end
def self.visible_count_options
opt = {}
opt.merge!(self.show_opt)
item = self.find(item_id, opt)
+ item.boosts 'read'
raise ActiveRecord::Forbidden unless item.visible?(operators)
item
end
{}
end
- def self.visible_count
- self.count self.visible_count_options
- end
-
- def self.visible_count_options
- []
+ def self.fold_extend_settings params
+ self.my_peta.boost.each do |name, manifest|
+ my_settings = params[self.item_name][manifest.settings_column_name]
+ if my_settings.is_a?(Hash)
+ params[self.item_name][manifest.settings_column_name] = my_settings.to_json
+ end
+ end
+ self.child_models.each do |child_model|
+ child_model.fold_extend_settings params
+ end
end
#InstanceMethods
self.class.table_name
end
- def extend_column
- nil
+ def path_name with_engine = false
+ self.class.path_name(with_engine)
end
- def extend_item_name
- self.extend_column ? self.attributes[self.extend_column] : self.item_name
+ def form_template with_engine = false
+ self.path_name(with_engine) + '/form'
end
def form_name
- self.extend_item_name
+ self.extend_column ? self.attributes[self.extend_column] : self.item_name
end
def valid_encode
end
end
+ def boosts level
+ self.class.my_peta.boost.each do |boost_name, boost_manifest|
+ next if level == 'read' and boost_manifest.level == 'post'
+ self.boost boost_manifest
+ end
+ end
+
+ def boost boost_manifest
+ @boosters ||= {}
+ @boosters[boost_manifest.name] = Locmare::Booster.new(boost_manifest, self)
+ end
+
+ def boosters
+ @boosters
+ end
+
+ def extend_column? column_name
+ self.class.extend_column? column_name
+ end
+
def supply_default
+ self.class.my_peta.supply_defaults self
end
def overwrite
true
end
- def dom_id_item
- self.new_record? ? '0' : self.id.to_s
+ def editize?
+ self.respond_to? :editor
end
- def tag_item_id c = nil
- self.item_name + self.item_id + c.to_s
+ def dom_id
+ self.id.to_s
end
- def dom_id_item_field field_name
- self.tag_item_id + field_name.to_s
+ def dom_pool_type
+ self.new_record? ? 'stored' : 'new'
end
- def path_name with_engine = false
- self.class.path_name(with_engine)
- end
-
- def form_template with_engine = false
- self.path_name(with_engine) + '/form'
- end
-
- def tag_item_attributes column = nil, opt = {}
- {
- :id => self.field_tag_id(column), :panel_id => self.tag_panel_id,
- :element_id => self.tag_element_id, :element_type => self.tag_element_type
- }.merge(opt)
- end
-
- def tag_new_index
+ def merge_dom_item_id attr, name = nil
+ if self.editize?
+ attr.merge({'id' => self.dom_item_id(name)})
+ else
+ attr
end
-
- def field_tag_attributes column, no_attr, opt = {}
- self.tag_attributes(column).merge(
- {:column => column, :new_index => self.tag_new_index, :no_attr => no_attr}
- ).merge(opt)
- end
-
- #render element by body
- def any_tag_attributes name = nil, opt = {}
- r = self.tag_attributes(name)
- r.merge!(
- {:new_index => self.tag_new_index}
- ) if self.new_index
- r.merge(opt)
- end
-
- def select_tag_attributes(selected, column, no_attr)
- [
- :last, :first,
- {:html => {:selected => selected}},
- self.field_tag_attributes(column, no_attr)
- ]
end
- def tag_attr column = nil, opt = {}
- self.tag_attributes(column, opt).to_attr
+ def tag_attributes name = nil, opt = {}
+ r = {
+ 'data-pool_type' => self.dom_pool_type, 'data-id' => self.dom_id,
+ 'data-item_name' => self.item_name
+ }
+ r = self.merge_dom_item_id r, name
+ r.merge!(opt)
+ r
end
- def field_tag_attr column, no_attr, opt = {}
- self.field_tag_attributes(column, no_attr, opt).to_attr
+ def field_tag_attributes column_name, opt = {}
+ self.tag_attributes(column_name, opt).merge(
+ {'data-column_name' => column_name}
+ )
end
- def any_tag_attr name = nil, opt = {}
- self.any_tag_attributes(name, opt).to_attr
+ def post_attribute_key
+ self.dom_id
end
end