class GroundColor < ActiveRecord::Base
+ include ElementInspire
belongs_to :panel
belongs_to :color
self.panel.remove_element(self, au)
end
- def copy_attributes
- r = self.attributes
- r.delete 'id'
- r.delete 'panel_id'
- r.delete 'created_at'
- r.delete 'updated_at'
- r
- end
-
- def copy to_panel
- new_ground_color = GroundColor.new self.copy_attributes
- new_ground_color.t = to_panel.new_t
- new_ground_color.z = to_panel.new_z
- new_attr = GroundColor.panelize new_ground_color.copy_attributes
- r = {} #Panel.panelize to_panel
- r.merge! new_attr
- r
- end
-
- def self.panelize ground_colors_attributes
- ground_colors_attributes = [ground_colors_attributes] unless ground_colors_attributes.is_a?(Array)
- hash = {}
- index = 0
- ground_colors_attributes.each do |ground_color_attributes|
- n = if ground_color_attributes['id']
- ground_color_attributes['id'].to_s
- else
- index += 1
- 'new' + index.to_s
- end
- hash[n] = ground_color_attributes
- end
- {self.to_s.tableize + '_attributes' => hash}
- end
-
def scenario
if caption.blank?
''
class GroundPicture < ActiveRecord::Base
+ include ElementInspire
belongs_to :panel
belongs_to :picture
self.panel.remove_element(self, au)
end
- def copy_attributes
- r = self.attributes
- r.delete 'id'
- r.delete 'panel_id'
- r.delete 'created_at'
- r.delete 'updated_at'
- r
- end
-
- def copy to_panel
- new_ground_picture = GroundPicture.new self.copy_attributes
- new_ground_picture.t = to_panel.new_t
- new_ground_picture.z = to_panel.new_z
- new_attr = GroundPicture.panelize new_ground_picture.copy_attributes
- r = {} #Panel.panelize to_panel
- r.merge! new_attr
- r
- end
-
- def self.panelize ground_pictures_attributes
- ground_pictures_attributes = [ground_pictures_attributes] unless ground_pictures_attributes.is_a?(Array)
- hash = {}
- index = 0
- ground_pictures_attributes.each do |ground_picture_attributes|
- n = if ground_picture_attributes['id']
- ground_picture_attributes['id'].to_s
- else
- index += 1
- 'new' + index.to_s
- end
- hash[n] = ground_picture_attributes
- end
- {self.to_s.tableize + '_attributes' => hash}
- end
-
def scenario
if caption.blank?
''
end
def parts
- res = []
- self.parts_element.each do |e|
- res[e.t] = e
- end
- res
end
def zorderd_elements
end
def panel_elements
- parts
+ res = []
+ self.parts_element.each do |e|
+ res[e.t] = e
+ end
+ res
end
@@elm_json_opt = {
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
res
end
- def copy au
- attr = self.attributes
- attr.delete 'id'
- self.panel_elements.each_with_index do |elm, index|
- elm_attr = elm.attributes
- elm_attr.delete 'id'
- elm_attr.delete 'panel_id'
- elm_attr.delete 'created_at'
- elm_attr.delete 'updated_at'
- case elm.class.to_s
- when 'PanelPicture'
- when 'SpeechBalloon'
- balloon_attr = elm.balloon.attributes
- balloon_attr.delete 'id'
- balloon_attr.delete 'speech_balloon_id'
- elm_attr['balloon_attributes'] = balloon_attr
- speech_attr = elm.speech.attributes
- speech_attr.delete 'id'
- speech_attr.delete 'speech_balloon_id'
- elm_attr['speech_attributes'] = speech_attr
- when 'GroundPicture'
- when 'GroundColor'
- end
- attr[elm.class.to_s.tableize + '_attributes'] ||= {}
- attr[elm.class.to_s.tableize + '_attributes'][index] = elm_attr
- 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
class PanelPicture < ActiveRecord::Base
+ include ElementInspire
belongs_to :panel
belongs_to :picture
self.panel.remove_element(self, au)
end
- def copy_attributes
- r = self.attributes
- r.delete 'id'
- r.delete 'panel_id'
- r.delete 'created_at'
- r.delete 'updated_at'
- r
- end
-
- def copy to_panel
- new_panel_picture = PanelPicture.new self.copy_attributes
- new_panel_picture.t = to_panel.new_t
- new_panel_picture.z = to_panel.new_z
- new_attr = PanelPicture.panelize new_panel_picture.copy_attributes
- r = {} #Panel.panelize to_panel
- r.merge! new_attr
- r
- end
-
- def self.panelize panel_pictures_attributes
- panel_pictures_attributes = [panel_pictures_attributes] unless panel_pictures_attributes.is_a?(Array)
- hash = {}
- index = 0
- panel_pictures_attributes.each do |panel_picture_attributes|
- n = if panel_picture_attributes['id']
- panel_picture_attributes['id'].to_s
- else
- index += 1
- 'new' + index.to_s
- end
- hash[n] = panel_picture_attributes
- end
- {self.to_s.tableize + '_attributes' => hash}
- end
-
def scenario
if caption.blank?
''
class SpeechBalloon < ActiveRecord::Base
+ include ElementInspire
has_one :balloon, :dependent => :destroy
has_one :speech, :dependent => :destroy
belongs_to :speech_balloon_template
self.panel.remove_element(self, au)
end
- def copy_attributes
- r = self.attributes
- r.delete 'id'
- r.delete 'panel_id'
- r.delete 'created_at'
- r.delete 'updated_at'
- new_balloon_attr = self.balloon ? Balloon.panelize(self.balloon.copy_attributes) : {}
- new_speech_attr = self.speech ? Speech.panelize(self.speech.copy_attributes) : {}
- r.merge! new_balloon_attr
- r.merge! new_speech_attr
- r
- end
-
- def copy to_panel
- new_speech_balloon = SpeechBalloon.new self.copy_attributes
- new_speech_balloon.t = to_panel.new_t
- new_speech_balloon.z = to_panel.new_z
- new_attr = SpeechBalloon.panelize new_speech_balloon.copy_attributes
- r = {} #Panel.panelize to_panel
- r.merge! new_attr
- r
- end
-
- def self.panelize speech_balloons_attributes
- speech_balloons_attributes = [speech_balloons_attributes] unless speech_balloons_attributes.is_a?(Array)
- hash = {}
- index = 0
- speech_balloons_attributes.each do |speech_balloon_attributes|
- n = if speech_balloon_attributes['id']
- speech_balloon_attributes['id'].to_s
- else
- index += 1
- 'new' + index.to_s
- end
- hash[n] = speech_balloon_attributes
- end
- {self.to_s.tableize + '_attributes' => hash}
+ def parts
+ @parts ||= [self.balloon, self.speech]
end
def scenario
<h2><%= t('panels.show.copy') -%></h2>
<%= form_for(Panel.new, :html => {:jqform => 'pettanr-panel-form'}) do |f| %>
- <%= hidden_field_tag "json", @panel.copy(@author).to_json %>
+ <%= hidden_field_tag "json", @panel.copy().to_json %>
<div>
<%= submit_tag t('panels.show.inspire') -%>
</div>
require 'ar_helper'
require 'import_result'
require 'common'
+require 'inspire'
--- /dev/null
+module ElementInspire
+ def self.included(base)
+ base.extend(ClassMethods)
+ base.__send__ :include, InstanceMethods
+ end
+
+ module ClassMethods
+ def panelize elements_attributes
+ elements_attributes = [elements_attributes] unless elements_attributes.is_a?(Array)
+ hash = {}
+ index = 0
+ elements_attributes.each do |element_attributes|
+ hash[self.to_s.tableize + '_attributes'] ||= {}
+ n = if element_attributes['id']
+ element_attributes['id'].to_s
+ else
+ index += 1
+ 'new' + index.to_s
+ end
+ hash[self.to_s.tableize + '_attributes'][n] = element_attributes
+ end
+ hash
+ end
+ end
+
+ module InstanceMethods
+ private
+
+ public
+
+ def parts
+ @parts ||= []
+ end
+
+ def copy_attributes
+ r = self.attributes
+ r.delete 'id'
+ r.delete 'panel_id'
+ r.delete 'created_at'
+ r.delete 'updated_at'
+ self.parts.each do |part|
+ new_part_attr = part ? part.class.panelize(part.copy_attributes) : {}
+ r.merge! new_part_attr
+ end
+ r
+ end
+
+ def copy to_panel
+ new_element = self.class.new self.copy_attributes
+ new_element.t = to_panel.new_t
+ new_element.z = to_panel.new_z
+ new_attr = new_element.class.panelize new_element.copy_attributes
+ r = {} #Panel.panelize to_panel
+ r.merge! new_attr
+ r
+ end
+
+ def copy_all index
+ new_element = self.class.new self.copy_attributes
+ new_attr = new_element.class.panelize(new_element.copy_attributes, index)
+ r = {} #Panel.panelize to_panel
+ r.merge! new_attr
+ r
+ end
+
+ end
+
+end
+
end\r
describe 'コマ部品に於いて' do\r
before do\r
- #コマを作成しておく。\r
- @panel = FactoryGirl.create :panel, :author_id => @author.id\r
- @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height\r
- @sb = @panel.speech_balloons.create(\r
- FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)\r
- )\r
- @gc = @panel.ground_colors.create(\r
- FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :t => 2)\r
- )\r
- @gp = @panel.ground_pictures.create(\r
- FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :t => 3)\r
- )\r
- @panel.reload\r
end\r
context 'つつがなく終わるとき' do\r
it 'コマ部品集合を利用している' do\r
- Panel.any_instance.stub(:parts_element).with(any_args).and_return([])\r
- Panel.any_instance.should_receive(:parts_element).with(any_args).exactly(1)\r
r = @panel.parts\r
end\r
end\r
it 'リストを返している' do\r
r = @panel.parts\r
- r.is_a?(Array).should be_true\r
- r.size.should eq 4\r
- end\r
- it 'tでソートしている' do\r
- r = @panel.parts\r
- r[0].should eq @sb\r
- r[1].should eq @pp\r
- r[2].should eq @gc\r
- r[3].should eq @gp\r
- end\r
- context 'さらに末尾にフキダシを追加したとき' do\r
- before do\r
- @sb2 = @panel.speech_balloons.create(\r
- FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4)\r
- )\r
- @panel.reload\r
- end\r
- it 'tでソートしている' do\r
- r = @panel.parts\r
- r[0].should eq @sb\r
- r[1].should eq @pp\r
- r[2].should eq @gc\r
- r[3].should eq @gp\r
- r[4].should eq @sb2\r
- end\r
end\r
end\r
\r
r[2].should eq @gc\r
r[3].should eq @gp\r
end\r
+ context 'さらに末尾にフキダシを追加したとき' do\r
+ before do\r
+ @sb2 = @panel.speech_balloons.create(\r
+ FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 4)\r
+ )\r
+ @panel.reload\r
+ end\r
+ it 'tでソートしている' do\r
+ r = @panel.panel_elements\r
+ r[0].should eq @sb\r
+ r[1].should eq @pp\r
+ r[2].should eq @gc\r
+ r[3].should eq @gp\r
+ r[4].should eq @sb2\r
+ end\r
+ end\r
end\r
describe 'コマ要素のjson出力に於いて' do\r
before do\r