#コマ
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
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 => {:both => false}
validates :publish, :presence => true, :numericality => true
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
return false unless reader_role_check(roles)
end
return true if self.own?(roles)
+ return true if self.new_record?
self.publish?
end
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 = {}
+ {
+ :id => self.field_tag_id(column), :panel_id => self.tag_panel_id
+ }.merge(opt)
+ end
+
+ def select_tag_attributes(selected, column, opt = {})
+ [
+ :last, :first,
+ {: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
+ 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
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
- {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloon => {}, :speech => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- },
+ r = {
:author => {}
}
+ self.each_element_classes do |e|
+ r.merge!(e.list_opt_for_panel)
+ end
+ r
end
def self.show rid, roles
end
def self.show_opt
- {:include => {
- :panel_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :speech_balloons => {:balloon => {}, :speech => {}},
- :ground_pictures => {
- :picture => {:artist => {}, :license => {}}
- },
- :ground_colors => {
- },
+ r = {
:author => {}
- }}
+ }
+ self.each_element_classes do |e|
+ r.merge!(e.show_opt_for_panel)
+ end
+ {:include => r}
end
def parts_element
- ((self.panel_pictures || []) + (self.speech_balloons || []) + (self.ground_colors || []) + (self.ground_pictures || [])).compact
- end
-
- def parts
- res = []
- self.parts_element.each do |e|
- res[e.t] = e
+ r = []
+ self.class.each_element_class_names do |k|
+ r += (self.elements_by_class_name(k) || [])
end
- res
+ r
end
def zorderd_elements
end
def panel_elements
- parts
- end
-
- @@elm_json_opt = {
- 'PanelPicture' => {
- :picture => {:artist => {}, :license => {}}
- },
- 'SpeechBalloon' => {:balloon => {}, :speech => {}},
- 'GroundPicture' => {
- :picture => {:artist => {}, :license => {}}
- },
- 'GroundColor' => {
- },
- }
-
- def self.elm_json_opt e
- @@elm_json_opt[e.class.to_s]
+ 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 => Panel.elm_json_opt(e)})
+ JSON.parse e.to_json({:include => e.class.json_opt_for_panel})
}
end
end
def new_t
- self.parts.size
+ self.panel_elements.size
end
def new_z
- self.parts.size + 1
+ self.panel_elements.size + 1
end
def scenario
def licensed_pictures
r = {}
- ((self.panel_pictures || []) + (self.ground_pictures || [])).compact.each do |elm|
+ self.panel_elements.each do |elm|
+ next unless elm.class.has_picture?
r[elm.picture_id] = elm.picture unless r[elm.picture_id]
end
r
end
def validate_serial_list
+ l = []
+ self.class.each_element_class_names do |k|
+ l << self.elements_by_class_name(k)
+ end
[
- {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :t, :offset => 0},
- {:elements => [self.panel_pictures, self.speech_balloons, self.ground_colors, self.ground_pictures], :name => :z, :offset => 1}
+ {:elements => l, :name => :t, :offset => 0},
+ {:elements => l, :name => :z, :offset => 1}
]
end
def validate_child
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, au
if attr == false
self.errors.add :base, I18n.t('errors.invalid_json')
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
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.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, au)
+ end
+
def destroy_with_elements
res = false
Panel.transaction do
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})
+ 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|