override_active_record_dynamic_finders 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in override_activerecord_dynamic_finders.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Naveen Agarwal
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.
@@ -0,0 +1,40 @@
1
+ # OverrideActiveRecordDynamicFinders
2
+
3
+ Overrides find, find_by_*, find_all_by_*, find_last_by_* and count method for the ActiveRecord module to use the new ActiveRecord relation methods like where, limit, offset etc.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'override_activerecord_dynamic_finders'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install override_activerecord_dynamic_finders
18
+
19
+ ## Usage
20
+
21
+ when upgrading one of the applications from rails 2.3 to 3.2 the onemajor hurdel was to update the ActiveRecord query methods like find(:all, :conditions => "...", select => "..."), find_by_status("active", :conditions => "...") etc.
22
+
23
+ As we had more than 2000 such method calls, so it was not feasible for us to to change each and every query to use new ActiveRecord methods.
24
+
25
+ Then I decided to come-up with a library which lets you use these methods, but behind the secenes these methods use only new ActiveRecord relation methods like where, limit, offset, includes, having etc. Just include this gem in your gemfile and you are good to go.
26
+
27
+ It overrides following methods for the ActiveRecord model class
28
+
29
+ 1. find
30
+ 2. find_by_*
31
+ 3. find_(all|last)_by_*
32
+ 4. count
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ require "override_active_record_dynamic_finders/version"
2
+ require "override_active_record_dynamic_finders/active_record_not_found"
3
+ require "override_active_record_dynamic_finders/class_methods"
4
+
5
+ module OverrideActiveRecordDynamicFinders
6
+
7
+ if defined? ::Rails::Railtie
8
+ class Engine < ::Rails::Railtie
9
+
10
+ config.after_initialize do
11
+ raise "ActiveRecord module not found", ActiveRecordNotFound unless defined? ActiveRecord
12
+ class ActiveRecord::Base
13
+ def self.inherited(subclass)
14
+ subclass.initialize_generated_modules
15
+ super
16
+ subclass.extend OverrideActiveRecordDynamicFinders::ClassMethods
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ else
23
+
24
+ raise ActiveRecordNotFound, "ActiveRecord module not found" unless defined? ActiveRecord
25
+
26
+ class ActiveRecord::Base
27
+ def self.inherited(subclass)
28
+ subclass.initialize_generated_modules
29
+ super
30
+ subclass.extend OverrideActiveRecordDynamicFinders::ClassMethods
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -0,0 +1,2 @@
1
+ class ActiveRecordNotFound < StandardError
2
+ end
@@ -0,0 +1,107 @@
1
+ module OverrideActiveRecordDynamicFinders
2
+ module ClassMethods
3
+
4
+ KEYS_SET = [:joins, :select, :group, :order, :having, :limit, :offset, :conditions, :extend, :include]
5
+ ALL = :all
6
+
7
+ def find(*args)
8
+ if args.first.is_a?(Symbol) && args.last.is_a?(Hash)
9
+ compute_result(*args).send(args.first)
10
+ elsif args.first.is_a?(Symbol) && !args.last.is_a?(Hash)
11
+ result = clone
12
+ result.send(args.first)
13
+ else
14
+ super *args
15
+ end
16
+ end
17
+
18
+ def count(*args)
19
+
20
+ if args.last.is_a?(Hash) && ALL == args.first
21
+ compute_result(*args).count
22
+ elsif args.last.is_a?(Hash) && ALL != args.first
23
+ compute_result(*args).count(args.first)
24
+ elsif args.first.is_a?(Symbol) && ALL == args.first
25
+ count
26
+ else
27
+ super *args
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def compute_result(*args)
34
+ result = clone
35
+
36
+ finder_keys = KEYS_SET & args.last.keys
37
+ finder_hash = args.last
38
+
39
+ finder_keys.each do |key|
40
+ method_name = key
41
+
42
+ method_name = :where if key == :conditions
43
+ method_name = :includes if key == :include
44
+ method_name = :extending if key == :extend
45
+
46
+ result = result.send(method_name, finder_hash[key]) if finder_hash[key].present?
47
+ end
48
+ result
49
+ end
50
+
51
+ def method_missing(method_id, *args, &block)
52
+ method = method_id.to_s
53
+
54
+ if method.start_with?("find_")
55
+
56
+ case method
57
+ when /^find_(all_|last_)?by_([_a-zA-Z]\w*)$/
58
+ finder = :last if $1 == 'last_'
59
+ finder = :all if $1 == 'all_'
60
+ names = $2.split("_and_")
61
+ when /^find_by_([_a-zA-Z]\w*)$/
62
+ names = $1.split("_and_")
63
+ end
64
+
65
+ finder ||= :first
66
+ finder_hash = {}
67
+ finder_hash = args.delete(args.last) if args.last.is_a?(Hash)
68
+
69
+ raise "Invalid number of arguments", ArgumentError if args.size != names.size
70
+
71
+ conditions = nil
72
+
73
+ if finder_hash[:conditions].blank?
74
+ conditions = {}
75
+
76
+ names.each_with_index do |name, index|
77
+ conditions[name.to_sym] = args[index]
78
+ end
79
+
80
+ finder_hash[:conditions] = conditions
81
+
82
+ else
83
+ conditions = []
84
+
85
+ names.each_with_index do |name, index|
86
+ conditions << " #{name} = '#{args[index]}' "
87
+ end
88
+
89
+ conditions = conditions.join(" AND ")
90
+
91
+ if finder_hash[:conditions].is_a?(Array)
92
+ finder_hash[:conditions][0] += " AND " + conditions
93
+ else
94
+ finder_hash[:conditions] += " AND " + conditions
95
+ end
96
+ end
97
+
98
+ find(finder, finder_hash)
99
+
100
+ else
101
+ super method_id, *args, &block
102
+ end
103
+ end # end method missing
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,3 @@
1
+ module OverrideActiveRecordDynamicFinders
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'override_active_record_dynamic_finders/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "override_active_record_dynamic_finders"
8
+ spec.version = OverrideActiveRecordDynamicFinders::VERSION
9
+ spec.authors = ["Naveen Agarwal"]
10
+ spec.email = ["naveenagarwal287@gmail.com"]
11
+ spec.description = %q{Overrides dynamic finders in ActiveRecord module to use new Activerecord relation metho}
12
+ spec.summary = %q{Overrides dynamic finders in ActiveRecord module to use new Activerecord relation metho}
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 "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: override_active_record_dynamic_finders
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Naveen Agarwal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Overrides dynamic finders in ActiveRecord module to use new Activerecord
47
+ relation metho
48
+ email:
49
+ - naveenagarwal287@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/override_active_record_dynamic_finders.rb
60
+ - lib/override_active_record_dynamic_finders/active_record_not_found.rb
61
+ - lib/override_active_record_dynamic_finders/class_methods.rb
62
+ - lib/override_active_record_dynamic_finders/version.rb
63
+ - override_active_record_dynamic_finders.gemspec
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.25
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Overrides dynamic finders in ActiveRecord module to use new Activerecord
89
+ relation metho
90
+ test_files: []