active_record-deselect 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: 948f3c1f8a00d8623058254e5a5383b56ca42e59
4
+ data.tar.gz: a1064da89b31313e0101a2f1764538775c376402
5
+ SHA512:
6
+ metadata.gz: 4b7965e8f1771ca2d692b5735e91c786dd1c5a8777443576f1e4d21d8b2b02383b0f573bf9e3ccaf505e0f1a633c24508ec2f53065628058d231d23c444738ef
7
+ data.tar.gz: b7ba701e5f781f096785b34bc6bd1057bbf35859108df97d6467dfe372582a4a5b81ac3ae27fa75fd602824f12f140601b004e222c5e39f1094fffafb6e9fee3
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record_deselect.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 fukayatsu
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,41 @@
1
+ # ActiveRecordDeselect
2
+
3
+ Adds deselect logic to ActiveRecord
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_record-deselect'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_record-deselect
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ Post.column_names
23
+ # => ["id", "name", "body"]
24
+
25
+ Post.select(:id, :name, :body).deselect(:id, :name).select_values
26
+ # => [:body]
27
+
28
+ Post.all.deselect(:id).select_values
29
+ # => [:name, :body]
30
+
31
+ Post.deselect(:id).select_values
32
+ # => [:name, :body]
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/deselect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_record-deselect"
8
+ spec.version = ActiveRecord::Deselect::VERSION
9
+ spec.authors = ["fukayatsu"]
10
+ spec.email = ["fukayatsu@gmail.com"]
11
+ spec.description = %q{Adds deselect logic to ActiveRecord}
12
+ spec.summary = %q{Adds deselect logic to ActiveRecord}
13
+ spec.homepage = "https://github.com/fukayatsu/active_record-deselect"
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_dependency 'activerecord', '> 3.0.0'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 3.0.0.beta1"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "sqlite3"
27
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module Deselect
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require "active_record/deselect/version"
2
+
3
+ module ActiveRecord
4
+ module Deselect
5
+ def deselect(*columns)
6
+ s = current_scope || relation
7
+
8
+ select_columns = (s.select_values.presence || column_names.map(&:to_sym)) -
9
+ columns.map{ |column|
10
+ column = column.to_s
11
+ column.include?('.') ? [column] : [column, "#{table_name}.#{column}"]
12
+ }.flatten.map{ |column|
13
+ [column.to_s, column.to_sym]
14
+ }.flatten
15
+
16
+ s.except(:select).select(select_columns)
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Querying.send(:include, ActiveRecord::Deselect)
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ ActiveRecord::Base.silence_stream(STDOUT) do
4
+ ActiveRecord::Schema.define do
5
+ create_table :posts do |t|
6
+ t.string :name
7
+ t.text :body
8
+ end
9
+ end
10
+ end
11
+
12
+ class Post < ActiveRecord::Base; end
13
+
14
+ describe ActiveRecord::Deselect do
15
+ describe '#deselect' do
16
+ context 'with select' do
17
+ it { expect(Post.select(:id).deselect(:name).select_values).to eq([:id]) }
18
+ it { expect(Post.select(:id, :name).deselect(:name).select_values).to eq([:id]) }
19
+ it { expect(Post.select(:id, :name, :body).deselect(:name).select_values).to eq([:id, :body]) }
20
+ it { expect(Post.select(:id, :name, :body).deselect(:id, :name).select_values).to eq([:body]) }
21
+
22
+ context "with String" do
23
+ it { expect(Post.select('id', :name, :body).deselect(:id, :name).select_values).to eq([:body]) }
24
+ end
25
+
26
+ context "with tablename" do
27
+ it { expect(Post.select('posts.id', :name).deselect('posts.id').select_values).to eq([:name]) }
28
+ it { expect(Post.select('posts.id', :name).deselect('id').select_values).to eq([:name]) }
29
+ it { expect(Post.select('posts.id', :name).deselect(:id).select_values).to eq([:name]) }
30
+ it { expect(Post.select(:'posts.id', :name).deselect('id').select_values).to eq([:name]) }
31
+ end
32
+ end
33
+
34
+ context "with all" do
35
+ it { expect(Post.all.deselect(:id).select_values).to eq([:name, :body]) }
36
+ end
37
+
38
+ context "with model" do
39
+ it { expect(Post.deselect(:id).select_values).to eq([:name, :body]) }
40
+ end
41
+
42
+ context "with where" do
43
+ it { expect(Post.where(id: 1).deselect(:id).select_values).to eq([:name, :body]) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'active_record/deselect'
4
+ require 'pry'
5
+
6
+
7
+ ActiveRecord::Base.establish_connection(
8
+ adapter: 'sqlite3',
9
+ database: ':memory:'
10
+ )
11
+
12
+ RSpec.configure do |config|
13
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-deselect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fukayatsu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-19 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.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.0
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0.beta1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0.beta1
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Adds deselect logic to ActiveRecord
98
+ email:
99
+ - fukayatsu@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - active_record-deselect.gemspec
111
+ - lib/active_record/deselect.rb
112
+ - lib/active_record/deselect/version.rb
113
+ - spec/active_record/deselect_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: https://github.com/fukayatsu/active_record-deselect
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.0.3
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Adds deselect logic to ActiveRecord
139
+ test_files:
140
+ - spec/active_record/deselect_spec.rb
141
+ - spec/spec_helper.rb