X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fpanel.rb;h=ad20b014db00fb2960b05c5b1db04fdb35ed3f8e;hb=467daaf74bbf62a7ad43d7af7fd6326bfacd9010;hp=4d1b9860c217f0798d41ff1f991679170896554e;hpb=26f1bb0f8cf40ebd82495945236f83542dfe21ed;p=pettanr%2Fpettanr.git diff --git a/app/models/panel.rb b/app/models/panel.rb index 4d1b9860..ad20b014 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -1,5 +1,6 @@ #コマ -class Panel < Pettanr::Content +class Panel < Peta::Root + load_manifest belongs_to :author has_many :scroll_panels has_many :sheet_panels @@ -18,31 +19,6 @@ class Panel < Pettanr::Content validates :author_id, :presence => true, :numericality => true, :existence => {:both => false} validates :publish, :presence => true, :numericality => true - def self.valid_encode_columns - super + ['caption'] - end - - def self.each_element_class_names - Pettanr::Application.manifest.system_resources.elements.each do |k, n| - yield k - end - end - - def self.class_name_to_class k - Object.const_get k - end - - def self.each_element_classes - self.each_element_class_names do |k| - e = self.class_name_to_class k - yield e - end - end - - def elements_by_class_name class_name - self.__send__ class_name.tableize - end - def supply_default self.border = 2 self.publish = 0 @@ -54,9 +30,15 @@ class Panel < Pettanr::Content end def visible? operators - return true if super - return true if self.new_record? - self.publish? + case super + when nil # super return + return true + when false + return false + else + return true if self.new_record? + self.publish? + end end def usable? operators @@ -67,318 +49,62 @@ class Panel < Pettanr::Content self.publish > 0 end - def tag_id c = nil - 'panel' + self.tag_panel_id + c.to_s - end - - def tag_panel_id - self.new_record? ? '0' : self.id.to_s - end - - def field_tag_id f - self.tag_id + f.to_s - end - - def tag_attributes column = nil, opt = {} + def style { - :id => self.field_tag_id(column), :panel_id => self.tag_panel_id - }.merge(opt) - end - - def select_tag_attributes(selected, column, opt = {}) - [ - {:html => {:selected => selected}}, - self.field_tag_attributes(column, opt) - ] - end - - def field_tag_attributes column, opt = {} - self.tag_attributes(column).merge( - {:column => column} - ).merge(opt) - end - - def tag_attr column = nil, opt = {} - self.tag_attributes(column, opt).to_attr + 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px', + 'border-style' => 'solid', 'border-width' => self.border.to_s + 'px', + 'border-color' => 'black', 'background-color' => 'white' + } end - def field_tag_attr column, no_attr, opt = {} - self.field_tag_attributes(column, no_attr, opt).to_attr + # ground_picture element template + def style_wh + { + 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px' + } end - def render_count - @render_count ||= 1 - end - - def rendered - @render_count = render_count + 1 - end - - def self.list_where + def self.public_list_where 'panels.publish > 0' end - def self.list_order - 'panels.updated_at desc' - end - - def self.list_opt - r = { - :author => {} - } - self.each_element_classes do |e| - r.merge!(e.list_opt_for_panel) - end - r - end - def self.show_opt r = { :author => {} } - self.each_element_classes do |e| - r.merge!(e.show_opt_for_panel) + self.child_models.each do |child_model| + r.merge!(child_model.show_opt_for_panel) end {:include => r} end - def parts_element - r = [] - self.class.each_element_class_names do |k| - r += (self.elements_by_class_name(k) || []) - end - r - end - - def zorderd_elements - res = [] - self.parts_element.each do |e| - res[e.z-1] = e - end - res - end - - def panel_elements - res = [] - self.parts_element.each do |e| - res[e.t] = e - end - res - end - - def elements - self.panel_elements.map {|e| - #(-_-;)<... kore wa hidoi - JSON.parse e.to_json({:include => e.class.json_opt_for_panel}) - } - end - - def panel_elements_as_json - self.to_json({:include => {:author => {}}, :methods => :elements}) - end - - def self.list_as_json_text ary - '[' + ary.map {|i| i.panel_elements_as_json }.join(',') + ']' - end - - def new_t - self.panel_elements.size - end - - def new_z - self.panel_elements.size + 1 - end - def scenario - panel_elements.map { |e| + scenario_elements.map { |e| e.scenario }.join end def plain_scenario - panel_elements.map { |e| + scenario_elements.map { |e| e.plain_scenario }.join end - def licensed_pictures + def self.licensed_pictures panels r = {} - self.panel_elements.each do |elm| - next unless elm.class.has_picture? - r[elm.picture_id] = elm.picture unless r[elm.picture_id] + panels.each do |panel| + r.merge!(panel.licensed_pictures) end r end - def self.visible_count - Panel.count - end - - def self.collect_element_value elements, name - elements.map {|e| - e.map {|o| - if o['_destroy'] or o[:_destroy] - nil - else - o[name] - end - }.compact - }.flatten - end - - def self.validate_serial ary, offset = 0 - i = offset - ary.compact.sort.each do |n| - break false unless n == i - i += 1 - end - ary.compact.size == i - offset - end - - def self.validate_element_serial elements, name, offset = 0 - Panel.validate_serial(Panel.collect_element_value(elements, name), offset) - end - - def self.validate_elements_serial c - c.map {|conf| - Panel.validate_element_serial(conf[:elements], conf[:name], conf[:offset]) ? nil : false - }.compact.empty? - end - - def validate_serial_list - l = [] - self.class.each_element_class_names do |k| - l << self.elements_by_class_name(k) - end - [ - {:elements => l, :name => :t, :offset => 0}, - {:elements => l, :name => :z, :offset => 1} - ] - end - def validate_child -# r1 = Panel.validate_elements_id validate_id_list - Panel.validate_elements_serial validate_serial_list - end - - def boost - @new_element_index = 0 - self.panel_elements.each do |elm| - if elm.new_record? - elm.new_index = @new_element_index - @new_element_index += 1 - end - end - end - - def store attr, operators - if attr == false - self.errors.add :base, I18n.t('errors.invalid_json') - return false - end - self.attributes = attr - self.overwrite operators - res = false - Panel.transaction do - self.panel_elements.each do |elm| - elm.new_panel = self - elm.boost - end -#self.publish = nil - res = self.save - unless validate_child - res = false - self.errors.add :base, I18n.t('errors.invalid_t') - raise ActiveRecord::Rollback - end - end - res - end - - def remove_element target, operators - ct = target.t - cz = target.z - panel_attributes = {} - self.panel_elements.each do |elm| - attr = elm.attributes - if elm == target - attr['_destroy'] = true - end - if elm.t > ct - attr['t'] -= 1 - end - if elm.z > cz - attr['z'] -= 1 - end - panel_attributes[elm.class.to_s.tableize + '_attributes'] ||= {} - panel_attributes[elm.class.to_s.tableize + '_attributes'][elm.id] = attr - end - self.store(panel_attributes, operators) - end - - def destroy_with_elements - res = false - Panel.transaction do - self.parts_element.each do |element| - raise ActiveRecord::Rollback unless element.destroy - end - raise ActiveRecord::Rollback unless self.destroy - res = true - end - res - end - - def copy - attr = self.copy_attributes - Panel.each_element_class_names do |n| - attr.merge! Panel.class_name_to_class(n).panelize(self.elements_by_class_name(n).map {|elm| elm.copy_attributes}) + def licensed_pictures + r = {} + self.scenario_elements.each do |elm| + next unless elm.class.has_picture? + r[elm.picture_id] = elm.picture unless r[elm.picture_id] end - attr - end - - def copy_attributes - r = self.attributes - r.delete 'id' - r.delete 'author_id' - r.delete 'created_at' - r.delete 'updated_at' - r - end - - def self.panelize panel - attr = panel.attributes - attr.delete 'id' - attr - end - - -=begin - def self.validate_id ary, pid - ary.map {|v| - if pid - (v == pid or v == nil) ? nil : false - else - v ? false : nil - end - }.compact.empty? - end - - def self.validate_element_id elements, name, pid - Panel.validate_id(Panel.collect_element_value(elements, name), pid) - end - - def self.validate_elements_id c - c.map {|conf| - Panel.validate_element_id(conf[:elements], conf[:name], conf[:parent_id]) ? nil : false - }.compact.empty? - end - - def validate_id_list - r = self.speech_balloons.map {|sb| - {:elements => [sb.speeches, sb.balloons], :name => :speech_balloon_id, :parent_id => sb.id} - } - r.unshift({:elements => [self.panel_pictures, self.speech_balloons], :name => :panel_id, :parent_id => self.id}) r end -=end end