#コマ
class Panel < ActiveRecord::Base
belongs_to :author
- belongs_to :resource_picture
- has_many :stories
+ has_many :scroll_panels
+ has_many :sheet_panels
has_many :panel_pictures, :dependent => :destroy
has_many :speech_balloons, :dependent => :destroy
has_many :ground_pictures, :dependent => :destroy
has_many :ground_colors, :dependent => :destroy
- has_many :panel_colors, :dependent => :destroy
accepts_nested_attributes_for :panel_pictures, :allow_destroy => true
accepts_nested_attributes_for :speech_balloons, :allow_destroy => true
accepts_nested_attributes_for :ground_pictures, :allow_destroy => true
accepts_nested_attributes_for :ground_colors, :allow_destroy => true
- accepts_nested_attributes_for :panel_colors, :allow_destroy => true
validates :width, :presence => true, :numericality => true, :natural_number => true
validates :height, :presence => true, :numericality => true, :natural_number => true
validates :border, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
- validates :x, :numericality => {:allow_blank => true}
- validates :y, :numericality => {:allow_blank => true}
- validates :z, :numericality => {:allow_blank => true, :greater_than => 0}
- validates :author_id, :presence => true, :numericality => true, :existence => true
+ validates :author_id, :presence => true, :numericality => true, :existence => {:both => false}
+ validates :publish, :presence => true, :numericality => true
+
+ before_validation :valid_encode
+
+ def valid_encode
+ ['caption'].each do |a|
+ next if attributes[a] == nil
+ raise Pettanr::BadRequest unless attributes[a].valid_encoding?
+ end
+ 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
end
def overwrite au
- return false unless au
self.author_id = au.id
end
- def self.collect_element_value elements, name, ex_nil = false
- e = elements.map {|e|
- e.map {|o|
- o[name]
- }
- }.flatten
- e = e.compact if ex_nil
- e
+ def own? roles
+ roles = [roles] unless roles.respond_to?(:each)
+ au = Panel.get_author_from_roles roles
+ return false unless au
+ self.author_id == au.id
end
- 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?
+ def visible? roles
+ if MagicNumber['run_mode'] == 0
+ return false unless guest_role_check(roles)
+ else
+ return false unless reader_role_check(roles)
+ end
+ return true if self.own?(roles)
+ return true if self.new_record?
+ self.publish?
end
- def self.validate_element_id elements, name, pid
- Panel.validate_id(Panel.collect_element_value(elements, name), pid)
+ def usable? au
+ visible? au
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?
+ def publish?
+ self.publish > 0
end
- def self.validate_t ary
- i = 0
- ary.compact.sort.each do |t|
- break false unless t == i
- i += 1
- end
- ary.compact.size == i
+ def tag_id c = nil
+ 'panel' + self.tag_panel_id + c.to_s
end
- def self.validate_element_t elements, name
- Panel.validate_t(Panel.collect_element_value(elements, name, true))
+ def tag_panel_id
+ self.new_record? ? '0' : self.id.to_s
end
- def self.validate_elements_t c
- c.map {|conf|
- Panel.validate_element_t(conf[:elements], conf[:name]) ? nil : false
- }.compact.empty?
+ def field_tag_id f
+ self.tag_id + f.to_s
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
+ def tag_attributes column = nil, opt = {}
+ {
+ :id => self.field_tag_id(column), :panel_id => self.tag_panel_id
+ }.merge(opt)
end
- def validate_child
- r1 = Panel.validate_elements_id validate_id_list
- r2 = Panel.validate_elements_t [
- {:elements => [self.panel_pictures, self.speech_balloons], :name => :t}
+ def select_tag_attributes(selected, column, opt = {})
+ [
+ :last, :first,
+ {:html => {:selected => selected}},
+ self.field_tag_attributes(column, opt)
]
- r1 and r2
end
- def store
- res = false
- Panel.transaction do
- unless validate_child
- self.errors.add :base , 'invalid time'
- raise ActiveRecord::Rollback
- end
- res = self.save
- end
- res
+ 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
+ end
+
+ def field_tag_attr column, no_attr, opt = {}
+ self.field_tag_attributes(column, no_attr, opt).to_attr
+ end
+
+ def render_count
+ @render_count ||= 1
+ end
+
+ def rendered
+ @render_count = render_count + 1
+ end
+
def self.default_page_size
25
end
page_size
end
- def self.offset cnt, prm = nil
- offset = prm.to_i
- offset = cnt - 1 if offset >= cnt
- offset = cnt - offset.abs if offset < 0
- offset = 0 if offset < 0
- offset
+ def self.list_where
+ 'panels.publish > 0'
end
- def self.list opt = {}, page = 1, page_size = self.default_page_size
- opt.merge!(self.list_opt) unless opt[:include]
- opt.merge!({:conditions => 'panels.publish > 0', :order => 'panels.updated_at desc', :limit => page_size, :offset => (page -1) * page_size})
- Panel.find(:all, opt)
+ def self.mylist_where au
+ ['panels.author_id = ?', au.id]
end
- def self.list_opt
- {:include => {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloons => {}, :speeches => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- :color => {}
- },
- :panel_colors => {
- },
- :author => {}
- }}
- end
-
- def self.list_json_opt
- {:include => {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloons => {}, :speeches => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- :color => {}
- },
- :panel_colors => {
- },
- :author => {}
- }}
+ def self.himlist_where au
+ ['panels.author_id = ? and panels.publish > 0', au.id]
end
- def self.mylist au, opt = {}, page = 1, panel_page_size = Author.default_panel_page_size
- opt.merge!(self.list_opt) unless opt[:include]
- opt.merge!({:conditions => ['panels.author_id = ?', au.id], :order => 'panels.updated_at desc', :limit => panel_page_size, :offset => (page -1) * page_size})
- Panel.find(:all, opt)
+ def self.list page = 1, page_size = self.default_page_size
+ Panel.where(self.list_where()).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
end
- def self.show rid, au, opt = {}
- r = Panel.find(rid, :include => self.show_include_opt(opt))
- raise ActiveRecord::Forbidden unless r.visible?(au)
- r
+ def self.mylist au, page = 1, page_size = Author.default_panel_page_size
+ Panel.where(self.mylist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
end
- def self.edit rid, au, opt = {}
- r = Panel.find(rid, :include => self.show_include_opt(opt))
- raise ActiveRecord::Forbidden unless r.own?(au)
- r
+ def self.himlist au, page = 1, page_size = Author.default_panel_page_size
+ Panel.where(self.himlist_where(au)).includes(Panel.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_paginate page = 1, page_size = self.default_page_size
+ Kaminari.paginate_array(Array.new(Panel.where(self.list_where()).count, nil)).page(page).per(page_size)
+ end
+
+ def self.mylist_paginate au, page = 1, page_size = Author.default_panel_page_size
+ Kaminari.paginate_array(Array.new(Panel.where(self.mylist_where(au)).count, nil)).page(page).per(page_size)
end
- def self.show_include_opt opt = {}
- res = {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloons => {}, :speeches => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- :color => {}
- },
- :panel_colors => {
- },
+ def self.himlist_paginate au, page = 1, page_size = Author.default_panel_page_size
+ Kaminari.paginate_array(Array.new(Panel.where(self.himlist_where(au)).count, nil)).page(page).per(page_size)
+ end
+
+ def self.list_by_scroll scroll_id, roles, page = 1, page_size = self.default_page_size
+ self.where(ScrollPanel.list_by_scroll_where(scroll_id)).includes(
+ {:scroll_panels => {}}
+ ).order('scroll_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_by_sheet sheet_id, roles, page = 1, page_size = self.default_page_size
+ self.where(SheetPanel.list_by_sheet_where(sheet_id)).includes(
+ {:sheet_panels => {}}
+ ).order('sheet_panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_by_speech_balloon_template speech_balloon_template_id, roles, page = 1, page_size = self.default_page_size
+ self.where(SpeechBalloon.list_by_speech_balloon_template_where(speech_balloon_template_id)).includes(
+ {:speech_balloons => {}}
+ ).order('speech_balloons.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_by_author_where author_id
+ ['panels.author_id = ?', author_id]
+ end
+
+ def self.list_by_author author_id, roles, page = 1, page_size = self.default_page_size
+ self.where(self.list_by_author_where(author_id)).includes(self.list_opt).order('panels.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_opt
+ r = {
:author => {}
}
- res.merge!(opt[:include]) if opt[:include]
+ self.each_element_classes do |e|
+ r.merge!(e.list_opt_for_panel)
+ end
+ r
+ end
+
+ def self.show rid, roles
+ opt = {}
+ opt.merge!(Panel.show_opt)
+ res = Panel.find(rid, opt)
+ raise ActiveRecord::Forbidden unless res.visible?(roles)
+ res
+ end
+
+ def self.edit rid, au
+ opt = {}
+ opt.merge!(Panel.show_opt)
+ res = Panel.find(rid, opt)
+ raise ActiveRecord::Forbidden unless res.own?(au)
res
end
- def self.show_json_include_opt
- {:include => {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloons => {}, :speeches => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- :color => {}
- },
- :panel_colors => {
- },
+ def self.show_opt
+ r = {
:author => {}
- }}
+ }
+ self.each_element_classes do |e|
+ r.merge!(e.show_opt_for_panel)
+ end
+ {:include => r}
end
- def visible? au
- return false unless au
- self.own?(au) or self.publish?
+ 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 own? author
- return false unless author
- self.author_id == author.id
+ def panel_elements
+ res = []
+ self.parts_element.each do |e|
+ res[e.t] = e
+ end
+ res
end
- def usable? au
- visible? au
+ def elements
+ self.panel_elements.map {|e|
+ #(-_-;)<... kore wa hidoi
+ JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
+ }
end
- def publish?
- self.publish > 0
+ def panel_elements_as_json
+ self.to_json({:include => {:author => {}}, :methods => :elements})
end
- def sort_by_time
- pe = []
- self.panel_pictures.each do |picture|
- pe[picture.t] = picture
- end
- self.speech_balloons.each do |sb|
- pe[sb.t] = sb
+ 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|
+ e.scenario
+ }.join
+ end
+
+ def plain_scenario
+ panel_elements.map { |e|
+ e.plain_scenario
+ }.join
+ end
+
+ def licensed_pictures
+ r = {}
+ self.panel_elements.each do |elm|
+ next unless elm.class.has_picture?
+ r[elm.picture_id] = elm.picture unless r[elm.picture_id]
end
- pe
+ r
end
- def each_ground
- (self.ground_colors || []) + (self.ground_pictures || []) + (self.panel_colors || []).each do |e|
- yield e
+ 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 each_element
- self.sort_by_time.each do |e|
- yield e
+ 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 panel_elements
- res = []
- self.each_element do |elm|
- if elm.kind_of?(PanelPicture)
- res[elm.t] = elm.to_json({:include => {:picture => {:artist => {}, :license => {}}}})
+ 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
- if elm.kind_of?(SpeechBalloon)
- res[elm.t] = elm.to_json({:include => {:balloons => {}, :speeches => {}}})
+ end
+ end
+
+ def store attr, au
+ if attr == false
+ self.errors.add :base, I18n.t('errors.invalid_json')
+ return false
+ end
+ self.attributes = attr
+ self.overwrite au
+ 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
- self.each_ground do |elm|
- if elm.kind_of?(GroundColor)
- res << elm.to_json({:include => :color})
+ res
+ end
+
+ def remove_element target, au
+ 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.kind_of?(GroundPicture)
- res << elm.to_json({:include => {:picture => {:artist => {}, :license => {}}}})
+ if elm.t > ct
+ attr['t'] -= 1
end
- if elm.kind_of?(PanelColor)
- res << elm.to_json({:include => {}})
+ 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, au)
+ 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 to_json_play
- self.to_json :methods => :panel_elements
+ 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})
+ end
+ attr
end
- def self.visible_count
- Panel.count
+ 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