Skip to content

Add method to mark next record as default when current is destroyed #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 92 additions & 73 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,84 +1,99 @@
PATH
remote: .
specs:
primary (0.0.2)
primary (0.0.3)
rails (>= 3.0.0)

GEM
remote: http://rubygems.org/
specs:
actionmailer (3.2.6)
actionpack (= 3.2.6)
mail (~> 2.4.4)
actionpack (3.2.6)
activemodel (= 3.2.6)
activesupport (= 3.2.6)
builder (~> 3.0.0)
actionmailer (4.2.11.3)
actionpack (= 4.2.11.3)
actionview (= 4.2.11.3)
activejob (= 4.2.11.3)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.11.3)
actionview (= 4.2.11.3)
activesupport (= 4.2.11.3)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.11.3)
activesupport (= 4.2.11.3)
builder (~> 3.1)
erubis (~> 2.7.0)
journey (~> 1.0.1)
rack (~> 1.4.0)
rack-cache (~> 1.2)
rack-test (~> 0.6.1)
sprockets (~> 2.1.3)
activemodel (3.2.6)
activesupport (= 3.2.6)
builder (~> 3.0.0)
activerecord (3.2.6)
activemodel (= 3.2.6)
activesupport (= 3.2.6)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activeresource (3.2.6)
activemodel (= 3.2.6)
activesupport (= 3.2.6)
activesupport (3.2.6)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.3)
builder (3.0.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (4.2.11.3)
activesupport (= 4.2.11.3)
globalid (>= 0.3.0)
activemodel (4.2.11.3)
activesupport (= 4.2.11.3)
builder (~> 3.1)
activerecord (4.2.11.3)
activemodel (= 4.2.11.3)
activesupport (= 4.2.11.3)
arel (~> 6.0)
activesupport (4.2.11.3)
i18n (~> 0.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.4)
builder (3.2.4)
concurrent-ruby (1.1.9)
crass (1.0.6)
diff-lcs (1.1.3)
erubis (2.7.0)
factory_girl (3.4.1)
activesupport (>= 3.0.0)
hike (1.2.1)
i18n (0.6.0)
journey (1.0.4)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.7.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
multi_json (1.3.6)
polyglot (0.3.5)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
mini_mime (1.1.2)
mini_portile2 (2.1.0)
minitest (5.12.0)
nokogiri (1.6.8.1)
mini_portile2 (~> 2.1.0)
rack (1.6.13)
rack-test (0.6.3)
rack (>= 1.0)
rails (3.2.6)
actionmailer (= 3.2.6)
actionpack (= 3.2.6)
activerecord (= 3.2.6)
activeresource (= 3.2.6)
activesupport (= 3.2.6)
bundler (~> 1.0)
railties (= 3.2.6)
railties (3.2.6)
actionpack (= 3.2.6)
activesupport (= 3.2.6)
rack-ssl (~> 1.3.2)
rails (4.2.11.3)
actionmailer (= 4.2.11.3)
actionpack (= 4.2.11.3)
actionview (= 4.2.11.3)
activejob (= 4.2.11.3)
activemodel (= 4.2.11.3)
activerecord (= 4.2.11.3)
activesupport (= 4.2.11.3)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.11.3)
sprockets-rails
rails-deprecated_sanitizer (1.0.4)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.9)
activesupport (>= 4.2.0, < 5.0)
nokogiri (~> 1.6)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.4.2)
loofah (~> 2.3)
railties (4.2.11.3)
actionpack (= 4.2.11.3)
activesupport (= 4.2.11.3)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
thor (>= 0.18.1, < 2.0)
rake (12.3.3)
rspec (2.10.0)
rspec-core (~> 2.10.0)
rspec-expectations (~> 2.10.0)
Expand All @@ -92,17 +107,18 @@ GEM
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.10.0)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.2)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.6)
thor (0.15.3)
tilt (1.3.3)
treetop (1.4.15)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.42)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.9)
thread_safe (~> 0.1)

PLATFORMS
ruby
Expand All @@ -115,3 +131,6 @@ DEPENDENCIES
rake
rspec-rails
sqlite3

BUNDLED WITH
1.17.3
22 changes: 20 additions & 2 deletions lib/primary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def is_primary(options = {})

before_save :primary_is_primary_mark
after_save :primary_is_primary_take_care
before_destroy :primary_is_primary_update_mark
include InstanceMethods
end

Expand All @@ -34,8 +35,19 @@ def primary_is_primary_take_care
end
end

def get_primary_scope(options)
check = self.class.default_scoped.where("#{options[:on]} = ?", true)
def primary_is_primary_update_mark
options = self.class.primary_is_primary_opts
return unless !!options[:auto_primary_record]

if send("#{options[:on]}?") == true
scope = get_primary_scope(options, false)
record = scope.where('id!=?', id).first
record.update_attribute("#{options[:on]}", true) if record.present?
end
end

def get_primary_scope(options, primary_scope = true)
check = self.class.default_scoped.where(build_condition_query(options, primary_scope), primary_scope)
if options[:scope]
sc = options[:scope]
if sc.is_a?(Symbol) || sc.is_a?(String)
Expand All @@ -49,6 +61,12 @@ def get_primary_scope(options)
check
end

def build_condition_query(options, primary_scope)
query = "#{options[:on]} = ?"
query = "#{query} OR #{options[:on]} IS NULL" unless primary_scope
query
end

def build_primary_scope(scope_chain, elem)
elem = elem.to_sym if elem.is_a?(String)
scope_chain.where("#{elem} = ?", send(elem))
Expand Down
2 changes: 1 addition & 1 deletion lib/primary/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Primary
VERSION = "0.0.3"
VERSION = "0.0.4"
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/background.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Background < ActiveRecord::Base
is_primary auto_primary_record: false

attr_accessible :is_primary, :url
# attr_accessible :is_primary, :url
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class Domain < ActiveRecord::Base
belongs_to :site
has_many :photos, :as=>:photoable

attr_accessible :domain, :is_primary
# attr_accessible :domain, :is_primary
end
4 changes: 2 additions & 2 deletions spec/dummy/app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Photo < ActiveRecord::Base
is_primary :scope=>[:photoable_type, :photoable_id]

## ASSOCIATIONS
belongs_to :photoable, :polymorphic=>true

Expand All @@ -9,5 +9,5 @@ class Photo < ActiveRecord::Base
#:presence=>true

## ACCESSIBLE
attr_accessible :photo_file_name
# attr_accessible :photo_file_name
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Site < ActiveRecord::Base
:uniqueness => true

## ACCESSIBLE
attr_accessible :is_default, :name
# attr_accessible :is_default, :name

def to_s
self.name
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
# require "active_resource/railtie"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"

Expand Down Expand Up @@ -53,7 +53,7 @@ class Application < Rails::Application
# This will create an empty whitelist of attributes available for mass-assignment for all models
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
config.active_record.whitelist_attributes = true
# config.active_record.whitelist_attributes = true

# Enable the asset pipeline
config.assets.enabled = true
Expand Down
6 changes: 4 additions & 2 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
config.cache_classes = true

# Configure static asset server for tests with Cache-Control for performance
config.serve_static_assets = true
config.serve_static_files = true
config.static_cache_control = "public, max-age=3600"

# Log error messages when you accidentally call methods on nil
Expand All @@ -30,8 +30,10 @@
config.action_mailer.delivery_method = :test

# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# config.active_record.mass_assignment_sanitizer = :strict

# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr

config.eager_load = false
end
13 changes: 13 additions & 0 deletions spec/models/background_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,18 @@

Background.where(is_primary: true).should == [first_background]
end

it "should not mark another record as default after destroy current default" do
background_1 = FactoryGirl.build(:background)
background_2 = FactoryGirl.build(:background)
background_1.is_primary = true

[background_1, background_2].map(&:save)

background_1.is_primary.should == true
background_2.is_primary.should == false
background_1.destroy!
background_2.is_primary.should == false
end
end
end
Loading