forked from rpheath/acts_as_lookup
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 [name of plugin creator] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the acts_as_lookup plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the acts_as_lookup plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'ActsAsLookup' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'acts_as_lookup' | ||
|
||
ActiveRecord::Base.send(:include, RyanHeath::ActsAsLookup) | ||
ActionView::Base.send(:include, RyanHeath::ActsAsLookup::ViewHelpers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Install hook code here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module RPH | ||
module ActsAsLookup | ||
def self.included(base) | ||
base.extend ActMethods | ||
end | ||
|
||
module ActMethods | ||
def acts_as_lookup(field_to_select, optionz={}) | ||
raise(Error::InvalidParams, Error::InvalidParams.message) if field_to_select.blank? | ||
raise(Error::InvalidAttr, Error::InvalidAttr.message) unless self.new.respond_to?(field_to_select) | ||
|
||
options = { | ||
:default_text => '--', | ||
:order => "#{field_to_select.to_s}" | ||
}.merge!(optionz) | ||
|
||
class_inheritable_accessor :options, :field_to_select | ||
extend ClassMethods | ||
|
||
self.options = options | ||
self.field_to_select = field_to_select.to_sym | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def options_for_select | ||
rows = self.find(:all, :conditions => (options[:conditions] || {}), :order => options[:order]) | ||
[[options[:default_text], nil]] + rows.collect { |r| [r.send(field_to_select), r.id] } | ||
end | ||
end | ||
|
||
module ViewHelpers | ||
def lookup_for(obj, f_key, options={}, html_options={}) | ||
begin | ||
klass = f_key.to_s.gsub(/_id$/, '').classify.constantize | ||
rescue NameError | ||
raise(Error::InvalidModel, Error::InvalidModel.message) | ||
end | ||
raise(Error::InvalidLookup, Error::InvalidLookup.message) unless klass && klass.respond_to?(:field_to_select) && klass.respond_to?(:options_for_select) | ||
select(obj.to_sym, f_key.to_sym, klass.options_for_select, options, html_options) | ||
end | ||
end | ||
|
||
module Error | ||
class CustomError < RuntimeError | ||
# getter/setter for setting custom error messages | ||
def self.message(msg=nil); msg.nil? ? @message : self.message = msg; end | ||
def self.message=(msg); @message = msg; end | ||
end | ||
|
||
class InvalidParams < CustomError | ||
message "must pass an attr to `acts_as_lookup' for the selection list (i.e. acts_as_lookup :title)" | ||
end | ||
class InvalidAttr < CustomError | ||
message "attr passed to `acts_as_lookup' does not exist" | ||
end | ||
class InvalidLookup < CustomError | ||
message "model passed to `lookup_for' does not have the `act_as_lookup' declaration" | ||
end | ||
class InvalidModel < CustomError | ||
message "model passed to `lookup_for' does not seem to exist" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# desc "Explaining what the task does" | ||
# task :acts_as_lookup do | ||
# # Task goes here | ||
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test/unit' | ||
|
||
class ActsAsLookupTest < Test::Unit::TestCase | ||
# Replace this with your real tests. | ||
def test_this_plugin | ||
flunk | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Uninstall hook code here |