activerecord_plurals 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/MIT-LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +44 -0
- data/activerecord_plurals.gemspec +33 -0
- data/install.rb +1 -0
- data/lib/activerecord_plurals.rb +59 -0
- data/rails/init.rb +1 -0
- data/uninstall.rb +1 -0
- metadata +73 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Activerecord Plurals
|
2
|
+
A simple rails plugin for batch-retrieving specific fields of activerecord models.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
`rails plugin install git://github.com/gurdotan/activerecord_plurals.git`
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
For example, assume we have a table of animals, assocsiated to the `Animal` ActiveRecord model. We can do the following:
|
9
|
+
|
10
|
+
>> Animal.names
|
11
|
+
=> ["Horse", "Cat", "Dog", "Chimpanzee", "Snake"]
|
12
|
+
|
13
|
+
>> Animal.ids
|
14
|
+
=> [1, 2, 3, 4, 5]
|
15
|
+
|
16
|
+
>> Animal.where(:type => "domestic").names
|
17
|
+
=> ["Cat", "Dog"]
|
18
|
+
|
19
|
+
So in fact this plugin supplies you with plural methods for all activerecord fields. That includes all of the model's fields, along with `ids`, `created_ats` and `updated_ats`.
|
20
|
+
|
21
|
+
### Known Issues
|
22
|
+
* If you have an activerecord model with a field who's plural form collides with an already exsiting method (say 'entry' collides with `Object#entries`), you won't be able to invoke a plural method, the original method will be called.
|
23
|
+
|
24
|
+
These issues will be fixed in the next release.
|
25
|
+
|
26
|
+
|
27
|
+
### Testing Notes
|
28
|
+
* To run the tests:
|
29
|
+
cd <RAILS_ROOT>/vendor/plugins/activerecord_plurals
|
30
|
+
rake
|
31
|
+
* The tests are written in rspec and make use of a private SQLite3 DB file. This is why you will need to run `bundle install` in order to run the tests. Bundler will install the sqlite3-ruby gem which is a dependency of the tests.
|
32
|
+
* Tested only on Rails 3.
|
33
|
+
|
34
|
+
Knock yourselves out!
|
35
|
+
|
36
|
+
Copyright (c) 2011 Gur Dotan, released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
|
5
|
+
|
6
|
+
desc 'Default: run specs.'
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
|
10
|
+
desc 'Test the activerecord_plurals plugin.'
|
11
|
+
RSpec::Core::RakeTask.new do |t|
|
12
|
+
t.rspec_opts = ["-fn", "-c"]
|
13
|
+
t.pattern = 'spec/**/*_spec.rb'
|
14
|
+
t.verbose = true
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
PKG_FILES = FileList[
|
19
|
+
'[a-zA-Z]*',
|
20
|
+
'generators/**/*',
|
21
|
+
'lib/**/*',
|
22
|
+
'rails/**/*',
|
23
|
+
'tasks/**/*',
|
24
|
+
'test/**/*'
|
25
|
+
]
|
26
|
+
|
27
|
+
spec = Gem::Specification.new do |s|
|
28
|
+
s.name = "activerecord_plurals"
|
29
|
+
s.version = "0.0.1"
|
30
|
+
s.author = "Gur Dotan"
|
31
|
+
s.email = "gurdotan@gmail.com"
|
32
|
+
s.platform = Gem::Platform::RUBY
|
33
|
+
s.summary = "Plural attribute methods: Account.ids, Account.names ..."
|
34
|
+
s.files = PKG_FILES.to_a
|
35
|
+
s.require_path = "lib"
|
36
|
+
s.has_rdoc = false
|
37
|
+
s.extra_rdoc_files = ["README.md"]
|
38
|
+
end
|
39
|
+
|
40
|
+
desc 'Turn this plugin into a gem.'
|
41
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
42
|
+
pkg.gem_spec = spec
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
PKG_FILES = FileList[
|
7
|
+
'[a-zA-Z]*',
|
8
|
+
'generators/**/*',
|
9
|
+
'lib/**/*',
|
10
|
+
'rails/**/*',
|
11
|
+
'tasks/**/*',
|
12
|
+
'test/**/*'
|
13
|
+
]
|
14
|
+
|
15
|
+
Gem::Specification.new do |s|
|
16
|
+
s.name = "activerecord_plurals"
|
17
|
+
s.version = "0.0.1"
|
18
|
+
s.platform = Gem::Platform::RUBY
|
19
|
+
s.authors = "Gur Dotan"
|
20
|
+
s.email = "gurdotan@gmail.com"
|
21
|
+
s.homepage = "http://github.com/gurdotan/activerecord_plurals"
|
22
|
+
s.summary = "Plural attribute methods for ActiveRecord"
|
23
|
+
s.description = "Want all the IDs, or names, or whatever field of your ActiveRecord model? Account.ids, Account.names ..."
|
24
|
+
|
25
|
+
s.required_rubygems_version = ">= 1.3.6"
|
26
|
+
s.add_development_dependency "rspec"
|
27
|
+
|
28
|
+
s.files = PKG_FILES.to_a
|
29
|
+
s.require_path = 'lib'
|
30
|
+
s.has_rdoc = false
|
31
|
+
s.extra_rdoc_files = ["README.md"]
|
32
|
+
end
|
33
|
+
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class << ActiveRecord::Base
|
2
|
+
|
3
|
+
alias_method :singular_method_missing, :method_missing
|
4
|
+
|
5
|
+
# Define an around alias for method_missing: if the given method call is a plural,
|
6
|
+
# return an array of attribute values for that method in singular form
|
7
|
+
# Otherwise, fallback to the original method missing
|
8
|
+
def method_missing(method_id, *args, &block)
|
9
|
+
singular_method_id = method_id.to_s.singularize.to_sym
|
10
|
+
|
11
|
+
if method_id == singular_method_id
|
12
|
+
singular_method_missing(method_id, *args, &block)
|
13
|
+
else
|
14
|
+
self.select(singular_method_id).map(&singular_method_id)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
alias_method :singular_respond_to?, :respond_to?
|
20
|
+
|
21
|
+
# Respond to all plurals methods as well
|
22
|
+
def respond_to?(*args)
|
23
|
+
return true if singular_respond_to?(*args)
|
24
|
+
singular_method_id = args[0].to_s.singularize.to_sym
|
25
|
+
instance_methods.include?(singular_method_id)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# This monkey-patches the array class to catch plural method calls.
|
32
|
+
# It assumes we're running in a rails environment that provides us with
|
33
|
+
# String#singularize.
|
34
|
+
# The limitation that this method imposes on rails applications is
|
35
|
+
# that you cannot have a column with a name that collides with methods
|
36
|
+
# defined on Array (or on its ancestor).
|
37
|
+
# i.e. if you have a model named "Diary" with the column "entry", calling
|
38
|
+
# Diary.entries will invoke the underlying Object#entries method. This is
|
39
|
+
# undesired behavior and it is up to the developer to wisely avoid these cases.
|
40
|
+
class Array
|
41
|
+
|
42
|
+
def method_missing(method_id, *args, &block)
|
43
|
+
singular_method_id = method_id.to_s.singularize.to_sym
|
44
|
+
if method_id != singular_method_id
|
45
|
+
return self.map(&singular_method_id)
|
46
|
+
end
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
# Respond also to methods on arrays of ActiveRecord instances.
|
51
|
+
# Note: There is intentionally no restriction on the uniformity of instances in the array,
|
52
|
+
# i.e. they don't have to all be of the same class. This is useful when you have a mixed array
|
53
|
+
# with objects that share the same attributes (Dog, Labrador < Dog for example) and follows ruby's
|
54
|
+
# duck-typing conventions.
|
55
|
+
def respond_to?(symbol, include_private=false)
|
56
|
+
super or all?{|e| e.is_a?(ActiveRecord::Base) && e.class.respond_to?(symbol)}
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'activerecord_plurals'
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord_plurals
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gur Dotan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-11 00:00:00 +03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
description: Want all the IDs, or names, or whatever field of your ActiveRecord model? Account.ids, Account.names ...
|
28
|
+
email: gurdotan@gmail.com
|
29
|
+
executables: []
|
30
|
+
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- README.md
|
35
|
+
files:
|
36
|
+
- uninstall.rb
|
37
|
+
- Rakefile
|
38
|
+
- README.md
|
39
|
+
- activerecord_plurals.gemspec
|
40
|
+
- MIT-LICENSE
|
41
|
+
- install.rb
|
42
|
+
- lib/activerecord_plurals.rb
|
43
|
+
- rails/init.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/gurdotan/activerecord_plurals
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.3.6
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.5.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Plural attribute methods for ActiveRecord
|
72
|
+
test_files: []
|
73
|
+
|