acts_as_default 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21e3b7d9b89c9e22291465716fe3ec253d338183
4
+ data.tar.gz: 3d34ac2a23342c69cd2028911143beee457a142d
5
+ SHA512:
6
+ metadata.gz: f8edf32e8a2a8a2a4c14e47186e08e23ad12b19d09546bb6dc358025596d6d1e722ce0d99dbec098996739b91a91f835c55d2522737aa07adae2fcb305e6a97e
7
+ data.tar.gz: aca6cc979cad90f12b09afe959b000b994d07c9c5b6dd93a3d93b4a068f27561fbcdb789b46d09b780f1981f11cad37277480e1788faa0a256ec7bd4bf912a4d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ script: bundle exec rake test
6
+ notifications:
7
+ email:
8
+ on_success: never
9
+ on_failure: change
10
+ gemfile:
11
+ - Gemfile
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_default.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mirko Mignini
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ [![Build Status](https://travis-ci.org/MirkoMignini/acts_as_default.png?branch=master)](https://travis-ci.org/MirkoMignini/acts_as_default)
2
+
3
+ # ActsAsDefault
4
+
5
+ Using acts as default you can set a default row in your activerecord collection, also for has_many relationships
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'acts_as_default'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install acts_as_default
20
+
21
+ ## Usage
22
+
23
+ Include ActsAsDefault in the models when you need a default row
24
+ Call acts_as_default, specifying which field, is present to determine sub-collections
25
+
26
+ ```ruby
27
+ class Parent < ActiveRecord::Base
28
+ include ActsAsDefault
29
+ acts_as_default
30
+ has_many :sons
31
+ end
32
+
33
+ class Son < ActiveRecord::Base
34
+ include ActsAsDefault
35
+ acts_as_default :parent_id
36
+ belongs_to :parent
37
+ end
38
+ ```
39
+
40
+ Then create a migration to create default column:
41
+
42
+ ```ruby
43
+ add_column :parents, :default, :boolean
44
+ add_column :sons, :default, :boolean
45
+ ```
46
+
47
+ ## Using default value in the simple way:
48
+
49
+ ```ruby
50
+ p1 = Parent.new
51
+ p1.save!
52
+ p2 = Parent.new
53
+ p2.save!
54
+ p3 = Parent.new
55
+ p3.save!
56
+ puts p1.default #true, because was the first created
57
+ puts p2.default #false, because a default is already present
58
+ puts p3.default #false, because a default is already present
59
+
60
+ p3.destroy! #if we destroy a non default object nothing happens
61
+
62
+ p1.destroy! #but if we destroy the default object...
63
+ p2.reload #don't forget to reload to see changes
64
+ puts p2.default #true, now p2 is default, because was the only one present, in case of more the recent based on created_at will become the default
65
+ ```
66
+
67
+ ## Setting the default manually
68
+
69
+ You can always call:
70
+ ```ruby
71
+ p1 = Parent.new
72
+ p2 = Parent.new
73
+
74
+ puts p1.default #true, because was the first created
75
+ puts p2.default #false, because a default is already present
76
+
77
+ model.set_as_default!
78
+ ```
79
+ To immediately switch the object as default and save.
80
+
81
+ ## Using default value in the complex way:
82
+
83
+ You can use acts_as_default to manage default for collection in has_many relationships
84
+ ```ruby
85
+ p1 = Parent.new
86
+ p2 = Parent.new
87
+
88
+ s1 = Son.new
89
+ s1.parent = p1
90
+ s1.save!
91
+
92
+ s2 = Son.new
93
+ s2.parent = p1
94
+ s2.save!
95
+
96
+ s3 = Son.new
97
+ s3.parent = p2
98
+ s3.save!
99
+
100
+ puts s1.default #true, because was the first created for p1
101
+ puts s2.default #false, because a default is already present in p1
102
+ puts s3.default #true, because was the first created for p2
103
+ ```
104
+
105
+ ## Contributing
106
+
107
+ 1. Fork it ( http://github.com/MirkoMignini/acts_as_default/fork )
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_default/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "acts_as_default"
8
+ spec.version = ActsAsDefault::VERSION
9
+ spec.authors = ["Mirko Mignini"]
10
+ spec.email = ["mirko.mignini@libero.it"]
11
+ spec.summary = %q{Using acts as default you can set a default row in your activerecord collection, also for has_many relationships}
12
+ spec.description = ''
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "activerecord", "~> 4.0.0"
23
+ spec.add_development_dependency 'minitest'
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "sqlite3"
26
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsDefault
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require_relative 'acts_as_default/version'
2
+ require 'active_support/concern'
3
+
4
+ module ActsAsDefault
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ attr :field_key
13
+
14
+ def acts_as_default(field = nil)
15
+ @field_key = field
16
+ include ActsAsDefault::InstanceMethods
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+ extend ActiveSupport::Concern
22
+
23
+ included do
24
+ before_save :check_before_save
25
+ after_destroy :check_after_destroy
26
+ end
27
+
28
+ def set_as_default!
29
+ if default == false
30
+ get_default_object.toggle!(:default)
31
+ toggle!(:default)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def check_before_save
38
+ if self.default
39
+ obj = get_default_object
40
+ obj.toggle!(:default) if obj and obj.id != id
41
+ else
42
+ self.default = check_if_empty?
43
+ end
44
+ return true
45
+ end
46
+
47
+ def check_after_destroy
48
+ obj = get_default_object
49
+ if obj.nil?
50
+ obj = get_collection.order(created_at: :desc).first
51
+ obj.toggle!(:default) if obj
52
+ end
53
+ end
54
+
55
+ def check_if_empty?
56
+ get_collection.size == 0
57
+ end
58
+
59
+ def get_collection
60
+ if self.class.field_key.nil?
61
+ self.class.all
62
+ else
63
+ self.class.where(self.class.field_key => self[self.class.field_key])
64
+ end
65
+ end
66
+
67
+ def get_default_object
68
+ if self.class.field_key.nil?
69
+ self.class.where("[default] = ?", true).first
70
+ else
71
+ self.class.where("[#{self.class.field_key}] = ? AND [default] = ?", self[self.class.field_key], true).first
72
+ end
73
+ end
74
+ end
75
+ end
data/test/test_all.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'minitest/autorun'
4
+ require_relative '../lib/acts_as_default'
5
+ require_relative 'test_objects'
6
+
7
+ class TestAll < Minitest::Unit::TestCase
8
+
9
+ def setup
10
+ setup_objects
11
+ end
12
+
13
+ def teardown
14
+ end
15
+
16
+ def test_simple
17
+ Parent.delete_all
18
+
19
+ 3.times do
20
+ p = Parent.new
21
+ p.save!
22
+ end
23
+
24
+ assert_equal true, Parent.first.default
25
+ assert_equal true, contains_one_default?(Parent.all)
26
+ end
27
+
28
+ def test_standard
29
+ Son.delete_all
30
+ Parent.delete_all
31
+
32
+ 2.times do
33
+ p = Parent.new
34
+ p.save!
35
+ 3.times do
36
+ s = Son.new
37
+ s.parent = p
38
+ s.save!
39
+ end
40
+ end
41
+
42
+ #check default
43
+ assert_equal true, contains_one_default?(Parent.first.sons)
44
+ assert_equal true, contains_one_default?(Parent.last.sons)
45
+
46
+ #check default change to last
47
+ Parent.first.sons.last.set_as_default!
48
+ assert_equal true, Parent.first.sons.last.default
49
+ assert_equal true, contains_one_default?(Parent.first.sons)
50
+ assert_equal true, contains_one_default?(Parent.last.sons)
51
+
52
+ #check default change back to first
53
+ Parent.first.sons.first.set_as_default!
54
+ assert_equal true, Parent.first.sons.first.default
55
+ assert_equal true, contains_one_default?(Parent.first.sons)
56
+ assert_equal true, contains_one_default?(Parent.last.sons)
57
+
58
+ #check destroy of non default
59
+ Parent.first.sons.last.destroy!
60
+ assert_equal true, Parent.first.sons.first.default
61
+ assert_equal true, contains_one_default?(Parent.first.sons)
62
+ assert_equal true, contains_one_default?(Parent.last.sons)
63
+
64
+ #check destroy of default
65
+ Parent.first.sons.first.destroy!
66
+ assert_equal true, Parent.first.sons.first.default
67
+ assert_equal true, contains_one_default?(Parent.first.sons)
68
+ assert_equal true, contains_one_default?(Parent.last.sons)
69
+ end
70
+
71
+ def contains_one_default?(collection)
72
+ collection.where(default: true).size == 1
73
+ end
74
+ end
@@ -0,0 +1,35 @@
1
+ class Parent < ActiveRecord::Base
2
+ include ActsAsDefault
3
+ acts_as_default
4
+
5
+ has_many :sons
6
+ end
7
+
8
+ class Son < ActiveRecord::Base
9
+ include ActsAsDefault
10
+ acts_as_default :parent_id
11
+
12
+ belongs_to :parent
13
+ end
14
+
15
+ def setup_objects
16
+ ActiveRecord::Base.establish_connection(
17
+ :adapter => 'sqlite3',
18
+ :database => ':memory:'
19
+ )
20
+
21
+ ActiveRecord::Schema.define do
22
+ self.verbose = false
23
+
24
+ create_table :parents, :force => true do |t|
25
+ t.timestamps
26
+ t.boolean :default
27
+ end
28
+
29
+ create_table :sons, :force => true do |t|
30
+ t.belongs_to :parent
31
+ t.boolean :default
32
+ t.timestamps
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_default
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mirko Mignini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 4.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - mirko.mignini@libero.it
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - acts_as_default.gemspec
97
+ - lib/acts_as_default.rb
98
+ - lib/acts_as_default/version.rb
99
+ - test/test_all.rb
100
+ - test/test_objects.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.0.14
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Using acts as default you can set a default row in your activerecord collection,
125
+ also for has_many relationships
126
+ test_files:
127
+ - test/test_all.rb
128
+ - test/test_objects.rb