-
Notifications
You must be signed in to change notification settings - Fork 4
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
27 changed files
with
606 additions
and
577 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,18 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
bin/ | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
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 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in hashdown.gemspec | ||
gemspec |
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 @@ | ||
Copyright (c) 2012 Solomon White | ||
|
||
MIT License | ||
|
||
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,93 @@ | ||
# Hashdown | ||
|
||
Hashdown is a super lightweight rails plugin that adds hash-style lookups and | ||
option lists (for generating dropdowns) to ActiveRecord models | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'hashdown' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install hashdown | ||
|
||
## Usage | ||
|
||
Given the following class definition: | ||
|
||
class State < ActiveRecord::Base | ||
finder :abbreviation | ||
end | ||
|
||
You can look up states via their abbreviations like so: | ||
|
||
@colorado = State[:CO] | ||
|
||
By default, calling the finder method with a token that does not exist in the | ||
database will result in an ActiveRecord::RecordNotFount exception being thrown. | ||
This can be overridden by adding a :default option to your finder declaration. | ||
|
||
class PurchaseOrder < ActiveRecord::Base | ||
finder :number, :default => nil | ||
end | ||
|
||
In this case, PurchaseOrder['00734'] will silently return nil if that number is | ||
not found. | ||
|
||
These types of reference data models are often something you need to populate a | ||
select list on your form, so hashdown includes a method to generate your option | ||
list: | ||
|
||
1. Declare your model to be selectable: | ||
|
||
class State < ActiveRecord::Base | ||
selectable | ||
end | ||
|
||
2. Call select_options in your form to return a set of name, value pairs to | ||
pass into a select builder: | ||
|
||
<%= form.select :state_id, State.select_options %> | ||
|
||
By default, selectable will return the :id of the record as the value, and the | ||
:name attribute value as the display. This can be overridden inline in the | ||
select_options call: | ||
|
||
State.select_options(:name, :abbreviation) | ||
|
||
The grouped_options_for_select format is also supported: | ||
|
||
State.select_options(:group => :region) | ||
|
||
Adding finder and selectable to a model is roughly equivalent to the following | ||
implementation: | ||
|
||
class State < ActiveRecord::Base | ||
validates_uniqueness_of :abbreviation | ||
|
||
def self.[](state_code) | ||
find_by_abbreviation(state_code) | ||
end | ||
|
||
def self.select_options | ||
find(:all).map{|s| [s.name, s.id] } | ||
end | ||
end | ||
|
||
...except hashdown adds configuration for flexibility and caching for speedy | ||
lookups. | ||
|
||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Added some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,34 +1,13 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
require 'rcov/rcovtask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the hashdown plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.libs << 'test' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Test coverage report for the hashdown plugin.' | ||
Rcov::RcovTask.new(:coverage) do |t| | ||
t.libs << "test" | ||
t.test_files = FileList["test/*_test.rb"] | ||
t.output_dir = "test/coverage/" | ||
t.verbose = true | ||
t.rcov_opts << '--exclude "/Library/*"' | ||
end | ||
|
||
desc 'Generate documentation for the hashdown plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'Hashdown' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README.markdown') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
require 'bundler/gem_tasks' | ||
require 'rspec/core/rake_task' | ||
|
||
desc "Run specs" | ||
task :spec do | ||
RSpec::Core::RakeTask.new(:spec) do |t| | ||
t.rspec_opts = %w{--colour --format progress} | ||
t.pattern = 'spec/**/*_spec.rb' | ||
end | ||
end | ||
|
||
desc "Default: run specs." | ||
task :default => :spec |
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,24 @@ | ||
# -*- encoding: utf-8 -*- | ||
require File.expand_path('../lib/hashdown/version', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.authors = ["Solomon White"] | ||
gem.email = ["rubysolo@gmail.com"] | ||
gem.description = %q{Hashdown} | ||
gem.summary = %q{super lightweight Rails plugin that adds hash-style lookups and option list (for generating dropdowns) to ActiveRecord models} | ||
gem.homepage = "https://github.com/rubysolo/hashdown" | ||
|
||
gem.add_dependency 'activerecord', '~> 3.0' | ||
|
||
gem.add_development_dependency 'pry-nav' | ||
gem.add_development_dependency 'rake' | ||
gem.add_development_dependency 'rspec' | ||
gem.add_development_dependency 'sqlite3' | ||
|
||
gem.files = `git ls-files`.split($\) | ||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } | ||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) | ||
gem.name = "hashdown" | ||
gem.require_paths = ["lib"] | ||
gem.version = Hashdown::VERSION | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,40 +1,3 @@ | ||
require 'activesupport' unless defined? ActiveSupport | ||
require 'activerecord' unless defined? ActiveRecord | ||
require 'finder' | ||
require 'selectable' | ||
|
||
module Rubysolo # :nodoc: | ||
module Hashdown | ||
def self.included(base) # :nodoc: | ||
base.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
def finder(attr_name, options={}) | ||
class << self | ||
attr_accessor :finder_options | ||
end | ||
self.finder_options = options.merge(:attribute => attr_name) | ||
|
||
self.send :include, Finder | ||
end | ||
|
||
def selectable(options={}) | ||
class << self | ||
attr_accessor :selectable_options | ||
end | ||
self.selectable_options = options | ||
|
||
self.send :include, Selectable | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.force_cache_miss? | ||
defined?(Rails) && Rails.env.test? | ||
end | ||
|
||
|
||
end # Hashdown | ||
end # Rubysolo | ||
require "hashdown/version" | ||
require "hashdown/finder" | ||
require "hashdown/select_options" |
Oops, something went wrong.