wildcard_scopes 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.rdoc +29 -0
- data/Rakefile +7 -0
- data/lib/wildcard_scopes.rb +10 -0
- data/lib/wildcard_scopes/base.rb +30 -0
- data/lib/wildcard_scopes/railtie.rb +10 -0
- data/lib/wildcard_scopes/version.rb +3 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/wildcard_scopes/wildcard_scopes_spec.rb +8 -0
- data/wildcard_scopes.gemspec +27 -0
- metadata +163 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= WildcardScopes
|
2
|
+
|
3
|
+
Use ActiveRecord scopes with dynamic names.
|
4
|
+
|
5
|
+
= Example
|
6
|
+
|
7
|
+
# config/initializers/wildcard_scopes.rb
|
8
|
+
|
9
|
+
# Global scopes
|
10
|
+
Avers::Application.config.wildcard_scopes = proc {
|
11
|
+
# Wildcard scope should be wrapped in a proc. a is array of matches.
|
12
|
+
wildcard_scope /descend_by_(.*)/, proc { |a| order("#{table_name}.#{a.first} DESC") }
|
13
|
+
}
|
14
|
+
|
15
|
+
# app/models/user.rb
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
wildcard_scope /ascend_by_(.*)/, proc { |a| order("#{table_name}.#{a.first} ASC") }
|
18
|
+
end
|
19
|
+
|
20
|
+
User.ascend_by_name.to_sql # SELECT "users".* FROM "users" ORDER BY users.name ASC
|
21
|
+
User.ascend_by_position.to_sql # SELECT "users".* FROM "users" ORDER BY users.position ASC
|
22
|
+
User.descend_by_name.to_sql # SELECT "users".* FROM "users" ORDER BY users.name DESC
|
23
|
+
|
24
|
+
= Notes
|
25
|
+
|
26
|
+
1. This gem is created to simplify migration from SearchLogic to Ransack.
|
27
|
+
2. Use of global scopes is not a good idea at all.
|
28
|
+
3. Association extensions are not yet allowed.
|
29
|
+
4. Tested with Rails 3.1, Ruby 1.8.7 and 1.9.3.
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/class/attribute'
|
4
|
+
require 'rails/railtie'
|
5
|
+
|
6
|
+
module WildcardScopes
|
7
|
+
require 'wildcard_scopes/version'
|
8
|
+
require 'wildcard_scopes/base'
|
9
|
+
require 'wildcard_scopes/railtie' if defined?(Rails)
|
10
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module WildcardScopes
|
2
|
+
module Base
|
3
|
+
extend ::ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :wildcard_scopes
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def wildcard_scope(name_regex, *args)
|
11
|
+
self.wildcard_scopes ||= {}
|
12
|
+
self.wildcard_scopes[name_regex] = args
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(method, *args, &block)
|
16
|
+
super if self.wildcard_scopes.blank?
|
17
|
+
self.wildcard_scopes.each do |name, scope_params|
|
18
|
+
matchdata = name.match(method.to_s)
|
19
|
+
if matchdata
|
20
|
+
self.class_eval do
|
21
|
+
scope method.to_sym, self.instance_exec(matchdata.to_a.slice(1..-1), &scope_params.first)
|
22
|
+
end
|
23
|
+
return self.send(method)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module WildcardScopes
|
2
|
+
class Railtie < ::Rails::Railtie
|
3
|
+
config.wildcard_scopes = proc { }
|
4
|
+
|
5
|
+
ActiveSupport.on_load :active_record do
|
6
|
+
ActiveRecord::Base.send(:include, WildcardScopes::Base)
|
7
|
+
ActiveRecord::Base.instance_exec(&WildcardScopes::Railtie.config.wildcard_scopes)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH << "." unless $LOAD_PATH.include?(".")
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'wildcard_scopes'
|
6
|
+
|
7
|
+
WildcardScopes::Railtie.config.wildcard_scopes = proc {
|
8
|
+
ActiveRecord::Base.wildcard_scope /ascend_by_(.*)/, proc { |a| order("#{table_name}.#{a.first} ASC") }
|
9
|
+
}
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(
|
12
|
+
"adapter" => "sqlite3",
|
13
|
+
"database" => ":memory:"
|
14
|
+
)
|
15
|
+
|
16
|
+
ActiveRecord::Schema.define :version => 0 do
|
17
|
+
create_table "users", :force => true do |t|
|
18
|
+
t.datetime :created_at
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
wildcard_scope /descend_by_(.*)/, proc { |a| order("#{table_name}.#{a.first} DESC") }
|
24
|
+
end
|
25
|
+
|
26
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WildcardScopes do
|
4
|
+
it "local & global wildcard scopes works" do
|
5
|
+
User.ascend_by_created_at.to_sql.should == 'SELECT "users".* FROM "users" ORDER BY users.created_at ASC'
|
6
|
+
User.descend_by_created_at.to_sql.should == 'SELECT "users".* FROM "users" ORDER BY users.created_at DESC'
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "wildcard_scopes/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "wildcard_scopes"
|
7
|
+
s.version = WildcardScopes::VERSION
|
8
|
+
s.authors = ["Victor Sokolov"]
|
9
|
+
s.email = ["gzigzigzeo@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{ActiveRecord scopes with dynamic names}
|
12
|
+
s.description = %q{ActiveRecord scopes with dynamic names}
|
13
|
+
|
14
|
+
s.rubyforge_project = "wildcard_scopes"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "sqlite3"
|
24
|
+
s.add_dependency "activerecord", "~> 3.0"
|
25
|
+
s.add_dependency "activesupport", "~> 3.0"
|
26
|
+
s.add_dependency "railties", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wildcard_scopes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Victor Sokolov
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-09 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: sqlite3
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activerecord
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 7
|
71
|
+
segments:
|
72
|
+
- 3
|
73
|
+
- 0
|
74
|
+
version: "3.0"
|
75
|
+
type: :runtime
|
76
|
+
version_requirements: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
name: activesupport
|
79
|
+
prerelease: false
|
80
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 7
|
86
|
+
segments:
|
87
|
+
- 3
|
88
|
+
- 0
|
89
|
+
version: "3.0"
|
90
|
+
type: :runtime
|
91
|
+
version_requirements: *id005
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: railties
|
94
|
+
prerelease: false
|
95
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 7
|
101
|
+
segments:
|
102
|
+
- 3
|
103
|
+
- 0
|
104
|
+
version: "3.0"
|
105
|
+
type: :runtime
|
106
|
+
version_requirements: *id006
|
107
|
+
description: ActiveRecord scopes with dynamic names
|
108
|
+
email:
|
109
|
+
- gzigzigzeo@gmail.com
|
110
|
+
executables: []
|
111
|
+
|
112
|
+
extensions: []
|
113
|
+
|
114
|
+
extra_rdoc_files: []
|
115
|
+
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- README.rdoc
|
120
|
+
- Rakefile
|
121
|
+
- lib/wildcard_scopes.rb
|
122
|
+
- lib/wildcard_scopes/base.rb
|
123
|
+
- lib/wildcard_scopes/railtie.rb
|
124
|
+
- lib/wildcard_scopes/version.rb
|
125
|
+
- spec/spec_helper.rb
|
126
|
+
- spec/wildcard_scopes/wildcard_scopes_spec.rb
|
127
|
+
- wildcard_scopes.gemspec
|
128
|
+
homepage: ""
|
129
|
+
licenses: []
|
130
|
+
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
none: false
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
hash: 3
|
151
|
+
segments:
|
152
|
+
- 0
|
153
|
+
version: "0"
|
154
|
+
requirements: []
|
155
|
+
|
156
|
+
rubyforge_project: wildcard_scopes
|
157
|
+
rubygems_version: 1.8.10
|
158
|
+
signing_key:
|
159
|
+
specification_version: 3
|
160
|
+
summary: ActiveRecord scopes with dynamic names
|
161
|
+
test_files:
|
162
|
+
- spec/spec_helper.rb
|
163
|
+
- spec/wildcard_scopes/wildcard_scopes_spec.rb
|