join_select 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2896deab13657eb7ac6a2da0d6f2848296599fac
4
+ data.tar.gz: c29dc4b4faa712c5b85ac9678697f160fec1918d
5
+ SHA512:
6
+ metadata.gz: 840773b4bfe1052720834bfc1817404615fc03567ea745d35c0bd098bcfce31e86ba694cf1466af47722ab1f61dfbc90f65d056de40e25ac26a8151793c5928a
7
+ data.tar.gz: 3f697c3eaf97604199137eab6fbf931638d7dfd607551a7689923a01decf61b8a8db0cf1a4d9c9af08eb5e64cf67ff6f33803d93e53f3f9fa5ff0a15d9f8b8f6
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'railties'
4
+
5
+ group :development do
6
+ gem "rspec", '~> 2.10', '> 2.10.0'
7
+ gem "rdoc", '~> 4.0', '> 4.0.0'
8
+ gem "bundler", '~> 1.2', '> 1.2.0'
9
+ gem "jeweler", '~> 1.8', '> 1.8.4'
10
+ gem "simplecov",'~> 0.5'
11
+ end
@@ -0,0 +1,99 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionpack (3.0.8)
6
+ activemodel (= 3.0.8)
7
+ activesupport (= 3.0.8)
8
+ builder (~> 2.1.2)
9
+ erubis (~> 2.6.6)
10
+ i18n (~> 0.5.0)
11
+ rack (~> 1.2.1)
12
+ rack-mount (~> 0.6.14)
13
+ rack-test (~> 0.5.7)
14
+ tzinfo (~> 0.3.23)
15
+ activemodel (3.0.8)
16
+ activesupport (= 3.0.8)
17
+ builder (~> 2.1.2)
18
+ i18n (~> 0.5.0)
19
+ activesupport (3.0.8)
20
+ addressable (2.3.5)
21
+ builder (2.1.2)
22
+ diff-lcs (1.2.5)
23
+ erubis (2.6.6)
24
+ abstract (>= 1.0.0)
25
+ faraday (0.8.7)
26
+ multipart-post (~> 1.1)
27
+ git (1.2.5)
28
+ github_api (0.10.1)
29
+ addressable
30
+ faraday (~> 0.8.1)
31
+ hashie (>= 1.2)
32
+ multi_json (~> 1.4)
33
+ nokogiri (~> 1.5.2)
34
+ oauth2
35
+ hashie (2.0.5)
36
+ highline (1.6.19)
37
+ httpauth (0.2.0)
38
+ i18n (0.5.0)
39
+ jeweler (1.8.6)
40
+ builder
41
+ bundler (~> 1.0)
42
+ git (>= 1.2.5)
43
+ github_api (= 0.10.1)
44
+ highline (>= 1.6.15)
45
+ nokogiri (= 1.5.10)
46
+ rake
47
+ rdoc
48
+ json (1.8.1)
49
+ jwt (0.1.8)
50
+ multi_json (>= 1.5)
51
+ multi_json (1.10.1)
52
+ multi_xml (0.5.4)
53
+ multipart-post (1.2.0)
54
+ nokogiri (1.5.10)
55
+ oauth2 (0.9.2)
56
+ faraday (~> 0.8)
57
+ httpauth (~> 0.2)
58
+ jwt (~> 0.1.4)
59
+ multi_json (~> 1.0)
60
+ multi_xml (~> 0.5)
61
+ rack (~> 1.2)
62
+ rack (1.2.8)
63
+ rack-mount (0.6.14)
64
+ rack (>= 1.0.0)
65
+ rack-test (0.5.7)
66
+ rack (>= 1.0)
67
+ railties (3.0.8)
68
+ actionpack (= 3.0.8)
69
+ activesupport (= 3.0.8)
70
+ rake (>= 0.8.7)
71
+ thor (~> 0.14.4)
72
+ rake (10.1.1)
73
+ rdoc (4.1.2)
74
+ json (~> 1.4)
75
+ rspec (2.13.0)
76
+ rspec-core (~> 2.13.0)
77
+ rspec-expectations (~> 2.13.0)
78
+ rspec-mocks (~> 2.13.0)
79
+ rspec-core (2.13.1)
80
+ rspec-expectations (2.13.0)
81
+ diff-lcs (>= 1.1.3, < 2.0)
82
+ rspec-mocks (2.13.0)
83
+ simplecov (0.7.1)
84
+ multi_json (~> 1.0)
85
+ simplecov-html (~> 0.7.1)
86
+ simplecov-html (0.7.1)
87
+ thor (0.14.6)
88
+ tzinfo (0.3.37)
89
+
90
+ PLATFORMS
91
+ ruby
92
+
93
+ DEPENDENCIES
94
+ bundler (~> 1.2, > 1.2.0)
95
+ jeweler (~> 1.8, > 1.8.4)
96
+ railties
97
+ rdoc (~> 4.0, > 4.0.0)
98
+ rspec (~> 2.10, > 2.10.0)
99
+ simplecov (~> 0.5)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Dileep Nandanam
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/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,27 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "join_select"
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.require_paths = ["lib"]
7
+ s.authors = ["Dileep Nandanam"]
8
+ s.date = "2014-11-20"
9
+ s.description = "gem to wright join queries on associations"
10
+ s.email = "dileepsnandanam@gmail.com"
11
+ s.extra_rdoc_files = [
12
+ "LICENSE.txt"
13
+ ]
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "LICENSE.txt",
18
+ "VERSION",
19
+ "lib/join_select.rb",
20
+ "join_select.gemspec"
21
+ ]
22
+ s.homepage = "http://github.com/dileepnandanam/join_select"
23
+ s.licenses = ["MIT"]
24
+ s.rubygems_version = "2.2.2"
25
+ s.summary = "Use join_select with rails active_record"
26
+ end
27
+
@@ -0,0 +1,64 @@
1
+ ActiveRecord::Base.class_eval do
2
+
3
+ def self.relations_meta(combined_key, association_type)
4
+ combined_key.singularize.camelize.constantize.reflect_on_all_associations(association_type).map{|r| [r.name, {foreign_key: r.options[:foreign_key], through: r.options[:through].to_s, class_name: r.options[:source] || r.plural_name, table_name: r.options[:source]}]}.to_h
5
+ end
6
+
7
+ def self.with(params)
8
+ self.find_with(self, self.table_name, params, self.table_name)
9
+ end
10
+
11
+ def self.find_with(relation, combined_key, params, current_table)
12
+ params.keys.each do |key|
13
+ if params[key].is_a?(Hash)
14
+ has_many_relations = relations_meta(combined_key, :has_many)
15
+ has_one_relations = relations_meta(combined_key, :has_one)
16
+ belongs_to_relations = relations_meta(combined_key, :belongs_to)
17
+ has_and_belongs_to_many_relations = relations_meta(combined_key, :has_and_belongs_to_many)
18
+
19
+ if has_many_relations.keys.include?(key)
20
+ if(has_many_relations[key][:through].present?)
21
+ join_scope = -> (rel) { rel.joins(%{
22
+ inner join #{has_many_relations[key][:through]}
23
+ on #{has_many_relations[key][:through]}.#{current_table.singularize}_id = #{current_table}.id
24
+ inner join #{has_many_relations[key][:class_name]}
25
+ on #{has_many_relations[key][:class_name]}.#{has_many_relations[key][:through].singularize}_id = #{has_many_relations[key][:through]}.id
26
+ })}
27
+ else
28
+ join_scope = -> (rel) { rel.joins(%{
29
+ inner join #{has_many_relations[key][:class_name].underscore.pluralize}
30
+ on #{combined_key}.id = #{has_many_relations[key][:class_name].underscore.pluralize}.#{has_many_relations[key][:foreign_key] || "#{combined_key.singularize}_id"}
31
+ })}
32
+ end
33
+ new_scope = -> (rel) { self.find_with(join_scope.call(relation), has_many_relations[key][:class_name].underscore.pluralize, params[key], has_many_relations[key][:class_name].underscore.pluralize) }
34
+ elsif has_one_relations.keys.include?(key)
35
+ join_scope = -> (rel) { rel.joins(%{
36
+ inner join #{has_one_relations[key][:class_name].underscore.pluralize}
37
+ on #{combined_key}.id = #{has_one_relations[key][:class_name].underscore.pluralize}.#{has_one_relations[key][:foreign_key] || "#{combined_key.singularize}_id"}
38
+ })}
39
+ new_scope = -> (rel) { self.find_with(join_scope.call(relation), has_one_relations[key][:class_name].underscore.pluralize, params[key], has_many_relations[key][:class_name].underscore.pluralize) }
40
+ elsif belongs_to_relations.keys.include?(key)
41
+ join_scope = -> (rel) { rel.joins(%{
42
+ inner join #{belongs_to_relations[key][:class_name].underscore.pluralize}
43
+ on #{combined_key}.#{belongs_to_relations[key][:class_name].underscore.singularize}_id = #{(belongs_to_relations[key][:source] || belongs_to_relations[key][:class_name])}.id
44
+ })}
45
+ new_scope = -> (rel) { self.find_with(join_scope.call(relation), belongs_to_relations[key][:class_name].underscore.pluralize, params[key], belongs_to_relations[key][:class_name].underscore.pluralize) }
46
+ elsif has_and_belongs_to_many_relations.keys.include?(key)
47
+ join_table = has_and_belongs_to_many_relations[key][:join_table] || [combined_key, key].sort.join('_')
48
+ join_scope = -> (rel) { rel.joins(%{
49
+ inner join #{join_table}
50
+ on #{join_table}.#{combined_key.singularize}_id = #{combined_key}.id
51
+ inner join #{has_and_belongs_to_many_relations[key][:class_name].underscore.pluralize}
52
+ on #{join_table}.#{key.singularize}_id = #{has_and_belongs_to_many_relations[key][:class_name].underscore.pluralize}.id
53
+ })}
54
+ new_scope = -> (rel) { self.find_with(join_scope.call(relation), has_and_belongs_to_many_relations[key][:class_name].underscore.pluralize, params[key], has_and_belongs_to_many_relations[key][:class_name].underscore.pluralize) }
55
+ end
56
+
57
+ else
58
+ new_scope = -> (rel) { rel.where( "#{combined_key}.#{key}" => params[key]) }
59
+ end
60
+ relation = new_scope.call(relation)
61
+ end
62
+ relation
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: join_select
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dileep Nandanam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: gem to wright join queries on associations
14
+ email: dileepsnandanam@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - LICENSE.txt
19
+ files:
20
+ - Gemfile
21
+ - Gemfile.lock
22
+ - LICENSE.txt
23
+ - VERSION
24
+ - join_select.gemspec
25
+ - lib/join_select.rb
26
+ homepage: http://github.com/dileepnandanam/join_select
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Use join_select with rails active_record
50
+ test_files: []