OSDN Git Service

build license model
authoryasushiito <yas@pen-chan.jp>
Wed, 27 Jun 2012 08:24:33 +0000 (17:24 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 27 Jun 2012 08:24:33 +0000 (17:24 +0900)
app/models/license.rb
app/models/system_picture.rb
db/migrate/20120617061753_change_license.rb
spec/factories.rb
spec/models/license_spec.rb

index 8f5a476..4414a15 100644 (file)
@@ -1,9 +1,12 @@
 class License < ActiveRecord::Base
   belongs_to :license_group
-  has_one :original_license
-  has_one :common_license
+  belongs_to :system_picture
+  
+  validates :license_group_id, :presence => true, :numericality => true, :existence => true
   validates :name, :presence => true, :length => {:maximum => 50}
-  validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => {:allow_blank => true}
+  validates :caption, :presence => true, :length => {:maximum => 30}
+  validates :url, :presence => true, :length => {:maximum => 200}, :uniqueness => true, :url => true #{:allow_blank => true}
+  validates :system_picture_id, :presence => true, :numericality => true, :existence => true
   
   def self.update_license cl
     l = License.find_by_url cl.url
@@ -18,47 +21,18 @@ class License < ActiveRecord::Base
     l
   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
-  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
-  end
-  
-  def self.offset cnt, prm = nil
-    offset = prm.to_i
-    offset = cnt - 1 if offset >= cnt
-    offset = cnt - offset.abs if offset < 0
-    offset = 0 if offset < 0
-    offset
-  end
-  
-  def self.list opt = {}, page = 1, page_size = self.default_page_size
+  def self.list lg_id, opt = {}
     opt.merge!(self.list_opt) unless opt[:include]
-    opt.merge!({:order => 'name', :limit => page_size, :offset => (page -1) * page_size})
+    opt.merge!({:conditions => ['licenses.license_group_id = ?', lg_id], :order => 'name'})
     License.find(:all, opt)
   end
   
   def self.list_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+    {:include => {:license_group => {}}}
   end
   
   def self.list_json_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+    {:include => {:license_group => {}}}
   end
   
   def self.show rid, opt = {}
@@ -68,13 +42,13 @@ class License < ActiveRecord::Base
   end
   
   def self.show_include_opt opt = {}
-    res = [:common_license, :original_license]
+    res = [:license_group]
     res.push(opt[:include]) if opt[:include]
     res
   end
   
   def self.show_json_include_opt
-    {:include => {:common_license => {}, :original_license => {}}}
+    {:include => {:license_group => {}}}
   end
   
 end
index db857b7..a2dea47 100644 (file)
@@ -1,6 +1,7 @@
 class SystemPicture < ActiveRecord::Base
   has_many :balloons
   has_many :balloon_templates
+  has_many :licenses
   
   validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}
   validates :width, :presence => true, :numericality => true, :natural_number => true
index 47d2af7..321af1e 100644 (file)
@@ -5,16 +5,18 @@ class ChangeLicense < ActiveRecord::Migration
     create_table :licenses do |t|
       t.integer :license_group_id, :null => false, :default => 0
       t.string :name, :null => false, :limit => 50, :default => 'Default'
-      t.string :classname, :null => false, :limit => 50, :default => 'Default'
       t.string :caption, :null => false, :limit => 30, :default => 'no name'
+      t.integer :system_picture_id, :null => false, :default => 0
       t.string :url, :null => false\r
       t.timestamps
     end
     add_index :licenses, [:name], :unique => true
+    add_index :licenses, [:url], :unique => true
   end
 
   def down
     remove_index :licenses, :column => [:name]
+    remove_index :licenses, :column => [:url]
     drop_table :licenses
     create_table :licenses do |t|
       t.string :name, :null => false\r
index 84ff1ee..779e4ed 100644 (file)
@@ -44,16 +44,11 @@ Factory.define :license_group, :class => LicenseGroup do |license_group|
 end
 
 Factory.define :license, :class => License do |license|
+  license.license_group_id 1
   license.name 'peta2.5'
+  license.caption 'flag'
   license.url 'http://test.lc/'
-  license.cc_by 0
-  license.cc_sa 0
-  license.cc_nd 0
-  license.cc_nc 0
-  license.no_resize 0
-  license.no_flip 0
-  license.no_convert 0
-  license.keep_aspect_ratio 0
+  license.system_picture_id 1
 end
 
 Factory.define :common_license, :class => CommonLicense do |license|
index 39fda16..4e93e29 100644 (file)
@@ -7,14 +7,38 @@ describe License do
   end
   describe '検証に於いて' do
     before do
-      @l = Factory.build :license
+      @sp = Factory :system_picture
+      @lg = Factory :license_group
+      @l = Factory.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
     
     it 'オーソドックスなデータなら通る' do
       @l.should be_valid
     end
     
+    context 'license_group_idを検証するとき' do
+      it 'テストデータの確認' do\r
+        @l.license_group_id = @lg.id\r
+        @l.should be_valid\r
+      end\r
+      it 'nullなら失敗する' do
+        @l.license_group_id = ''
+        @l.should_not be_valid
+      end
+      it '数値でなければ失敗する' do\r
+        @l.license_group_id = 'a'\r
+        @l.should_not be_valid\r
+      end\r
+      it '存在するライセンスグループでなければ失敗する' do\r
+        @l.license_group_id = 0\r
+        @l.should_not be_valid\r
+      end\r
+    end
     context 'nameを検証するとき' do
+      it 'テストデータの確認' do\r
+        @l.name = 'a'*50\r
+        @l.should be_valid\r
+      end\r
       it 'nullなら失敗する' do
         @l.name = ''
         @l.should_not be_valid
@@ -23,84 +47,86 @@ describe License do
         @l.name = 'a'*51
         @l.should_not be_valid
       end
+      it '重複していたら失敗する' do
+        lc = Factory :license
+        @l.should_not be_valid
+      end
+    end
+    context 'captionを検証するとき' do
+      it 'テストデータの確認' do\r
+        @l.caption = 'a'*30\r
+        @l.should be_valid\r
+      end\r
+      it 'nullなら失敗する' do
+        @l.caption = ''
+        @l.should_not be_valid
+      end
+      it '31文字以上なら失敗する' do
+        @l.caption = 'a'*31
+        @l.should_not be_valid
+      end
     end
     context 'urlを検証するとき' do
+      it 'テストデータの確認' do\r
+        @l.url = 'http://test.jp/aaaaa' + 'a' * 180
+        @l.save!\r
+        @l.should be_valid\r
+      end\r
       it 'nullなら失敗する' do
         @l.url = ''
         @l.should_not be_valid
       end
       it '201文字以上なら失敗する' do
-        @l.url = 'a'*201
-        @l.should_not be_valid
-      end
-      it '重複していたら失敗する' do
-        lc = Factory :license
+        @l.url = 'http://test.jp/aaaaa' + 'a' * 181
         @l.should_not be_valid
       end
       it 'url形式でなら失敗する' do
-        @l.url = ''
-        pending
-      end
-    end
-  end
-  
-  describe '対象ライセンスの取得に於いて' do
-    before do
-      @lc = Factory :license
-    end
-    context 'urlが一致するライセンスがないとき' do
-      it '新規ライセンスを準備して返す' do
-        cl = Factory.build(:common_license, :url => 'http://domain.no')
-        r = License.update_license cl
-        r.should be_a_new License
+        @l.url = 'a'*200\r
+        @l.should_not be_valid\r
       end
     end
-    context 'urlが一致するライセンスがあるとき' do
-      it '該当ライセンスを返す' do
-        r = License.update_license @lc
-        r.is_a?(License).should be_true
-        r.should_not be_a_new License
-        r.url.should eq @lc.url
-        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
+    context 'system_picture_idを検証するとき' do
+      it 'テストデータの確認' do\r
+        @l.system_picture_id = @sp.id\r
+        @l.should be_valid\r
+      end\r
+      it 'nullなら失敗する' do
+        @l.system_picture_id = ''
+        @l.should_not be_valid
       end
+      it '数値でなければ失敗する' do\r
+        @l.system_picture_id = 'a'\r
+        @l.should_not be_valid\r
+      end\r
+      it '存在するシステム画像でなければ失敗する' do\r
+        @l.system_picture_id = 0\r
+        @l.should_not be_valid\r
+      end\r
     end
   end
   
-  #作成が
   describe '単体取得に於いて' do
     before do
-      @lcl = Factory :license
-      @cl = Factory :common_license, :license_id => @lcl.id
-      @lol = Factory :license, :url => 'http://test.ptn/10'
-      @ol = Factory :original_license, :license_id => @lol.id, :url => 'http://test.ptn/10'
+      @sp = Factory :system_picture
+      @lg = Factory :license_group
+      @l = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
     it '指定のコマを返す' do
-      l = License.show @lcl.id
-      l.should eq @lcl
+      l = License.show @l.id
+      l.should eq @l
     end
   end
   describe '関連テーブルプションに於いて' do
     context 'オプションがないとき' do
-      it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\81¨ã\82ªã\83ªã\82¸ã\83\8aã\83«ã\83©ã\82¤ã\82»ã\83³ã\82¹を含んでいる' do
+      it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97を含んでいる' do
         r = License.show_include_opt
-        r.should eq [:common_license, :original_license]
+        r.should eq [:license_group]
       end
     end
-    context 'オプションで原画を含ませたとき' do
-      it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\81¨ã\82ªã\83ªã\82¸ã\83\8aã\83«ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\81¨å\8e\9fç\94»ã\83\87ã\83¼ã\82¿を含んでいる' do
-        r = License.show_include_opt(:include => :original_picture)
-        r.should eq [:common_license, :original_license, :original_picture]
+    context 'オプションで素材ライセンスを含ませたとき' do
+      it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97ã\81¨ç´ æ\9d\90ã\83©ã\82¤ã\82»ã\83³ã\82¹を含んでいる' do
+        r = License.show_include_opt(:include => :resource_picture_license)
+        r.should eq [:license_group, :resource_picture_license]
       end
     end
   end
@@ -109,88 +135,31 @@ describe License do
       r = License.show_json_include_opt
       r.has_key?(:include).should be_true
     end
-    it '2つの項目を含んでいる' do
+    it '1つの項目を含んでいる' do
       r = License.show_json_include_opt[:include]
-      r.should have(2).items
+      r.should have(1).items
     end
-    it 'ã\82³ã\83¢ã\83³ã\83©ã\82¤ã\82»ã\83³ã\82¹を含んでいる' do
+    it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97を含んでいる' do
       r = License.show_json_include_opt[:include]
-      r.has_key?(:common_license).should be_true
-    end
-    it 'オリジナルライセンスを含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.has_key?(:original_license).should be_true
+      r.has_key?(:license_group).should be_true
     end
   end
   describe '一覧取得に於いて' do
     before do
-      @lcl = Factory :license, :name => 'peta2.0'
-      @cl = Factory :common_license, :license_id => @lcl.id
-    end
-    context 'page補正について' do
-      it '文字列から数値に変換される' do
-        License.page('8').should eq 8
-      end
-      it 'nilの場合は1になる' do
-        License.page().should eq 1
-      end
-      it '0以下の場合は1になる' do
-        License.page('0').should eq 1
-      end
-    end
-    context 'page_size補正について' do
-      it '文字列から数値に変換される' do
-        License.page_size('7').should eq 7
-      end
-      it 'nilの場合はLicense.default_page_sizeになる' do
-        License.page_size().should eq License.default_page_size
-      end
-      it '0以下の場合はLicense.default_page_sizeになる' do
-        License.page_size('0').should eq License.default_page_size
-      end
-      it 'License.max_page_sizeを超えた場合はLicense.max_page_sizeになる' do
-        License.page_size('1000').should eq License.max_page_size
-      end
+      @sp = Factory :system_picture
+      @lg = Factory :license_group
+      @l = Factory :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @lg2 = Factory :license_group, :name => 'pubdm'
     end
     it 'リストを返す' do
-      pl = License.list
-      pl.should eq [@lcl]
-    end
-    it '名前順で並んでいる' do
-      @lol = Factory :license, :name => 'peta1.0', :url => 'http://test.ptn/10'
-      @ol = Factory :original_license, :license_id => @lol.id, :name => 'peta1.0', :url => 'http://test.ptn/10'
-      l = License.list
-      l.should eq [@lol, @lcl]
-    end
-    context 'DBに5件あって1ページの件数を2件に変えたとして' do
-      before do
-        @lol2 = Factory :license, :name => 'peta2.1', :url => 'http://test.ptn/21'
-        @ol2 = Factory :original_license, :license_id => @lol2.id, :name => 'peta2.1', :url => 'http://test.ptn/21'
-        @lol3 = Factory :license, :name => 'peta2.2', :url => 'http://test.ptn/22'
-        @ol3 = Factory :original_license, :license_id => @lol3.id, :name => 'peta2.2', :url => 'http://test.ptn/22'
-        @lol4 = Factory :license, :name => 'peta2.3', :url => 'http://test.ptn/23'
-        @ol4 = Factory :original_license, :license_id => @lol4.id, :name => 'peta2.3', :url => 'http://test.ptn/23'
-        @lol5 = Factory :license, :name => 'peta2.4', :url => 'http://test.ptn/24'
-        @ol5 = Factory :original_license, :license_id => @lol5.id, :name => 'peta2.4', :url => 'http://test.ptn/24'
-        License.stub(:default_page_size).and_return(2)
-      end
-      it '通常は2件を返す' do
-        l = License.list
-        l.should have(2).items 
-      end
-      it 'page=1なら末尾2件を返す' do
-        #名前順で並んでいる
-        l = License.list( {}, 1)
-        l.should eq [@lcl, @lol2]
-      end
-      it 'page=2なら中間2件を返す' do
-        l = License.list({}, 2)
-        l.should eq [@lol3, @lol4]
-      end
-      it 'page=3なら先頭1件を返す' do
-        l = License.list({}, 3)
-        l.should eq [@lol5]
-      end
+      l = License.list @lg.id
+      l.should eq [@l]
+    end
+    it 'グループ順,名前順で並んでいる' do
+      @l2 = Factory :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l3 = Factory :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
+      l = License.list @lg.id
+      l.should eq [@l, @l2]
     end
   end
   describe 'list関連テーブルプションに於いて' do
@@ -198,17 +167,13 @@ describe License do
       r = License.list_opt
       r.has_key?(:include).should be_true
     end
-    it '2つの項目を含んでいる' do
-      r = License.list_opt[:include]
-      r.should have(2).items
-    end
-    it 'コモンライセンスを含んでいる' do
+    it '1つの項目を含んでいる' do
       r = License.list_opt[:include]
-      r.has_key?(:common_license).should be_true
+      r.should have(1).items
     end
-    it 'ã\82ªã\83ªã\82¸ã\83\8aã\83«ã\83©ã\82¤ã\82»ã\83³ã\82¹を含んでいる' do
+    it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97を含んでいる' do
       r = License.list_opt[:include]
-      r.has_key?(:original_license).should be_true
+      r.has_key?(:license_group).should be_true
     end
   end
   describe 'json一覧出力オプションに於いて' do
@@ -216,17 +181,13 @@ describe License do
       r = License.list_json_opt
       r.has_key?(:include).should be_true
     end
-    it '2つの項目を含んでいる' do
-      r = License.list_json_opt[:include]
-      r.should have(2).items
-    end
-    it 'コモンライセンスを含んでいる' do
+    it '1つの項目を含んでいる' do
       r = License.list_json_opt[:include]
-      r.has_key?(:common_license).should be_true
+      r.should have(1).items
     end
-    it 'ã\82ªã\83ªã\82¸ã\83\8aã\83«ã\83©ã\82¤ã\82»ã\83³ã\82¹を含んでいる' do
+    it 'ã\83©ã\82¤ã\82»ã\83³ã\82¹ã\82°ã\83«ã\83¼ã\83\97を含んでいる' do
       r = License.list_json_opt[:include]
-      r.has_key?(:original_license).should be_true
+      r.has_key?(:license_group).should be_true
     end
   end