Permalink
Browse files

Reworked gem file and code structure to make it load properly in Rails 3

  • Loading branch information...
1 parent d3264fa commit dd4f89e49762e64b1c7e69fd346737f9da370946 @kavu kavu committed Nov 23, 2010
Showing with 162 additions and 136 deletions.
  1. +1 −0 Rakefile
  2. +12 −11 crummy.gemspec
  3. +1 −2 init.rb
  4. +9 −120 lib/crummy.rb
  5. +56 −0 lib/crummy/action_controller.rb
  6. +65 −0 lib/crummy/action_view.rb
  7. +18 −0 lib/crummy/railtie.rb
  8. +0 −3 rails/init.rb
View
@@ -13,6 +13,7 @@ begin
gem.email = "zach@lt3media.com"
gem.homepage = "http://github.com/zachinglis/crummy"
gem.authors = ["Zach Inglis"]
+ gem.files = FileList['lib/**/*.rb','tasks/*.rake','init.rb','MIT-LICENSE','Rakefile','README.textile','VERSION', '.gitignore']
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
@@ -1,6 +1,6 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
@@ -9,25 +9,26 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Zach Inglis"]
- s.date = %q{2010-09-10}
+ s.date = %q{2010-11-23}
s.description = %q{Crummy is a simple and tasty way to add breadcrumbs to your Rails applications.}
s.email = %q{zach@lt3media.com}
s.extra_rdoc_files = [
"README.textile"
]
s.files = [
".gitignore",
- "MIT-LICENSE",
- "README.textile",
- "Rakefile",
- "VERSION",
- "init.rb",
- "lib/crummy.rb",
- "rails/init.rb",
- "tasks/crummy_tasks.rake"
+ "MIT-LICENSE",
+ "README.textile",
+ "Rakefile",
+ "VERSION",
+ "init.rb",
+ "lib/crummy.rb",
+ "lib/crummy/action_controller.rb",
+ "lib/crummy/action_view.rb",
+ "lib/crummy/railtie.rb",
+ "tasks/crummy_tasks.rake"
]
s.homepage = %q{http://github.com/zachinglis/crummy}
- s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Tasty breadcrumbs!}
View
@@ -1,2 +1 @@
-ActionController::Base.send :include, Crummy::ControllerMethods
-ActionView::Base.send :include, Crummy::ViewMethods
+require 'crummy'
View
@@ -1,121 +1,10 @@
module Crummy
- module ControllerMethods
- module ClassMethods
- # Add a crumb to the crumbs array.
- #
- # add_crumb("Home", "/")
- # add_crumb("Business") { |instance| instance.business_path }
- #
- # Works like a before_filter so +:only+ and +except+ both work.
- def add_crumb(name, *args)
- options = args.extract_options!
- url = args.first
- raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
- raise ArgumentError, "Cannot pass url and use block" if url && block_given?
- before_filter(options) do |instance|
- url = yield instance if block_given?
- url = instance.send url if url.is_a? Symbol
- record = instance.instance_variable_get("@#{name}") unless url or block_given?
- if record and record.respond_to? :to_param
- name, url = record.to_s, instance.url_for(record)
- end
-
- # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
- # FIXME: Add ||= for the name, url above
- instance.add_crumb(name, url)
- end
- end
- end
-
- module InstanceMethods
- # Add a crumb to the crumbs array.
- #
- # add_crumb("Home", "/")
- # add_crumb("Business") { |instance| instance.business_path }
- #
- def add_crumb(name, url=nil)
- crumbs.push [name, url]
- end
-
- # Lists the crumbs as an array
- def crumbs
- get_or_set_ivar "@_crumbs", []
- end
-
- def get_or_set_ivar(var, value) # :nodoc:
- instance_variable_set var, instance_variable_get(var) || value
- end
- private :get_or_set_ivar
- end
-
- def self.included(receiver) # :nodoc:
- receiver.extend ClassMethods
- receiver.send :include, InstanceMethods
- end
- end
-
- module ViewMethods
- # List the crumbs as an array
- def crumbs
- @_crumbs ||= [] # Give me something to push to
- end
-
- # Add a crumb to the +crumbs+ array
- def add_crumb(name, url=nil)
- crumbs.push [name, url]
- end
-
- # Render the list of crumbs as either html or xml
- #
- # Takes 3 options:
- # The output format. Can either be xml or html. Default :html
- # :format => (:html|:xml)
- # The seperator text. It does not assume you want spaces on either side so you must specify. Default +»+ for :html and +crumb+ for xml
- # :seperator => string
- # Render links in the output. Default +true+
- # :link => boolean
- #
- # Examples:
- # render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
- # render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
- # render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
- #
- # The only argument is for the seperator text. It does not assume you want spaces on either side so you must specify. Defaults to +&raquo;+
- #
- # render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
- #
- def render_crumbs(options = {})
- options[:format] = :html if options[:format] == nil
- if options[:seperator] == nil
- options[:seperator] = " &raquo; " if options[:format] == :html
- options[:seperator] = "crumb" if options[:format] == :xml
- end
- options[:links] = true if options[:links] == nil
- case options[:format]
- when :html
- crumb_string = crumbs.collect do |crumb|
- crumb_to_html crumb, options[:links]
- end * options[:seperator]
- crumb_string = crumb_string.html_safe if crumb_string.respond_to?(:html_safe)
- crumb_string
- when :xml
- crumbs.collect do |crumb|
- crumb_to_xml crumb, options[:links], options[:seperator]
- end * ''
- else
- raise "Unknown breadcrumb output format"
- end
- end
-
- def crumb_to_html(crumb, links)
- name, url = crumb
- url && links ? link_to(name, url) : name
- end
-
- def crumb_to_xml(crumb, links, seperator)
- name, url = crumb
- url && links ? "<#{seperator} href=\"#{url}\">#{name}</#{seperator}>" : "<#{seperator}>#{name}</#{seperator}>"
- end
-
- end
-end
+ if defined?(Rails)
+ require 'crummy/railtie'
+ else
+ require 'crummy/action_controller'
+ require 'crummy/action_view'
+ ActionController::Base.send :include, Crummy::ControllerMethods
+ ActionView::Base.send :include, Crummy::ViewMethods
+ end
+end
@@ -0,0 +1,56 @@
+module Crummy
+ module ControllerMethods
+ module ClassMethods
+ # Add a crumb to the crumbs array.
+ #
+ # add_crumb("Home", "/")
+ # add_crumb("Business") { |instance| instance.business_path }
+ #
+ # Works like a before_filter so +:only+ and +except+ both work.
+ def add_crumb(name, *args)
+ options = args.extract_options!
+ url = args.first
+ raise ArgumentError, "Need more arguments" unless name or options[:record] or block_given?
+ raise ArgumentError, "Cannot pass url and use block" if url && block_given?
+ before_filter(options) do |instance|
+ url = yield instance if block_given?
+ url = instance.send url if url.is_a? Symbol
+ record = instance.instance_variable_get("@#{name}") unless url or block_given?
+ if record and record.respond_to? :to_param
+ name, url = record.to_s, instance.url_for(record)
+ end
+
+ # FIXME: url = instance.url_for(name) if name.respond_to?("to_param") && url.nil?
+ # FIXME: Add ||= for the name, url above
+ instance.add_crumb(name, url)
+ end
+ end
+ end
+
+ module InstanceMethods
+ # Add a crumb to the crumbs array.
+ #
+ # add_crumb("Home", "/")
+ # add_crumb("Business") { |instance| instance.business_path }
+ #
+ def add_crumb(name, url=nil)
+ crumbs.push [name, url]
+ end
+
+ # Lists the crumbs as an array
+ def crumbs
+ get_or_set_ivar "@_crumbs", []
+ end
+
+ def get_or_set_ivar(var, value) # :nodoc:
+ instance_variable_set var, instance_variable_get(var) || value
+ end
+ private :get_or_set_ivar
+ end
+
+ def self.included(receiver) # :nodoc:
+ receiver.extend ClassMethods
+ receiver.send :include, InstanceMethods
+ end
+ end
+end
View
@@ -0,0 +1,65 @@
+module Crummy
+ module ViewMethods
+ # List the crumbs as an array
+ def crumbs
+ @_crumbs ||= [] # Give me something to push to
+ end
+
+ # Add a crumb to the +crumbs+ array
+ def add_crumb(name, url=nil)
+ crumbs.push [name, url]
+ end
+
+ # Render the list of crumbs as either html or xml
+ #
+ # Takes 3 options:
+ # The output format. Can either be xml or html. Default :html
+ # :format => (:html|:xml)
+ # The seperator text. It does not assume you want spaces on either side so you must specify. Default +&raquo;+ for :html and +crumb+ for xml
+ # :seperator => string
+ # Render links in the output. Default +true+
+ # :link => boolean
+ #
+ # Examples:
+ # render_crumbs #=> <a href="/">Home</a> &raquo; <a href="/businesses">Businesses</a>
+ # render_crumbs :seperator => ' | ' #=> <a href="/">Home</a> | <a href="/businesses">Businesses</a>
+ # render_crumbs :format => :xml #=> <crumb href="/">Home</crumb><crumb href="/businesses">Businesses</crumb>
+ #
+ # The only argument is for the seperator text. It does not assume you want spaces on either side so you must specify. Defaults to +&raquo;+
+ #
+ # render_crumbs(" . ") #=> <a href="/">Home</a> . <a href="/businesses">Businesses</a>
+ #
+ def render_crumbs(options = {})
+ options[:format] = :html if options[:format] == nil
+ if options[:seperator] == nil
+ options[:seperator] = " &raquo; " if options[:format] == :html
+ options[:seperator] = "crumb" if options[:format] == :xml
+ end
+ options[:links] = true if options[:links] == nil
+ case options[:format]
+ when :html
+ crumb_string = crumbs.collect do |crumb|
+ crumb_to_html crumb, options[:links]
+ end * options[:seperator]
+ crumb_string = crumb_string.html_safe if crumb_string.respond_to?(:html_safe)
+ crumb_string
+ when :xml
+ crumbs.collect do |crumb|
+ crumb_to_xml crumb, options[:links], options[:seperator]
+ end * ''
+ else
+ raise "Unknown breadcrumb output format"
+ end
+ end
+
+ def crumb_to_html(crumb, links)
+ name, url = crumb
+ url && links ? link_to(name, url) : name
+ end
+
+ def crumb_to_xml(crumb, links, seperator)
+ name, url = crumb
+ url && links ? "<#{seperator} href=\"#{url}\">#{name}</#{seperator}>" : "<#{seperator}>#{name}</#{seperator}>"
+ end
+ end
+end
View
@@ -0,0 +1,18 @@
+require 'crummy'
+require 'rails'
+
+module Crummy
+ class Railtie < Rails::Railtie
+ initializer "crummy.action_controller" do |app|
+ if defined?(ActionController)
+ require 'crummy/action_controller'
+ ActionController::Base.send :include, Crummy::ControllerMethods
+ end
+ end
+
+ initializer "crummy.action_view" do |app|
+ require 'crummy/action_view'
+ ActionView::Base.send :include, Crummy::ViewMethods
+ end
+ end
+end
View
@@ -1,3 +0,0 @@
-require File.join(File.dirname(__FILE__), *%w[.. lib crummy])
-ActionController::Base.send :include, Crummy::ControllerMethods
-ActionView::Base.send :include, Crummy::ViewMethods

0 comments on commit dd4f89e

Please sign in to comment.