active_record-deselect 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/README.md +33 -11
- data/lib/active_record/deselect.rb +23 -10
- data/lib/active_record/deselect/version.rb +1 -1
- data/spec/active_record/deselect_spec.rb +17 -0
- metadata +20 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 132d754338138de6eb3c65c48fc6a54ccb1d17f2
|
4
|
+
data.tar.gz: 32e39afffbd04a019e87b050c236f7711068502c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8f22e9c7f5178f3098d548d5e633c278ca4fecc5e4a3d436392137e14732765a01cabb636bd2b160e9b1fefb8a5691e91547e28919fe30e17ecb95365683b6b
|
7
|
+
data.tar.gz: 8e1c15447df029780a2cbb38f1a889057df1fb50d001e0818c9a6da9e023fa03c11182c14d05ce0bc7622d7909283bffbf1a612cacf7bd2f584419dca564d22b
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
[![Build Status](https://travis-ci.org/fukayatsu/active_record-deselect.png?branch=master)](https://travis-ci.org/fukayatsu/active_record-deselect)
|
2
2
|
|
3
|
-
|
3
|
+
# ActiveRecord::Deselect
|
4
|
+
|
5
|
+
Adds deselect logic to ActiveRecord.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -19,17 +21,37 @@ Or install it yourself as:
|
|
19
21
|
## Usage
|
20
22
|
|
21
23
|
```ruby
|
22
|
-
Post
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
4.0.2 @ 2.0.0 (main)>Post
|
25
|
+
#=> class Post < ActiveRecord::Base {
|
26
|
+
:id => :integer,
|
27
|
+
:name => :string,
|
28
|
+
:body => :text,
|
29
|
+
:user_id => :integer,
|
30
|
+
:published_at => :datetime,
|
31
|
+
:created_at => :datetime,
|
32
|
+
:updated_at => :datetime
|
33
|
+
}
|
34
|
+
|
35
|
+
4.0.2 @ 2.0.0 (main)>Post.deselect(:body).to_sql
|
36
|
+
#=> "SELECT id, name, user_id, published_at, created_at, updated_at FROM \"posts\""
|
37
|
+
|
38
|
+
4.0.2 @ 2.0.0 (main)>Post.select(:id, :name, :body, :user_id).deselect(:body, "user_id").to_sql
|
39
|
+
#=> "SELECT id, name FROM \"posts\""
|
40
|
+
|
41
|
+
4.0.2 @ 2.0.0 (main)>Post.select('posts.id', 'posts.name').deselect(:name).to_sql
|
42
|
+
#=> "SELECT posts.id FROM \"posts\""
|
43
|
+
|
44
|
+
4.0.2 @ 2.0.0 (main)>Post.select(:id).deselect(:id).to_sql
|
45
|
+
#=> "SELECT \"posts\".* FROM \"posts\""
|
46
|
+
```
|
27
47
|
|
28
|
-
|
29
|
-
# => [:name, :body]
|
48
|
+
### deselect!
|
30
49
|
|
31
|
-
|
32
|
-
|
50
|
+
```ruby
|
51
|
+
posts = Post.select(:id, :name)
|
52
|
+
posts.deselect!(:name)
|
53
|
+
posts.to_sql
|
54
|
+
#=> "SELECT id FROM \"posts\""
|
33
55
|
```
|
34
56
|
|
35
57
|
## Contributing
|
@@ -2,18 +2,31 @@ require "active_record/deselect/version"
|
|
2
2
|
|
3
3
|
module ActiveRecord
|
4
4
|
module Deselect
|
5
|
-
def deselect(*
|
6
|
-
|
5
|
+
def deselect(*deselect_columns)
|
6
|
+
scope = current_scope || relation
|
7
|
+
select_columns = filter_columns(scope, deselect_columns)
|
8
|
+
scope.except(:select).select(select_columns)
|
9
|
+
end
|
10
|
+
|
11
|
+
def deselect!(*deselect_columns)
|
12
|
+
scope = current_scope || relation
|
13
|
+
select_columns = filter_columns(scope, deselect_columns)
|
14
|
+
scope.select_values = select_columns
|
15
|
+
scope
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
7
19
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
20
|
+
def filter_columns(scope, deselect_columns)
|
21
|
+
current_columns = scope.select_values.presence || column_names.map(&:to_sym)
|
22
|
+
deselect_columns = deselect_columns.map{ |column|
|
23
|
+
column = column.to_s
|
24
|
+
column.include?('.') ? [column] : [column, "#{table_name}.#{column}"]
|
25
|
+
}.flatten.map{ |column|
|
26
|
+
[column.to_s, column.to_sym]
|
27
|
+
}.flatten
|
15
28
|
|
16
|
-
|
29
|
+
current_columns - deselect_columns
|
17
30
|
end
|
18
31
|
end
|
19
32
|
end
|
@@ -21,6 +21,7 @@ describe ActiveRecord::Deselect do
|
|
21
21
|
|
22
22
|
context "with String" do
|
23
23
|
it { expect(Post.select('id', :name, :body).deselect(:id, :name).select_values).to eq([:body]) }
|
24
|
+
it { expect(Post.select(:id, :name, :body).deselect('id', :name).select_values).to eq([:body]) }
|
24
25
|
end
|
25
26
|
|
26
27
|
context "with tablename" do
|
@@ -42,5 +43,21 @@ describe ActiveRecord::Deselect do
|
|
42
43
|
context "with where" do
|
43
44
|
it { expect(Post.where(id: 1).deselect(:id).select_values).to eq([:name, :body]) }
|
44
45
|
end
|
46
|
+
|
47
|
+
context "chain" do
|
48
|
+
it {
|
49
|
+
posts = Post.select(:id, :name, :body).deselect(:name)
|
50
|
+
posts.deselect(:body) # not deselect!
|
51
|
+
expect(posts.select_values).to eq([:id, :body])
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#deselect!' do
|
57
|
+
it 'deselect name from current scope' do
|
58
|
+
posts = Post.select(:id, :name)
|
59
|
+
posts.deselect!(:name)
|
60
|
+
expect(posts.select_values).to eq([:id])
|
61
|
+
end
|
45
62
|
end
|
46
63
|
end
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-deselect
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fukayatsu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 3.0.0.beta1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 3.0.0.beta1
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: sqlite3
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
description: Adds deselect logic to ActiveRecord
|
@@ -101,8 +101,9 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
- .gitignore
|
105
|
-
- .rspec
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
106
107
|
- Gemfile
|
107
108
|
- LICENSE.txt
|
108
109
|
- README.md
|
@@ -122,17 +123,17 @@ require_paths:
|
|
122
123
|
- lib
|
123
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
125
|
requirements:
|
125
|
-
- -
|
126
|
+
- - ">="
|
126
127
|
- !ruby/object:Gem::Version
|
127
128
|
version: '0'
|
128
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
130
|
requirements:
|
130
|
-
- -
|
131
|
+
- - ">="
|
131
132
|
- !ruby/object:Gem::Version
|
132
133
|
version: '0'
|
133
134
|
requirements: []
|
134
135
|
rubyforge_project:
|
135
|
-
rubygems_version: 2.0
|
136
|
+
rubygems_version: 2.2.0
|
136
137
|
signing_key:
|
137
138
|
specification_version: 4
|
138
139
|
summary: Adds deselect logic to ActiveRecord
|