X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=app%2Fmodels%2Fpanel.rb;h=d19cc62e20b2c8dc88d0f24e42de344e34a0105e;hb=17717b9db2cd9f094e4daf7b7a213c838736746d;hp=dd919d0468a251f432db788d2b1880c55379f3d8;hpb=e80acf2cb4f99b007a3a28e50b31931f1304687d;p=pettanr%2Fpettanr.git diff --git a/app/models/panel.rb b/app/models/panel.rb index dd919d04..d19cc62e 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -1,8 +1,9 @@ #コマ -class Panel < ActiveRecord::Base +class Panel < Peta::Root + load_manifest 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 @@ -15,179 +16,84 @@ class Panel < ActiveRecord::Base 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 - 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 supply_default self.border = 2 self.publish = 0 end - def overwrite au - self.author_id = au.id + def overwrite operators + return false unless operators.author + self.author_id = operators.author.id end - 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 visible? roles - if MagicNumber['run_mode'] == 0 - return false unless guest_role_check(roles) + def visible? operators + case super + when nil # super return + return true + when false + return false else - return false unless reader_role_check(roles) + return true if self.new_record? + self.publish? end - return true if self.own?(roles) - self.publish? end - def usable? au - visible? au + def usable? operators + self.visible? operators end def publish? self.publish > 0 end - def tag_id - 'panel' + self.tag_panel_id - 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 field_tree f - 'panels-' + tag_panel_id + '-' + f.to_s - end - - def self.default_page_size - 25 - end - - def self.max_page_size - 100 - end - - def self.page prm = nil - page = prm.to_i - page = 1 if page < 1 - page + def style + { + '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 self.page_size prm = self.default_page_size - page_size = prm.to_i - page_size = self.max_page_size if page_size > self.max_page_size - page_size = self.default_page_size if page_size < 1 - page_size + # ground_picture element template + def style_wh + { + 'width' => self.width.to_s + 'px', 'height' => self.height.to_s + 'px' + } end - def self.list_where + def self.public_list_where 'panels.publish > 0' end - def self.mylist_where au - ['panels.author_id = ?', au.id] - end - - def self.himlist_where au - ['panels.author_id = ? and panels.publish > 0', au.id] - end - - 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.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.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.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) + def self.list_order + 'panels.updated_at desc' end def self.list_opt - { - :panel_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :speech_balloons => {:balloon => {}, :speech => {}}, - :ground_pictures => { - :picture => {:artist => {}, :license => {}} - }, - :ground_colors => { - }, + r = { :author => {} } - 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 + self.child_models.each do |child_model| + r.merge!(child_model.list_opt_for_panel) + end + r 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.child_models.each do |child_model| + r.merge!(child_model.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 + self.elements_items end def zorderd_elements @@ -206,26 +112,10 @@ class Panel < ActiveRecord::Base res 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] - 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 @@ -259,7 +149,8 @@ class Panel < ActiveRecord::Base 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 @@ -302,8 +193,8 @@ class Panel < ActiveRecord::Base def validate_serial_list [ - {: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 => self.elements_items, :name => :t, :offset => 0}, + {:elements => self.elements_items, :name => :z, :offset => 1} ] end def validate_child @@ -311,15 +202,19 @@ class Panel < ActiveRecord::Base Panel.validate_elements_serial validate_serial_list end - def store attr, au + def store attr, operators if attr == false self.errors.add :base, I18n.t('errors.invalid_json') return false end self.attributes = attr - self.overwrite au + self.overwrite operators res = false Panel.transaction do + self.panel_elements.each do |elm| + elm.boost + end +#self.publish = nil res = self.save unless validate_child res = false @@ -330,7 +225,7 @@ class Panel < ActiveRecord::Base res end - def remove_element target, au + def remove_element target, operators ct = target.t cz = target.z panel_attributes = {} @@ -348,7 +243,7 @@ class Panel < ActiveRecord::Base 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) + self.store(panel_attributes, operators) end def destroy_with_elements @@ -363,25 +258,6 @@ class Panel < ActiveRecord::Base res end - def copy - attr = self.copy_attributes - attr.merge! PanelPicture.panelize(self.panel_pictures.map {|elm| elm.copy_attributes}) - attr.merge! SpeechBalloon.panelize(self.speech_balloons.map {|elm| elm.copy_attributes}) - attr.merge! GroundPicture.panelize(self.ground_pictures.map {|elm| elm.copy_attributes}) - attr.merge! GroundColor.panelize(self.ground_colors.map {|elm| elm.copy_attributes}) - p attr - 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'