# GET /stories/new.json
def new
@story = Story.new
- @story.supply_default @author
- @form_opt = {}
+ @story.supply_default
respond_to do |format|
format.html # new.html.erb
- format.js { @form_opt = {:remote => true} ; render action: "new" }
+ format.js
format.json { render json: @story }
end
end
# POST /stories.json
def create
@story = Story.new
- @story.supply_default @author
+ @story.supply_default
@story.attributes = params[:story]
+ @story.overwrite @author
respond_to do |format|
if @story.store
# PUT /stories/1.json
def update
@story = Story.show(params[:id], @author)
- @story.author_id = @author.id
ot = @story.t
@story.attributes = params[:story]
+ @story.overwrite @author
respond_to do |format|
if @story.store ot
format.html { redirect_to action: :show, id: @story.comic_id }
validates :author_id, :presence => true, :numericality => true, :existence => true
validates :t, :presence => true, :numericality => {:greater_than_or_equal_to => 0}
- def supply_default au
+ def supply_default
self.t = nil
+ end
+
+ def overwrite au
return false unless au
self.author_id = au.id
end
--- /dev/null
+<%= form_for(@story) do |f| %>
+ <% if @story.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
+
+ <ul>
+ <% @story.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.hidden_field :comic_id %>
+ </div>
+ <div class="field">
+ <%= f.number_field :t %>
+ </div>
+ <div class="field">
+ <%= f.hidden_field :panel_id %>
+ </div>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
+<%= button_to 'Destroy', @story.panel, confirm: 'Are you sure?', method: :delete %>
--- /dev/null
+<%= form_for(@story) do |f| %>
+ <% if @story.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
+
+ <ul>
+ <% @story.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.number_field :comic_id %>
+ </div>
+ <div class="field">
+ <%= f.number_field :t %>
+ </div>
+ <div class="field">
+ <%= f.number_field :panel_id %>
+ </div>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
-<%= form_for(@story) do |f| %>
- <% if @story.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
-
- <ul>
- <% @story.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div class="field">
- <%= f.hidden_field :comic_id %>
- </div>
- <div class="field">
- <%= f.number_field :t %>
- </div>
- <div class="field">
- <%= f.hidden_field :panel_id %>
- </div>
-
- <div class="actions">
- <%= f.submit %>
- </div>
-<% end %>
-<%= button_to 'Destroy', @story.panel, confirm: 'Are you sure?', method: :delete %>
+<%= render 'editform' %>
-<%= form_for(@story) do |f| %>
- <% if @story.errors.any? %>
- <div id="error_explanation">
- <h2><%= pluralize(@story.errors.count, "error") %> prohibited this comic from being saved:</h2>
-
- <ul>
- <% @story.errors.full_messages.each do |msg| %>
- <li><%= msg %></li>
- <% end %>
- </ul>
- </div>
- <% end %>
-
- <div class="field">
- <%= f.number_field :comic_id %>
- </div>
- <div class="field">
- <%= f.number_field :t %>
- </div>
- <div class="field">
- <%= f.number_field :panel_id %>
- </div>
-
- <div class="actions">
- <%= f.submit %>
- </div>
-<% end %>
+<%= render 'newform' %>
get :new
assigns(:story).should be_a_new(Story)
end
- it 'ã\82³ã\83\9eã\83¢ã\83\87ã\83«ã\81«ã\83\87ã\83\95ã\82©ã\83«ã\83\88å\80¤è£\9cå\85\85ã\82\92ä¾\9dé ¼ã\81\97ã\81¦ã\81\84ã\82\8b' do
+ it 'モデルにデフォルト値補充を依頼している' do
Story.any_instance.should_receive(:supply_default).exactly(1)
get :new
end
sign_in @user
end
context 'つつがなく終わるとき' do
+ it 'デフォルト値補充を依頼する' do
+ Story.any_instance.should_receive(:supply_default).exactly(1)
+ post :create, :story => @attr
+ end
+ it 'POSTデータから、カラム値を復元している' do
+ Story.any_instance.stub(:store).and_return(true)
+ Story.any_instance.should_receive(:attributes=).exactly(1)
+ post :create, :story => @attr
+ end
+ it '上書き補充を依頼する' do
+ Story.any_instance.should_receive(:overwrite).exactly(1)
+ post :create, :story => @attr
+ end
it 'モデルに保存依頼する' do
Story.any_instance.should_receive(:store).exactly(1)
post :create, :story => @attr
response.should redirect_to(:action => :show, :id => @attr[:comic_id])
end
end
- context 'js形式' do
- it 'ステータスコード302 Foundを返す' do
- Story.any_instance.stub(:store).and_return(true)
- post :create, :story => @attr, :format => :js
- response.status.should eq 302
- end
- it 'コミックのストーリー表示へ遷移する' do
-# Story.any_instance.stub(:store).and_return(true)
- post :create, :story => @attr, :format => :js
- response.should redirect_to(:action => :show, :id => @attr[:comic_id])
- end
- end
context 'json形式' do
it 'ステータスコード200 OKを返す' do
# Story.any_instance.stub(:store).and_return(true)
it 'データがアレになっている' do
post :create, :story => @attr, :format => :json
json = JSON.parse response.body
- json["t"].should eq @story.t
+ json["t"].should eq 0
end
end
end
Story.should_receive(:show).exactly(1)
put :update, :id => @story.id, :story => @attr
end
+ it 'POSTデータから、カラム値を復元している' do
+ Story.any_instance.stub(:store).and_return(true)
+ Story.any_instance.should_receive(:attributes=).exactly(1)
+ put :update, :id => @story.id, :story => @attr
+ end
+ it '上書き補充を依頼する' do
+ Story.any_instance.should_receive(:overwrite).exactly(1)
+ put :update, :id => @story.id, :story => @attr
+ end
it 'モデルに保存依頼する' do
Story.any_instance.should_receive(:store).exactly(1)
put :update, :id => @story.id, :story => @attr
end
end
- describe 'データ補充に於いて' do
+ describe 'デフォルト値補充に於いて' do
+ before do
+ @comic = Factory :comic, :author_id => @author.id
+ @panel = Factory :panel, :author_id => @author.id
+ end
+
+ #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
+ it 'tをnilにする' do
+ @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
+ @story.supply_default
+ @story.t.should be_nil
+ end
+
+ end
+
+ describe '上書き補充に於いて' do
before do
@comic = Factory :comic, :author_id => @author.id
@panel = Factory :panel, :author_id => @author.id
it '問答無用でauthor_idを補充する' do
@story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
@story.author_id = nil
- @story.supply_default @author
+ @story.overwrite @author
@story.author_id.should eq @author.id
end
end