Skip to content

Commit a730064

Browse files
committed
Merge pull request #2 from rafaltrojanowski/feat-option
add option for dont mark first record as default
2 parents 194419b + c254c7d commit a730064

File tree

8 files changed

+74
-10
lines changed

8 files changed

+74
-10
lines changed

Gemfile.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
primary (0.0.1)
4+
primary (0.0.2)
55
rails (>= 3.0.0)
66

77
GEM
@@ -34,7 +34,7 @@ GEM
3434
activesupport (3.2.6)
3535
i18n (~> 0.6)
3636
multi_json (~> 1.0)
37-
arel (3.0.2)
37+
arel (3.0.3)
3838
builder (3.0.0)
3939
diff-lcs (1.1.3)
4040
erubis (2.7.0)
@@ -51,9 +51,9 @@ GEM
5151
i18n (>= 0.4.0)
5252
mime-types (~> 1.16)
5353
treetop (~> 1.4.8)
54-
mime-types (1.19)
54+
mime-types (1.25.1)
5555
multi_json (1.3.6)
56-
polyglot (0.3.3)
56+
polyglot (0.3.5)
5757
rack (1.4.1)
5858
rack-cache (1.2)
5959
rack (>= 0.4)
@@ -99,10 +99,10 @@ GEM
9999
sqlite3 (1.3.6)
100100
thor (0.15.3)
101101
tilt (1.3.3)
102-
treetop (1.4.10)
102+
treetop (1.4.15)
103103
polyglot
104104
polyglot (>= 0.3.1)
105-
tzinfo (0.3.33)
105+
tzinfo (0.3.42)
106106

107107
PLATFORMS
108108
ruby

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ is_primary :on=>'is_default', :scope=>'site_id'
5353
# Scope can be defined as array of params
5454
:scope => ['site_id', 'language_code']
5555
:scope => ['polymorphic_id', 'polymorphic_type']
56+
:auto_primary_record => false # if you don't want primary record
57+
# to be set automatically (default: true)
5658
```
5759

5860
##Requirements

lib/primary.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ module Primary
22
def is_primary options={}
33
cattr_accessor :primary_is_primary_opts
44
self.primary_is_primary_opts = options.reverse_merge({
5-
:on => :is_primary,
6-
:scope => nil
5+
:on => :is_primary,
6+
:scope => nil,
7+
:auto_primary_record => true
78
})
89

910
before_save :primary_is_primary_mark
@@ -14,6 +15,8 @@ module InstanceMethods
1415
private
1516
def primary_is_primary_mark
1617
options = self.class.primary_is_primary_opts
18+
return unless !!options[:auto_primary_record]
19+
1720
check = get_primary_scope(options)
1821
if check.count == 0
1922
self.send("#{options[:on]}=", true)
@@ -35,7 +38,7 @@ def get_primary_scope(options)
3538
if sc.is_a?(Symbol) or sc.is_a?(String)
3639
check = build_primary_scope(check, sc)
3740
elsif sc.is_a?(Array)
38-
sc.each do |scope_elem|
41+
sc.each do |scope_elem|
3942
check = build_primary_scope(check, scope_elem)
4043
end
4144
end

spec/dummy/app/models/background.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Background < ActiveRecord::Base
2+
is_primary auto_primary_record: false
3+
4+
attr_accessible :is_primary, :url
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateBackgrounds < ActiveRecord::Migration
2+
def change
3+
create_table :backgrounds do |t|
4+
t.string :url
5+
t.boolean :is_primary
6+
7+
t.timestamps
8+
end
9+
end
10+
end

spec/dummy/db/schema.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20120708132456) do
14+
ActiveRecord::Schema.define(:version => 20141126165902) do
15+
16+
create_table "backgrounds", :force => true do |t|
17+
t.string "url"
18+
t.boolean "is_primary"
19+
t.datetime "created_at", :null => false
20+
t.datetime "updated_at", :null => false
21+
end
1522

1623
create_table "domains", :force => true do |t|
1724
t.integer "site_id"

spec/factories/common.rb

+4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
sequence(:domain){|n| "test_domain_#{n}.test"}
1010
is_primary false
1111
end
12+
1213
factory :photo do
1314
is_primary false
1415
end
1516

17+
factory :background do
18+
is_primary false
19+
end
1620
end

spec/models/background_spec.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
describe Background do
4+
5+
context 'auto_primary_record is false' do
6+
it "should not be marked as default if it's first record" do
7+
background = FactoryGirl.build(:background)
8+
background.save
9+
background.is_primary.should == false
10+
end
11+
12+
it "should not be marked as default out of the box" do
13+
background_1 = FactoryGirl.build(:background)
14+
background_2 = FactoryGirl.build(:background)
15+
16+
[background_1, background_2].map(&:save)
17+
18+
background_1.is_primary.should == false
19+
background_2.is_primary.should == false
20+
end
21+
22+
it "should make sure, that there is only one default record" do
23+
backgrounds = FactoryGirl.create_list(:background, 3)
24+
Background.update_all(is_primary: true)
25+
26+
first_background = Background.first
27+
first_background.is_primary = true
28+
first_background.save
29+
30+
Background.where(is_primary: true).should == [first_background]
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)