easy_arel 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.md +17 -0
- data/Rakefile +1 -0
- data/easy_arel.gemspec +23 -0
- data/lib/easy_arel/version.rb +3 -0
- data/lib/easy_arel.rb +28 -0
- data/test/easy_arel_test.rb +53 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Easy Arel
|
2
|
+
|
3
|
+
With `EasyArel` instead of:
|
4
|
+
|
5
|
+
Thing.arel_table[:created_at]
|
6
|
+
|
7
|
+
you can say
|
8
|
+
|
9
|
+
Thing[:created_at]
|
10
|
+
|
11
|
+
`EasyArel` also creates class & instance methods named `_` that reference your class, So in your model you can be all like
|
12
|
+
|
13
|
+
scope :recent, order(_[:created_at].desc)
|
14
|
+
|
15
|
+
You can't just use the brackets in this case, because that would just be an array. It's stupid, but I didn't feel like experimenting with any crazy operator overloading. I'm open to suggestions on making this better.
|
16
|
+
|
17
|
+
I doubt this is even a good idea, but whatever, tests pass.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/easy_arel.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "easy_arel/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "easy_arel"
|
7
|
+
s.version = EasyArel::VERSION
|
8
|
+
s.authors = ["Jason L Perry"]
|
9
|
+
s.email = ["jasper@ambethia.com"]
|
10
|
+
s.homepage = "https://github.com/ambethia/easy_arel"
|
11
|
+
s.summary = %q{Shorthand for `arel_table`}
|
12
|
+
s.description = %q{Shorthand references to `arel_table` in ActiveRecord for lazy people, like me.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "easy_arel"
|
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_runtime_dependency 'activerecord', '~> 3'
|
22
|
+
s.add_runtime_dependency 'sqlite3-ruby'
|
23
|
+
end
|
data/lib/easy_arel.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "easy_arel/version"
|
2
|
+
|
3
|
+
module EasyArel
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
# Shorthand access to Arel attributes
|
9
|
+
def [] name
|
10
|
+
arel_table[name]
|
11
|
+
end
|
12
|
+
|
13
|
+
def _
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module InstanceMethods
|
19
|
+
|
20
|
+
def _
|
21
|
+
self.class._
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveSupport.on_load(:active_record) do
|
27
|
+
include EasyArel
|
28
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'active_support/test_case'
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/migration'
|
7
|
+
require 'easy_arel'
|
8
|
+
|
9
|
+
ActiveRecord::Base.configurations = {
|
10
|
+
'test' => { adapter: 'sqlite3', database: ":memory:" }
|
11
|
+
}
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection('test')
|
14
|
+
|
15
|
+
class CreateThings < ActiveRecord::Migration
|
16
|
+
|
17
|
+
def self.up
|
18
|
+
create_table :things, force: true do |t|
|
19
|
+
t.timestamps
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
CreateThings.migrate(:up)
|
25
|
+
|
26
|
+
class Thing < ActiveRecord::Base
|
27
|
+
|
28
|
+
scope :recent, order(_[:created_at].desc)
|
29
|
+
end
|
30
|
+
|
31
|
+
class EasyArelTest < ActiveSupport::TestCase
|
32
|
+
|
33
|
+
test 'sanity' do
|
34
|
+
assert_kind_of Module, EasyArel
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'arel_table is aliased' do
|
38
|
+
assert_equal Thing.arel_table[:created_at], Thing[:created_at]
|
39
|
+
end
|
40
|
+
|
41
|
+
test 'silly underscore is self' do
|
42
|
+
assert_equal Thing, Thing._
|
43
|
+
end
|
44
|
+
|
45
|
+
test 'silly underscore is for instances too' do
|
46
|
+
assert_equal Thing, Thing.new._
|
47
|
+
end
|
48
|
+
|
49
|
+
test 'can be used to do sexy stuff' do
|
50
|
+
sql = Thing.order(Thing.arel_table[:created_at].desc).to_sql
|
51
|
+
assert_equal sql, Thing.recent.to_sql
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_arel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason L Perry
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-25 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "3"
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: sqlite3-ruby
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Shorthand references to `arel_table` in ActiveRecord for lazy people, like me.
|
38
|
+
email:
|
39
|
+
- jasper@ambethia.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- easy_arel.gemspec
|
52
|
+
- lib/easy_arel.rb
|
53
|
+
- lib/easy_arel/version.rb
|
54
|
+
- test/easy_arel_test.rb
|
55
|
+
homepage: https://github.com/ambethia/easy_arel
|
56
|
+
licenses: []
|
57
|
+
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
hash: 3482211662166887035
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3482211662166887035
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: easy_arel
|
84
|
+
rubygems_version: 1.8.5
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Shorthand for `arel_table`
|
88
|
+
test_files:
|
89
|
+
- test/easy_arel_test.rb
|