acts_as_has_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: 095ce5a710211eec1ab541f2b95d24e9f1d08d94
4
+ data.tar.gz: 6569f6299743894cc87f05de6edcb26c1db18e4f
5
+ SHA512:
6
+ metadata.gz: ac7a9ac70d0a1e855de440d710f8bbb0d2eb3c31e7d1b94a7518d0ddd5454e3e328d6617f6ba7b31d1ad87c67181f282048f9274684c4672b5d3abca9cbec2ab
7
+ data.tar.gz: d6fb01920f6aee2634c1da49f6508c646e7359cb88c27f9bbd03dbce0898d211c5cb6c34a85953f1abe4b71104d11bd6e080dcee9f9b3421dc099f04e611aa7d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Iliya Grushevskiy <iliya.gr@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = ActsAsHasDefault
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsAsHasDefault'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,60 @@
1
+ module ActiveRecord
2
+ module Acts
3
+
4
+ module Default
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ def acts_as_has_default(options = {})
10
+ configuration = {column: 'default', scope: []}
11
+ configuration.update(options) if options.is_a?(Hash)
12
+
13
+ configuration[:scope] = [configuration[:scope]] if configuration[:scope].is_a? Symbol
14
+
15
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
+
17
+ def scope_condition_for_#{configuration[:column]}
18
+ scope = ::ActiveRecord::VERSION::MAJOR == 4 ? self.class.all : self.class.scoped
19
+ %w(#{configuration[:scope].join(' ')}).each do |attr|
20
+ scope = scope.where(attr.intern => self[attr.intern])
21
+ end
22
+ scope
23
+ end
24
+
25
+ RUBY
26
+
27
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
28
+
29
+ def update_#{configuration[:column]}_as_default
30
+ unless #{configuration[:column]}
31
+ self.#{configuration[:column]} = scope_condition_for_#{configuration[:column]}.where(self.class.arel_table[:id].not_eq(id)).empty?
32
+ end
33
+
34
+ true
35
+ end
36
+
37
+ def invalidate_#{configuration[:column]}_as_default
38
+ scope_condition_for_#{configuration[:column]}.where(self.class.arel_table[:id].not_eq(id)).update_all(['`#{configuration[:column]}` = ?', false]) if #{configuration[:column]}
39
+ end
40
+
41
+ def elect_#{configuration[:column]}_as_default
42
+ unless scope_condition_for_#{configuration[:column]}.where(self.class.arel_table[:id].not_eq(id)).empty?
43
+ target = scope_condition_for_#{configuration[:column]}.where(self.class.arel_table[:id].not_eq(id)).first
44
+ target.update_attributes(#{configuration[:column]}: true) if target
45
+ end
46
+ end
47
+
48
+ RUBY
49
+
50
+ before_save "update_#{configuration[:column]}_as_default".intern, if: configuration[:if], unless: configuration[:unless]
51
+ after_save "invalidate_#{configuration[:column]}_as_default".intern, if: configuration[:if], unless: configuration[:unless]
52
+ after_destroy "elect_#{configuration[:column]}_as_default".intern, if: configuration[:if], unless: configuration[:unless]
53
+ end
54
+
55
+ end # ClassMethods
56
+
57
+ end # Default
58
+
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsHasDefault
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ require 'acts_as_has_default/active_record/acts/default'
2
+
3
+ module ActsAsHasDefault
4
+
5
+ if defined? Rails::Railtie
6
+ require 'rails'
7
+ class Railtie < Rails::Railtie
8
+ initializer 'acts_as_has_default.insert_into_active_record' do
9
+ ActiveSupport.on_load :active_record do
10
+ ActsAsHasDefault::Railtie.insert
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class Railtie
17
+ def self.insert
18
+ if defined?(ActiveRecord)
19
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::Default)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActsAsHasDefault::Railtie.insert
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_has_default do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_has_default
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Iliya Grushevskiy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
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: factory_girl_rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This "acts_as" extension provides the capabilities for selecting default
70
+ model according to scope.
71
+ email:
72
+ - iliya.gr@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - lib/acts_as_has_default/active_record/acts/default.rb
78
+ - lib/acts_as_has_default/version.rb
79
+ - lib/acts_as_has_default.rb
80
+ - lib/tasks/acts_as_has_default_tasks.rake
81
+ - MIT-LICENSE
82
+ - Rakefile
83
+ - README.rdoc
84
+ homepage: http://github.com/iliya-gr/acts_as_has_default
85
+ licenses: []
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.0.3
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: A gem allowing active_record model has default value.
107
+ test_files: []