rails_select_on_includes 0.1.0

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: 760c7ce5db03df38f8ad642bc0b72f71be1a1ec0
4
+ data.tar.gz: 71806ae57830758f1bda4ed45e3df7c92ebd2eae
5
+ SHA512:
6
+ metadata.gz: be0c07732e03e4a573948a00dbe1215d019e31ddf4eb4357a767f5c1cf984807471f218d66db5beda73f4d839792c15583335c71a58678e79072b9b3ea600639
7
+ data.tar.gz: 4a7c5c76780c6f7a8dc70b0a9b77975c948c1b9a459ba04badcf030428399e406c53413510eeda622a04143788c7b71f6d28df78d258560407667688b7aad75f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.idea
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_select_on_includes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 alekseyl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # RailsSelectOnIncludes
2
+
3
+ This gem solves issue in rails: https://github.com/rails/rails/issues/15185 for base_class.
4
+
5
+ It was impossible to select virtual attributes to object from its relations or any other way
6
+ when using includes and where.
7
+
8
+ Example from upper rails issue:
9
+
10
+ ```ruby
11
+ post = Post.includes(:comments).select("posts.*, 1 as testval").where( SOME_CONDITION ).first
12
+ post.testval # Undefined method!
13
+ ```
14
+
15
+ This gem solves problem for base class i.e.
16
+
17
+ ```ruby
18
+ post = Post.includes(:comments).select("posts.*, 1 as testval").where( SOME_CONDITION ).first
19
+ post.testval # 1
20
+ ```
21
+
22
+ but of course it doesn't include virtual attributes in included relations
23
+
24
+ ```ruby
25
+ post = Post.includes(:comments).select("posts.*, 1 as testval").where( SOME_CONDITION ).first
26
+ post.comments.first.testval # Undefined method!
27
+ ```
28
+
29
+ ## Installation
30
+
31
+ Add this line to your application's Gemfile:
32
+
33
+ ```ruby
34
+ gem 'rails_select_on_includes'
35
+ ```
36
+
37
+ And then execute:
38
+
39
+ $ bundle
40
+
41
+ Or install it yourself as:
42
+
43
+ $ gem install rails_select_on_includes
44
+
45
+ ## Usage
46
+
47
+ Works out of the box, gently monkey-patching base-class alias columns. It not affecting query creation, since query already contains all columns.
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_select_on_includes.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_select_on_includes"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,95 @@
1
+ require 'active_support/deprecation'
2
+ require 'active_support/core_ext/string/filters'
3
+
4
+ module ActiveRecord
5
+ module Associations
6
+ class JoinDependency # :nodoc:
7
+
8
+ class Aliases # :nodoc:
9
+ def initialize(tables)
10
+ @tables = tables
11
+ @alias_cache = tables.each_with_object({}) { |table,h|
12
+ h[table.node] = table.columns.each_with_object({}) { |column,i|
13
+ i[column.name] = column.alias
14
+ }
15
+ }
16
+ @name_and_alias_cache = tables.each_with_object({}) { |table,h|
17
+ h[table.node] = table.columns.map { |column|
18
+ [column.name, column.alias]
19
+ }
20
+ @base_class_node_aliases ||= h[table.node] if table.node.is_a?(ActiveRecord::Associations::JoinDependency::JoinBase)
21
+ }
22
+ end
23
+ # valid formats are:
24
+ # 1 'table_name.column' or 'table_name.column as column_1' will be parsed! distinct on can be used also
25
+ # 2 {table_name: column} or { table_name: [column1, column2] }
26
+ # 3 table_name: 2
27
+ def update_aliases_to_select_values( select_values )
28
+ return if select_values.blank?
29
+ select_values.each do |sv|
30
+ # if sv is symbol that we assume that its a base table column and it will be aliased and added as usual
31
+ # all we need is some specials joins+select from related tables
32
+ if sv.is_a?(Hash)
33
+ flatten_hash_values(sv).each{|sub_sv| @base_class_node_aliases << [sub_sv, sub_sv] }
34
+ elsif sv.is_a?(String)
35
+ # this is the case of long raw select
36
+ sv.split( ", " ).each do |sub_sv|
37
+ if sub_sv[/.+ as .+/]
38
+ selected_column = sub_sv[/ as .+/][4..-1]
39
+ @base_class_node_aliases << [selected_column, selected_column]
40
+ elsif sub_sv[/.+\.[^\*]+/]
41
+ selected_column = sub_sv[/\..+/][1..-1]
42
+ @base_class_node_aliases << [selected_column, selected_column]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+ def flatten_hash_values( some_hash )
51
+ some_hash.values.map{ |value| value.is_a?(Hash) ? all_hash_values( value ) : value }.flatten!
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ module ActiveRecord
58
+ module FinderMethods
59
+
60
+ def find_with_associations
61
+ # NOTE: the JoinDependency constructed here needs to know about
62
+ # any joins already present in `self`, so pass them in
63
+ #
64
+ # failing to do so means that in cases like activerecord/test/cases/associations/inner_join_association_test.rb:136
65
+ # incorrect SQL is generated. In that case, the join dependency for
66
+ # SpecialCategorizations is constructed without knowledge of the
67
+ # preexisting join in joins_values to categorizations (by way of
68
+ # the `has_many :through` for categories).
69
+ #
70
+ join_dependency = construct_join_dependency(joins_values)
71
+
72
+ aliases = join_dependency.aliases
73
+ relation = select aliases.columns
74
+ relation = apply_join_dependency(relation, join_dependency)
75
+
76
+ if block_given?
77
+ yield relation
78
+ else
79
+ if ActiveRecord::NullRelation === relation
80
+ []
81
+ else
82
+ arel = relation.arel
83
+ rows = connection.select_all(arel, 'SQL', arel.bind_values + relation.bind_values)
84
+ #DISTINCTION IS HERE:
85
+ # now we gently mokey-patching existing column aliases with select values
86
+ aliases.update_aliases_to_select_values(values[:select]) unless values[:select].blank?
87
+
88
+ join_dependency.instantiate(rows, aliases)
89
+ end
90
+ end
91
+ end
92
+
93
+ end
94
+ end
95
+
@@ -0,0 +1,3 @@
1
+ module RailsSelectOnIncludes
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_select_on_includes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_select_on_includes"
8
+ spec.version = RailsSelectOnIncludes::VERSION
9
+ spec.authors = ["alekseyl"]
10
+ spec.email = ["leshchuk@gmail.com"]
11
+
12
+ spec.summary = %q{Patching rails include/select/virtual attributes issue}
13
+ spec.description = %q{Patching rails include/select/virtual attributes issue ( https://github.com/rails/rails/issues/15185 )}
14
+ spec.homepage = "https://github.com/alekseyl/rails_select_on_includes"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "activerecord", ">=4.1"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.13"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_select_on_includes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alekseyl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-23 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: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Patching rails include/select/virtual attributes issue ( https://github.com/rails/rails/issues/15185
56
+ )
57
+ email:
58
+ - leshchuk@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".idea/.name"
65
+ - ".idea/misc.xml"
66
+ - ".idea/modules.xml"
67
+ - ".idea/rails_select_on_includes.iml"
68
+ - ".idea/vcs.xml"
69
+ - ".idea/workspace.xml"
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - lib/rails_select_on_includes.rb
77
+ - lib/rails_select_on_includes/version.rb
78
+ - rails_select_on_includes.gemspec
79
+ homepage: https://github.com/alekseyl/rails_select_on_includes
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ allowed_push_host: https://rubygems.org
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Patching rails include/select/virtual attributes issue
104
+ test_files: []