magic_scopes 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +51 -0
- data/lib/magic_scopes/scopes_generators/boolean.rb +2 -2
- data/lib/magic_scopes/scopes_generators/mixins/equality_scopes.rb +1 -1
- data/lib/magic_scopes/scopes_generators/mixins/order_scopes.rb +2 -2
- data/lib/magic_scopes/scopes_generators/mixins/presence_scopes.rb +2 -2
- data/lib/magic_scopes/scopes_generators/state.rb +2 -2
- data/lib/magic_scopes/standard_scopes.rb +5 -5
- data/lib/magic_scopes/version.rb +1 -1
- metadata +14 -44
- data/README.rdoc +0 -55
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c5d9345a05c0300bf73e385b9b5295d48becd2b7
|
4
|
+
data.tar.gz: 568857c953e14d9c0e3016e1511a13d521890a85
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fe3ec398d18b7adc935177eefd3859f2a80ee4f06e2499129f81cb7e997ecc78717ac93c9dbb67f0c1a4f120f37a59d62ceb696f33b4667b5f9482c9b95bf9a
|
7
|
+
data.tar.gz: bf5401a1ee1d38bb16365db4a3663b7a41ffe265fa22634f5ca04cbe7dee96291b6c91711581f83626f17a5a6d3e06197e6d9a61a4cdaf485104db5c28df144d
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# MagicScopes
|
2
|
+
|
3
|
+
MagicScopes is the scopes generator for ActiveRecord.
|
4
|
+
It adds magic_scopes method (and nothing else) to ActiveRecord::Base.
|
5
|
+
It depends on ActiveRecord and ActiveSupport and can be used without Rails.
|
6
|
+
MagicScopes module should be included manually in the latter case.
|
7
|
+
It also has support for the StateMachine gem and can generate scopes for states.
|
8
|
+
|
9
|
+
Code speaks much louder than words, so I show you some usage examples at first.
|
10
|
+
|
11
|
+
## Usage examples
|
12
|
+
|
13
|
+
* Call to `magic_scopes` without parameters generates all possible scopes for all attributes, belongs_to associations and states. It also generates asc, sorted, desc, recent, random standard scopes (read further for details). By specifying parameters you tell magic_scopes to generate only the scopes you need.
|
14
|
+
|
15
|
+
* You can customize change by specifying list of attributes and options, (all options accept strings, symbols and arrays as arguments).
|
16
|
+
|
17
|
+
* `magic_scopes :title, :bogus` generates all possible scopes for title and bogus attributes.
|
18
|
+
|
19
|
+
* You can specify in (stands for include) and ex (for exclude) options, so:
|
20
|
+
* `magic_scopes in: %w(with by), std: %w(recent random)` generates 'with' and 'by' scopes for all suitable attributes and 'recent' and 'random' standard scopes.
|
21
|
+
* `magic_scopes ex: %w(with by)` generates all possible scopes except 'with' and 'by' ones when 'ex' option is specified.
|
22
|
+
* `magic_scopes :title, :bogus, in: %w(with by)` generates 'with' and 'by' scopes for all suitable attributes in specified list when both attributes list and in options are cpecified:
|
23
|
+
* You can also override scopes that should be generated per attribute:
|
24
|
+
`magic_scopes :title, :rating, bogus: :by_desc, in: %w(with by)` generates with and by scopes for title and rating attributes and by_desc scope for bogus attribute.
|
25
|
+
* Also you can specify scope name for scope type per attribute, like so: `magic_scopes :title, rating: {by_desc: :by_popularity}`. It generates all possible scopes for title attribute and 'by_popularity' scope that sorts relation by rating, desc.
|
26
|
+
|
27
|
+
|
28
|
+
## MagicScopes can generate these scopes:
|
29
|
+
|
30
|
+
* **with** and **without** options generates scopes such as **with_rating**, **without_rating** that will look for non-null and null values, respectively. For state attributes they redefine corresponding state_machine scopes so they can be called without parameters to check against null and non-null. (You can use it in the normal way too, of course). These scopes can be used with boolean, state, integer, decimal, time, date, datetime, float and string attributes. For integer, decimal, time, date, datetime and string attributes value(s) can be passed to the scope so the check will be performed against passed values (not against null/non-null vakues). (ex.: with_rating(1,2,3), without_rating([5,4,3]))
|
31
|
+
* **is** and **not** options generates scopes such as **published**, **not_published** ("is" is the one option that is not reflected in the name of the generated scope) that look for true and and false or null values for boolean attributes and for the specified state and for state that is not the specifieed one no null for state attributes.
|
32
|
+
* **eq** (stands for equal), **ne** (not equal), **gt** (greater than), **gte** (greater than or equal), **lt** (lesser than), **lte** (lesser than or equal) options generate scopes with corresponding suffixes and accept list of values or array as parameteters. Options names are self explanatory. These scopes can be used with integer, decimal, time, date and datetime attributes. lt and gt options can be used with float attributes.
|
33
|
+
* for string attributes **like** and **ilike** options can be specified which generate scopes like **title_like**, **title_ilike** which accept list or array of arguments and look for values like the specified ones, case sensitive and insensitive, respectively.
|
34
|
+
* **by** and **by_desc** options can be used with integer, decimal, time, date, datetime, float and string attributes and generate scopes like **by_created_at** and **by_created_at_desc**, which sort by created at asc and created at desc, respectively.
|
35
|
+
* **for** and **not_for** scopes can be used with belongs_to associations (including polymorphic ones) and generate scopes like **for_user**, **not_for_commentable**. They can accept list or array of associations. Also, hash(es) with id and type keys can be specified for polymorphic associations.
|
36
|
+
|
37
|
+
|
38
|
+
## Standard scopes
|
39
|
+
|
40
|
+
In addition, some standard scopes can be specified using :std option, they are:
|
41
|
+
asc and sorted sort by id asc, desc and recent sort by id desc and random sorts in random order.
|
42
|
+
ex.: magic_scopes :title, :bogus, in: %w(with without), std: %w(recent rendom)
|
43
|
+
magic_scopes generate all possible standard scopes unless std option is specified.
|
44
|
+
|
45
|
+
MagicScopes is thoroughly tested and ready for production.
|
46
|
+
It works with ActiveRecord >= 3.0 and ruby 1.9. (1.8 is not supported).
|
47
|
+
|
48
|
+
## Authors
|
49
|
+
* Dmitry Afanasyev (dimarzio1986@gmail.com), please do not hesitate to contact me if you have any questions or suggestions.
|
50
|
+
|
51
|
+
This project rocks and uses MIT-LICENSE.
|
@@ -4,11 +4,11 @@ module MagicScopes
|
|
4
4
|
include PresenceScopes
|
5
5
|
|
6
6
|
def is(name)
|
7
|
-
scope name || @attr, where("#{@key}" => true)
|
7
|
+
scope name || @attr, -> { where("#{@key}" => true) }
|
8
8
|
end
|
9
9
|
|
10
10
|
def not(name)
|
11
|
-
scope name || "not_#{@attr}", where("#{@key}" => [false, nil])
|
11
|
+
scope name || "not_#{@attr}", -> { where("#{@key}" => [false, nil]) }
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module MagicScopes
|
2
2
|
module OrderScopes
|
3
3
|
def by(name)
|
4
|
-
scope name || "by_#{@attr}", order("#{@attr} ASC")
|
4
|
+
scope name || "by_#{@attr}", -> { order("#{@attr} ASC") }
|
5
5
|
end
|
6
6
|
|
7
7
|
def by_desc(name)
|
8
|
-
scope name || "by_#{@attr}_desc", order("#{@attr} DESC")
|
8
|
+
scope name || "by_#{@attr}_desc", -> { order("#{@attr} DESC") }
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module MagicScopes
|
2
2
|
module PresenceScopes
|
3
3
|
def with(name)
|
4
|
-
scope name || "with_#{@attr}", where("#{@key} IS NOT NULL")
|
4
|
+
scope name || "with_#{@attr}", -> { where("#{@key} IS NOT NULL") }
|
5
5
|
end
|
6
6
|
|
7
7
|
def without(name)
|
8
|
-
scope name || "without_#{@attr}", where("#{@key} IS NULL")
|
8
|
+
scope name || "without_#{@attr}", -> { where("#{@key} IS NULL") }
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -8,11 +8,11 @@ if defined?(StateMachine)
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def is(name)
|
11
|
-
scope name || @state, where("#{@key}" => @state)
|
11
|
+
scope name || @state, -> { where("#{@key}" => @state) }
|
12
12
|
end
|
13
13
|
|
14
14
|
def not(name)
|
15
|
-
scope name || "not_#{@state}", where("#{@key} != ? OR #{@key} IS NULL", @state)
|
15
|
+
scope name || "not_#{@state}", -> { where("#{@key} != ? OR #{@key} IS NULL", @state) }
|
16
16
|
end
|
17
17
|
|
18
18
|
def with(name)
|
@@ -9,23 +9,23 @@ module MagicScopes
|
|
9
9
|
private
|
10
10
|
|
11
11
|
def asc_scope
|
12
|
-
scope :asc, order(:id)
|
12
|
+
scope :asc, -> { order(:id) }
|
13
13
|
end
|
14
14
|
|
15
15
|
def sorted_scope
|
16
|
-
scope :sorted, order(:id)
|
16
|
+
scope :sorted, -> { order(:id) }
|
17
17
|
end
|
18
18
|
|
19
19
|
def desc_scope
|
20
|
-
scope :desc, order('id DESC')
|
20
|
+
scope :desc, -> { order('id DESC') }
|
21
21
|
end
|
22
22
|
|
23
23
|
def recent_scope
|
24
|
-
scope :recent, order('id DESC')
|
24
|
+
scope :recent, -> { order('id DESC') }
|
25
25
|
end
|
26
26
|
|
27
27
|
def random_scope
|
28
|
-
scope :random, order('RANDOM()')
|
28
|
+
scope :random, -> { order('RANDOM()') }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/magic_scopes/version.rb
CHANGED
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magic_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Dmitry Afanasyev
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: activesupport
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rails
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,65 +55,43 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: sqlite3
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - '>='
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - '>='
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: state_machine
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - '>='
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - '>='
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: rspec-rails
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
|
-
- -
|
87
|
+
- - '>='
|
100
88
|
- !ruby/object:Gem::Version
|
101
89
|
version: '0'
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
|
-
- -
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '0'
|
110
|
-
- !ruby/object:Gem::Dependency
|
111
|
-
name: pry-debugger
|
112
|
-
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
|
-
requirements:
|
115
|
-
- - ! '>='
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
|
-
requirements:
|
123
|
-
- - ! '>='
|
94
|
+
- - '>='
|
124
95
|
- !ruby/object:Gem::Version
|
125
96
|
version: '0'
|
126
97
|
description: ActiveRecord scopes generators
|
@@ -149,30 +120,29 @@ files:
|
|
149
120
|
- lib/magic_scopes.rb
|
150
121
|
- MIT-LICENSE
|
151
122
|
- Rakefile
|
152
|
-
- README.
|
123
|
+
- README.md
|
153
124
|
homepage: http://github.com/icrowley
|
154
125
|
licenses: []
|
126
|
+
metadata: {}
|
155
127
|
post_install_message:
|
156
128
|
rdoc_options: []
|
157
129
|
require_paths:
|
158
130
|
- lib
|
159
131
|
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
-
none: false
|
161
132
|
requirements:
|
162
|
-
- -
|
133
|
+
- - '>='
|
163
134
|
- !ruby/object:Gem::Version
|
164
135
|
version: '0'
|
165
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
-
none: false
|
167
137
|
requirements:
|
168
|
-
- -
|
138
|
+
- - '>='
|
169
139
|
- !ruby/object:Gem::Version
|
170
140
|
version: '0'
|
171
141
|
requirements: []
|
172
142
|
rubyforge_project:
|
173
|
-
rubygems_version:
|
143
|
+
rubygems_version: 2.0.0
|
174
144
|
signing_key:
|
175
|
-
specification_version:
|
145
|
+
specification_version: 4
|
176
146
|
summary: ActiveRecord scopes generators
|
177
147
|
test_files: []
|
178
148
|
has_rdoc:
|
data/README.rdoc
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
= MagicScopes
|
2
|
-
|
3
|
-
MagicScopes is the scopes generator for ActiveRecord.
|
4
|
-
|
5
|
-
It adds magic_scopes method (and nothing else) to ActiveRecord::Base.
|
6
|
-
|
7
|
-
It depends on ActiveRecord and ActiveSupport and can be used without Rails.
|
8
|
-
MagicScopes module should be included manually in the latter case.
|
9
|
-
|
10
|
-
It also has support for the StateMachine gem and can generate scopes for states.
|
11
|
-
|
12
|
-
|
13
|
-
== MagicScopes can generate these scopes:
|
14
|
-
|
15
|
-
* <b>with</b> and <b>without</b> options will generate scopes such as <b>with_rating</b>, <b>without_rating</b> that will look for non null and null values, respectively. For state attributes thee redefine corresponding state_machine scopes so they can be called without parameters to check against null and non null. (You can use it in the normal way too, of course). These scopes can be used with boolean, state, integer, decimal, time, date, datetime, float and string attributes. For integer, decimal, time, date, datetime and string attributes value(s) can be passed to the scope so the check will be performed against passed values (not against null/non null vakues). (ex.: with_rating(1,2,3), without_rating([5,4,3]))
|
16
|
-
* <b>is</b> and <b>not</b> options will generate scopes such as <b>published</b>, <b>not_published</b> ("is" is the one option that is not reflected in the name of the generated scope) that look for true and and false or null values for boolean attributes and for the specified state and for state that is not the specifieed one no null for state attributes.
|
17
|
-
* <b>eq</b> (stands for equal), <b>ne</b> (not equal), <b>gt</b> (greater than), <b>gte</b> (greater than or equal), <b>lt</b> (lesser than), <b>lte</b> (lesser than or equal) options generate scopes with corresponding suffixes and accept list of values or array as parameteters. Options names are self explanatory. These scopes can be used with integer, decimal, time, date and datetime attributes. lt and gt options can be used with float attributes.
|
18
|
-
* for string attributes <b>like</b> and <b>ilike</b> options can be specified which generate scopes like <b>title_like</b>, <b>title_ilike</b> which accept list or array of arguments and look for values like the specified ones, case sensitive and insensitive, respectively.
|
19
|
-
* <b>by</b> and <b>by_desc</b> options can be used with integer, decimal, time, date, datetime, float and string attributes and generate scopes like <b>by_created_at</b> and <b>by_created_at_desc</b>, which sort by created at asc and created at desc, respectively.
|
20
|
-
* <b>for</b> and <b>not_for</b> scopes can be used with belongs_to associations (including polymorphic ones) and generate scopes like <b>for_user</b>, <b>not_for_commentable</b>. They can accept list or array of associations. Also, hash(es) with id and type keys can be specified for polymorphic associations.
|
21
|
-
|
22
|
-
|
23
|
-
== Usage examples
|
24
|
-
|
25
|
-
* Call to magic_scopes without parameters will generate all possible scopes for all attributes, belongs_to associations and states. By specifying parameters you tell magic_scopes to generate only the scopes you need.
|
26
|
-
magic_scopes
|
27
|
-
|
28
|
-
* With specified attributes it will generate all possible scopes for title and bogus attributes.
|
29
|
-
magic_scopes :title, :bogus
|
30
|
-
|
31
|
-
* You can specify in (stands for include) and ex (for exclude) options, so:
|
32
|
-
* it will generate with and by scopes for all suitable attributes when in option is specified.
|
33
|
-
magic_scopes in: %w(with by)
|
34
|
-
* It will generate generate all possible scopes except with and by ones when ex option is specified.
|
35
|
-
magic_scopes ex: %w(with by)
|
36
|
-
* It will generate with and by scopes for all suitable attributes in specified list when both attributes list and in options are cpecified:
|
37
|
-
magic_scopes :title, :bogus, in: %w(with by)
|
38
|
-
* You can also override scopes that should be generated per attribute:
|
39
|
-
magic_scopes :title, :rating, bogus: :by_desc, in: %w(with by)
|
40
|
-
It will generate with and by scopes for title and rating attributes and by_desc scope for bogus attribute.
|
41
|
-
|
42
|
-
* All options accept strings, symbols and arrays as arguments.
|
43
|
-
|
44
|
-
|
45
|
-
== Standard scopes
|
46
|
-
|
47
|
-
In addition, some standard scopes can be specified using :std option, they are:
|
48
|
-
asc and sorted sort by id asc, desc and recent sort by id desc and random sorts in random order.
|
49
|
-
ex.: magic_scopes :title, :bogus, in: %w(with without), std: %w(recent rendom)
|
50
|
-
magic_scopes generate all possible standard scopes unless std option is specified.
|
51
|
-
|
52
|
-
MagicScopes is thoroughly tested and ready for production.
|
53
|
-
It works with ActiveRecord >= 3.0 and ruby 1.9. (1.8 is not supported).
|
54
|
-
|
55
|
-
This project rocks and uses MIT-LICENSE.
|