+#コモンライセンス
class CommonLicensesController < ApplicationController
layout 'test' if Pettanr::TestLayout
before_filter :authenticate_user!, :only => [:index, :show]
format.html { redirect_to :action => :list }
format.json { render text: 'ok', status: :created }
else
+ #@errorsがNILならJson展開できない全体異常
format.html { render action: "result" }
format.json { render json: @errors, status: :unprocessable_entity }
end
class OriginalLicensesController < ApplicationController
- before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy]
+ layout 'test' if Pettanr::TestLayout
+ before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy, :import]
+ private
+
+ def set_data(file)
+ if file.respond_to?(:read)
+ file.read
+ else
+ file
+ end
+ end
+
+ public
+
# GET /original_licenses
# GET /original_licenses.json
def index
format.json { head :ok }
end
end
+
+ def import
+ @data = set_data params[:file]
+ respond_to do |format|
+ @errors = OriginalLicense.import @data
+ if @errors and @errors.empty?
+ format.html { redirect_to :action => :list }
+ format.json { render text: 'ok', status: :created }
+ else
+ format.html { render action: "result" }
+ format.json { render json: @errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
end
CommonLicense.import t
end
+ def import_error_message(c = '\n')
+ self.errors.each do |atr, messages|
+ if atr == :base
+ end
+ full_messages.join c
+ end
+ end
+
end
:name => cl.name, :url => cl.url, :cc_by => cl.cc_by, :cc_sa => cl.cc_sa, :cc_nd => cl.cc_nd, :cc_nc => cl.cc_nc,
:no_resize => cl.no_resize, :no_flip => cl.no_flip, :no_convert => cl.no_convert, :keep_aspect_ratio => cl.keep_aspect_ratio
}
+ if cl.new_record? and l.new_record? == false
+ l.errors.add :base, 'dupulicate url'
+ end
l
end
+<table>
+ <tr>
+ <th>name</th>
+ <th>message</th>
+ </tr>
<% @errors.each do |common_license| %>
+ <td><%= h common_license.name %></td>
+ <td><%= h common_license.import_error_message('<br>') %></td>
<% end %>
+</table>
<table>
<tr>
- <th></th>
+ <th>name</th>
<th></th>
<th></th>
</tr>
<% @original_licenses.each do |original_license| %>
<tr>
- <td><%= link_to 'Show', original_license %></td>
+ <td><%= link_to h(original_license.name), original_license %></td>
<td><%= link_to 'Edit', edit_original_license_path(original_license) %></td>
<td><%= link_to 'Destroy', original_license, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<h1>Listing original_licenses</h1>
+<%= form_tag( {:controller => 'original_licenses',:action => "import"} , { :multipart => true }) do %>
+ <label for="file">File to Upload</label> <%= file_field_tag "file" %>
+ <%= submit_tag 'upload' -%>
+<% end -%>
<table>
<tr>
<th>id</th>
- <th>artist_id</th>
<th>license_id</th>
<th>name</th>
<th>url</th>
<% @original_licenses.each do |original_license| %>
<tr>
<td><%= link_to original_license.id, :action => :browse, :id => original_license.id %></td>
- <td><%= link_to original_license.artist_id, :controller => 'artists', :action => :browse, :id => original_license.artist_id %></td>
<td><%= h original_license.name %></td>
<td><%= h original_license.url %></td>
<td><%= original_license.cc_by %></td>
</p>
<p>
- <b>artist:</b>
- <%= @original_license.artist_id %>
-</p>
-<p>
<b>cc_by:</b>
<%= @original_license.cc_by %>
</p>
get :show
get :new
post :create
+ post :import
get :list
get :browse
end
--- /dev/null
+{\r
+ "name": "Local ebi", \r
+ "url": "http://eni.doc/", \r
+ "cc_by": 0, \r
+ "cc_sa": 0, \r
+ "cc_nd": 0, \r
+ "cc_nc": 0, \r
+ "no_resize": 0, \r
+ "no_flip": 0, \r
+ "no_convert": 0, \r
+ "keep_aspect_ratio": 0\r
+}\r
end
describe 'インポートに於いて' do
+ before do
+ sign_in @admin
+ sign_in @user
+ #テストデータを用意してね
+ @f = Rails.root + 'spec/json/original_license.json'
+ @t = File.open(@f, 'r').read
+ @j = JSON.parse @t
+ @fs = Rails.root + 'spec/json/original_licenses.json'
+ @ts = File.open(@fs, 'r').read
+ @js = JSON.parse @ts
+ @fes = Rails.root + 'spec/json/invalid_original_licenses.json'
+ @tes = File.open(@fes, 'r').read
+ @jes = JSON.parse @tes
+ end
+ context '事前チェックしておく' do
+ before do
+ #異常な行を返すから、正常の意味で空を返す
+ OriginalLicense.stub(:import).with(any_args()).and_return([])
+ end
+ it "@dataに渡したデータを保持している" do
+ post :import, :file => @t
+ assigns(:data).should_not be_nil
+ end
+ it 'モデルにインポート依頼する' do
+ OriginalLicense.should_receive(:import).with(any_args()).exactly(1)
+ post :import, :file => @t
+ end
+ it "@errorsに結果を保持している" do
+ post :import, :file => @t
+ assigns(:errors).should eq []
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ post :import, :file => @t
+ response.status.should eq 302
+ end
+ it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
+ post :import, :file => @t
+ response.should redirect_to('/original_licenses/list')
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ post :import, :file => @t, :format => :json
+ response.should be_success
+ end
+ end
+ end
+ context '管理者権限がないとき' do
+ before do
+ sign_out @admin
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ post :import, :file => @t
+ response.status.should eq 302
+ end
+ it '管理者サインインページへ遷移する' do
+ post :import, :file => @t
+ response.body.should redirect_to '/admins/sign_in'
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード401 Unauthorizedを返す' do
+ post :import, :file => @t, :format => :json
+ response.status.should eq 401
+ end
+ it '応答メッセージにUnauthorizedを返す' do
+ post :import, :file => @t, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ context '作家権限がないとき' do
+ #必要なのは管理者権限であって作家権限ではない。成功を見届ける
+ before do
+ sign_out @user
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ post :import, :file => @t
+ response.status.should eq 302
+ end
+ it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
+ post :import, :file => @t
+ response.should redirect_to('/original_licenses/list')
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード200 OKを返す' do
+ post :import, :file => @t, :format => :json
+ response.should be_success
+ end
+ end
+ end
+ context '検証、保存に失敗した' do
+ before do
+ #異常な行を返す
+ OriginalLicense.stub(:import).with(any_args()).and_return(
+ [OriginalLicense.new(Factory.attributes_for(:original_license))]
+ )
+ end
+ context 'html形式' do
+ it 'ステータスコード200 OKを返す' do
+ post :import, :file => @t
+ response.status.should eq 200
+ end
+ it 'resultページを描画する' do
+ post :import, :file => @t
+ response.should render_template("result")
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード422 unprocessable_entity を返す' do
+ post :import, :file => @t, :format => :json
+ response.status.should eq 422
+ end
+ it '応答メッセージUnprocessable Entityを返す' do
+ post :import, :file => @t, :format => :json
+ response.message.should match(/Unprocessable/)
+ end
+ end
+ end
end
end
end
end
+ describe 'インポートエラーの表示に於いて' do
+ before do
+ @l = Factory :license
+ @cl = Factory.build :common_license, :license_id => @l.id
+ end
+ it '全体エラーだけなら、そのまま返す' do
+ @cl.errors.add :base, 'base error'
+ @cl.import_error_message.should eq 'base error'
+ end
+ context '複数でエラーのとき' do
+ it '各エラーを改行で区切って結合して返す' do
+ @cl.errors.add :name, 'name error'
+ @cl.errors.add :url, 'url error'
+ @cl.import_error_message.should eq 'name error\nurl error'
+ end
+ end
+ context '区切り指定が<br>で複数でエラーのとき' do
+ it '各エラーを改行で区切って結合して返す' do
+ @cl.errors.add :name, 'name error'
+ @cl.errors.add :url, 'url error'
+ @cl.import_error_message('<br>').should eq 'name error<br>url error'
+ end
+ end
+ end
+
describe 'ファイル取り込みに於いて' do
before do
CommonLicense.stub(:import).with(any_args).and_return(true)
r.name.should eq @lc.name
end
end
+ #コモンライセンスとオリジナルライセンスをまたいだUrl重複チェックはここでやるよりない
+ #コモンライセンスが新規オブジェクトなのにライセンスが取得できるのは、
+ #そのUrlがオリジナルライセンスから登録されているということ
+ context '新規でユニークチェックするとき' do
+ it 'ライセンスの全体エラーに重複メッセージを入れて返す' do
+ cl = Factory.build(:common_license, :url => 'http://domain.no')
+ License.stub(:find_by_url).with(any_args).and_return(@lc)
+ r = License.update_license cl
+ r.errors[:base].should_not be_empty
+ end
+ end
end
#作成が