acts_as_finder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .project
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_finder.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tomasz Gieniusz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # ActsAsFinder
2
+
3
+ This `acts_as` extension provides the capabilities for beautiful record finding by specified column(s).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'acts_as_finder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install acts_as_finder
18
+
19
+ ## Example Usage
20
+
21
+ Assume you have following model:
22
+
23
+ class Language < ActiveRecord::Base
24
+ acts_as_finder :short_name, :name
25
+ end
26
+
27
+ And records in database:
28
+
29
+ +----+------------+---------+
30
+ | id | short_name | name |
31
+ +----+------------+---------+
32
+ | 1 | EN | English |
33
+ | 2 | PL | Polish |
34
+ +----+------------+---------+
35
+
36
+ Now you can use this extension like this:
37
+
38
+ >> Language.EN
39
+ => #<Language id: 1, short_name: "EN", name: "English">
40
+
41
+ >> Language.Polish
42
+ => #<Language id: 2, short_name: "PL", name: "Polish">
43
+
44
+ ## Build Status
45
+ [![Build Status](https://secure.travis-ci.org/tomgi/acts_as_finder.png)](https://secure.travis-ci.org/tomgi/acts_as_finder)
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
53
+ 5. Push to the branch (`git push origin my-new-feature`)
54
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/acts_as_finder/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tomasz Gieniusz"]
6
+ gem.email = ["tomasz.gieniusz@gmail.com"]
7
+ gem.description = %q{This `acts_as` extension provides the capabilities for beautiful finding record in database by specified column.}
8
+ gem.summary = %q{A gem allowing beautiful finding records by specified column.}
9
+ gem.homepage = "http://github.com/tomgi/acts_as_finder"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "acts_as_finder"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActiveRecord::Acts::Finder::VERSION
17
+
18
+ gem.add_dependency "activerecord"
19
+
20
+ gem.add_development_dependency "bundler"
21
+ gem.add_development_dependency "sqlite3"
22
+ gem.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,3 @@
1
+ require "acts_as_finder/active_record/acts/finder"
2
+
3
+ ActiveRecord::Base.send(:include, ActiveRecord::Acts::Finder)
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Finder
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def acts_as_finder(*fields)
10
+ instance_eval <<-EOV
11
+ @acts_as_finder_fields ||= []
12
+ @acts_as_finder_fields += #{fields}
13
+ def method_missing(sym, *args, &block)
14
+ begin
15
+ super
16
+ rescue => e
17
+ @acts_as_finder_fields.each do |field|
18
+ el = where(field.to_sym => sym).first
19
+ return el if el
20
+ end
21
+ raise e
22
+ end
23
+ end
24
+
25
+ def methods
26
+ @acts_as_finder_fields.inject(super) do |acc, field|
27
+ acc += all.map { |r| r.send(field).to_sym }
28
+ end
29
+ end
30
+ EOV
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Finder
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "acts_as_finder" do
4
+ it "finds record by given field" do
5
+ Currency.EUR.should == Currency.find_by_short_name('EUR')
6
+ end
7
+
8
+ context "tries to invoke method from upper class before finding" do
9
+ it "returns base result if base method exists" do
10
+ ActiveRecord::Base.should_receive(:EUR).and_return(:ok)
11
+ Currency.EUR.should == :ok
12
+ end
13
+ it "raises error if base method and record don't exist" do
14
+ expect{Currency.TEST}.to raise_error(NameError)
15
+ end
16
+ end
17
+
18
+ context "can find by many fields" do
19
+ context "#invoking acts_as_finder once passing two fields" do
20
+ it "returns record found by first field if exists" do
21
+ Language.EN.should == Language.find_by_short_name('EN')
22
+ end
23
+
24
+ it "searches by second field if nothing was found by first field " do
25
+ Language.English.should == Language.find_by_name('English')
26
+ end
27
+ end
28
+
29
+ context "#invoking acts_as_finder twice with different fields" do
30
+ it "returns record found by first field if exists" do
31
+ Country.EN.should == Country.find_by_short_name('EN')
32
+ end
33
+
34
+ it "searches by second field if nothing was found by first field " do
35
+ Country.England.should == Country.find_by_name('England')
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'has records fields values included in #methods' do
41
+ it 'one field' do
42
+ Currency.methods.should include(:EUR, :PLN)
43
+ end
44
+
45
+ it 'two fields' do
46
+ Language.methods.should include(:EN, :PL, :English, :Polish)
47
+ end
48
+
49
+ it 'includes also base methods' do
50
+ ActiveRecord::Base.should_receive(:methods).and_return([:ok])
51
+ Language.methods.should include(:ok)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'active_record'
13
+ require 'acts_as_finder'
14
+
15
+ ActiveRecord::Base.establish_connection(
16
+ :adapter => "sqlite3",
17
+ :database => ":memory:"
18
+ )
19
+
20
+ require 'support/schema.rb'
21
+ require 'support/models.rb'
22
+ require 'support/data.rb'
@@ -0,0 +1,7 @@
1
+ Currency.create(:short_name => "EUR")
2
+ Currency.create(:short_name => "PLN")
3
+
4
+ Language.create(:short_name => "EN", :name => "English")
5
+ Language.create(:short_name => "PL", :name => "Polish")
6
+
7
+ Country.create(:short_name => "EN", :name => "England")
@@ -0,0 +1,12 @@
1
+ class Currency < ActiveRecord::Base
2
+ acts_as_finder :short_name
3
+ end
4
+
5
+ class Language < ActiveRecord::Base
6
+ acts_as_finder :short_name, :name
7
+ end
8
+
9
+ class Country < ActiveRecord::Base
10
+ acts_as_finder :short_name
11
+ acts_as_finder :name
12
+ end
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :currencies, :force => true do |t|
5
+ t.string :short_name
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :languages, :force => true do |t|
10
+ t.string :short_name
11
+ t.string :name
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :countries, :force => true do |t|
16
+ t.string :short_name
17
+ t.string :name
18
+ t.timestamps
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tomasz Gieniusz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This `acts_as` extension provides the capabilities for beautiful finding
79
+ record in database by specified column.
80
+ email:
81
+ - tomasz.gieniusz@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - .rspec
88
+ - Gemfile
89
+ - LICENSE
90
+ - README.md
91
+ - Rakefile
92
+ - acts_as_finder.gemspec
93
+ - lib/acts_as_finder.rb
94
+ - lib/acts_as_finder/active_record/acts/finder.rb
95
+ - lib/acts_as_finder/version.rb
96
+ - spec/acts_as_finder_spec.rb
97
+ - spec/spec_helper.rb
98
+ - spec/support/data.rb
99
+ - spec/support/models.rb
100
+ - spec/support/schema.rb
101
+ homepage: http://github.com/tomgi/acts_as_finder
102
+ licenses: []
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.24
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: A gem allowing beautiful finding records by specified column.
125
+ test_files:
126
+ - spec/acts_as_finder_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/data.rb
129
+ - spec/support/models.rb
130
+ - spec/support/schema.rb