Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpheath committed May 1, 2008
1 parent 5a58ab1 commit 78e1c63
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
@@ -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.
22 changes: 22 additions & 0 deletions Rakefile
@@ -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
4 changes: 4 additions & 0 deletions init.rb
@@ -0,0 +1,4 @@
require 'acts_as_lookup'

ActiveRecord::Base.send(:include, RyanHeath::ActsAsLookup)
ActionView::Base.send(:include, RyanHeath::ActsAsLookup::ViewHelpers)
1 change: 1 addition & 0 deletions install.rb
@@ -0,0 +1 @@
# Install hook code here
65 changes: 65 additions & 0 deletions lib/acts_as_lookup.rb
@@ -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
4 changes: 4 additions & 0 deletions tasks/acts_as_lookup_tasks.rake
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :acts_as_lookup do
# # Task goes here
# end
8 changes: 8 additions & 0 deletions test/acts_as_lookup_test.rb
@@ -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
1 change: 1 addition & 0 deletions uninstall.rb
@@ -0,0 +1 @@
# Uninstall hook code here

0 comments on commit 78e1c63

Please sign in to comment.