Removed SimplyHelpful plugin (functionality has been rolled into Rails) and modified ResourceFeeder as necessary.

This commit is contained in:
Luke Melia 2008-06-18 16:13:37 -04:00
parent 0f7d6f7a1d
commit 85dc6f4898
21 changed files with 6 additions and 755 deletions

View file

@ -3,6 +3,7 @@ require 'resource_feeder/common'
module ResourceFeeder
module Atom
include ResourceFeeder::Common
include ActionController::Routing
extend self
def render_atom_feed_for(resources, options = {})
@ -25,14 +26,14 @@ module ResourceFeeder
options[:feed][:title] ||= klass.name.pluralize
options[:feed][:id] ||= "tag:#{request.host_with_port}:#{klass.name.pluralize}"
options[:feed][:link] ||= SimplyHelpful::PolymorphicRoutes.polymorphic_url(new_record, options[:url_writer])
options[:feed][:link] ||= polymorphic_url(new_record, :controller => options[:url_writer])
options[:item][:title] ||= [ :title, :subject, :headline, :name ]
options[:item][:description] ||= [ :description, :body, :content ]
options[:item][:pub_date] ||= [ :updated_at, :updated_on, :created_at, :created_on ]
options[:item][:author] ||= [ :author, :creator ]
resource_link = lambda { |r| SimplyHelpful::PolymorphicRoutes.polymorphic_url(r, options[:url_writer]) }
resource_link = lambda { |r| polymorphic_url(r, :controller => options[:url_writer]) }
xml.instruct!
xml.feed "xml:lang" => "en-US", "xmlns" => 'http://www.w3.org/2005/Atom' do

View file

@ -3,6 +3,7 @@ require 'resource_feeder/common'
module ResourceFeeder
module Rss
include ResourceFeeder::Common
include ActionController::Routing
extend self
def render_rss_feed_for(resources, options = {})
@ -25,7 +26,7 @@ module ResourceFeeder
use_content_encoded = options[:item].has_key?(:content_encoded)
options[:feed][:title] ||= klass.name.pluralize
options[:feed][:link] ||= SimplyHelpful::PolymorphicRoutes.polymorphic_url(new_record, options[:url_writer])
options[:feed][:link] ||= polymorphic_url(new_record, :controller => options[:url_writer])
options[:feed][:language] ||= "en-us"
options[:feed][:ttl] ||= "40"
@ -33,7 +34,7 @@ module ResourceFeeder
options[:item][:description] ||= [ :description, :body, :content ]
options[:item][:pub_date] ||= [ :updated_at, :updated_on, :created_at, :created_on ]
resource_link = lambda { |r| SimplyHelpful::PolymorphicRoutes.polymorphic_url(r, options[:url_writer]) }
resource_link = lambda { |r| polymorphic_url(r, :controller => options[:url_writer]) }
rss_root_attributes = { :version => 2.0 }
rss_root_attributes.merge!("xmlns:content" => "http://purl.org/rss/1.0/modules/content/") if use_content_encoded

View file

@ -1,9 +0,0 @@
* Added formatted_polymorphic_url, formatted_polymorphic_path
* Added support for route options. Example:
polymorphic_url(:id => @subject, :anchor => "photo")
* Added polymorphic routes for new and edit actions (new_polymorphic_url, edit_polymorphic_url)
* Moved polymorphic routes from SimplyHelpful::RecordIdentifier to SimplyHelpful::PolymorphicRoutes

View file

@ -1,4 +0,0 @@
SimplyHelpful
=============
Description goes here

View file

@ -1,22 +0,0 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
desc 'Test the simply_helpful plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
desc 'Generate documentation for the simply_helpful plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SimplyHelpful'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

View file

@ -1,4 +0,0 @@
require 'simply_helpful'
ActionController::Base.send :include, SimplyHelpful::RecordIdentificationHelper
ActionController::Base.helper SimplyHelpful::RecordIdentificationHelper,
SimplyHelpful::RecordTagHelper

View file

@ -1,10 +0,0 @@
require 'simply_helpful/record_identification_helper'
require 'simply_helpful/record_identifier'
require 'simply_helpful/record_tag_helper'
require 'simply_helpful/polymorphic_routes'
require 'simply_helpful/jsg_extensions'
require 'simply_helpful/av_extensions'
require 'simply_helpful/form_helper_extensions'
require 'simply_helpful/controller_extensions'

View file

@ -1,26 +0,0 @@
module ActionView
module Partials
def render_partial_with_record_identification(partial_path, local_assigns = nil, deprecated_local_assigns = nil)
if partial_path.is_a?(String) || partial_path.is_a?(Symbol) || partial_path.nil?
render_partial_without_record_identification(
partial_path, local_assigns, deprecated_local_assigns
)
elsif partial_path.is_a?(Array)
if partial_path.any?
path = SimplyHelpful::RecordIdentifier.partial_path(partial_path.first)
collection = partial_path
render_partial_collection(
path, collection, nil, local_assigns.value
)
else
""
end
else
render_partial_without_record_identification(
SimplyHelpful::RecordIdentifier.partial_path(partial_path), local_assigns, deprecated_local_assigns
)
end
end
alias_method_chain :render_partial, :record_identification
end
end

View file

@ -1,42 +0,0 @@
module SimplyHelpful
module ActionControllerExtensions
POLYMORPHIC_ROUTES = %w(
polymorphic_url polymorphic_path
edit_polymorphic_url edit_polymorphic_path
new_polymorphic_url new_polymorphic_path
formatted_polymorphic_url formatted_polymorphic_path
)
def self.included(base)
POLYMORPHIC_ROUTES.each { |route| base.helper_method(route) }
end
POLYMORPHIC_ROUTES.each do |route|
module_eval <<-EOT
def #{route}(record)
SimplyHelpful::PolymorphicRoutes.#{route}(record, self)
end
EOT
end
def redirect_to_with_record_identification(*args)
return redirect_to_without_record_identification *args unless args.size == 1
potential_object = args.first
case potential_object
when String, Symbol, Hash
redirect_to_without_record_identification *args
else
redirect_to_without_record_identification SimplyHelpful::PolymorphicRoutes.polymorphic_url(potential_object, self)
end
end
end
end
module ActionController
class Base
include SimplyHelpful::ActionControllerExtensions
alias_method_chain :redirect_to, :record_identification
end
end

View file

@ -1,48 +0,0 @@
module ActionView
module Helpers
module FormHelper
def form_for_with_record_identification(name_or_object, *args, &proc)
form_method_with_record_identification :form_for, name_or_object, *args, &proc
end
alias_method_chain :form_for, :record_identification
protected
def form_method_with_record_identification(method_name, name_or_object, *args, &proc)
old_method_name = "#{method_name}_without_record_identification"
case name_or_object
when String, Symbol, NilClass
send(old_method_name, name_or_object, *args, &proc)
else
options = args.first || {}
object_name = SimplyHelpful::RecordIdentifier.singular_class_name(name_or_object)
object = name_or_object
url = SimplyHelpful::PolymorphicRoutes.polymorphic_url(object, self)
html_options = if object.new_record?
{ :class => dom_class(object, :new), :id => dom_id(object), :method => :post }
else
{ :class => dom_class(object, :edit), :id => dom_id(object, :edit), :method => :put }
end
send(old_method_name,
object_name, object, options.merge({ :url => url, :html => html_options.update(options[:html] || {}) }), &proc
)
end
end
end
end
end
module ActionView
module Helpers
module PrototypeHelper
def remote_form_for_with_record_identification(name_or_object, *args, &proc)
form_method_with_record_identification :remote_form_for, name_or_object, *args, &proc
end
alias_method_chain :remote_form_for, :record_identification
end
end
end

View file

@ -1,18 +0,0 @@
module ActionView
module Helpers
module PrototypeHelper
class JavaScriptGenerator
module GeneratorMethods
def [](id)
case id
when String, Symbol, NilClass
JavaScriptElementProxy.new(self, id)
else
JavaScriptElementProxy.new(self, SimplyHelpful::RecordIdentifier.dom_id(id))
end
end
end
end
end
end
end

View file

@ -1,52 +0,0 @@
module SimplyHelpful
module PolymorphicRoutes
extend self
def polymorphic_url(record_or_hash, url_writer, options = {})
record = extract_record(record_or_hash)
case
when options[:action] == "new"
url_writer.send(action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options))
when record.new_record?
url_writer.send(RecordIdentifier.plural_class_name(record) + routing_type(options))
else
url_writer.send(
action_prefix(options) + RecordIdentifier.singular_class_name(record) + routing_type(options), record_or_hash
)
end
end
def polymorphic_path(record_or_hash, url_writer)
polymorphic_url(record_or_hash, url_writer, :routing_type => :path)
end
%w( edit new formatted ).each do |action|
module_eval <<-EOT
def #{action}_polymorphic_url(record_or_hash, url_writer)
polymorphic_url(record_or_hash, url_writer, :action => "#{action}")
end
def #{action}_polymorphic_path(record_or_hash, url_writer)
polymorphic_url(record_or_hash, url_writer, :action => "#{action}", :routing_type => :path)
end
EOT
end
private
def action_prefix(options)
options[:action] ? "#{options[:action]}_" : ""
end
def routing_type(options)
"_#{options[:routing_type] || "url"}"
end
def extract_record(record_or_hash)
record_or_hash.is_a?(Hash) ? record_or_hash[:id] : record_or_hash
end
end
end

View file

@ -1,16 +0,0 @@
module SimplyHelpful
module RecordIdentificationHelper
protected
def partial_path(*args, &block)
RecordIdentifier.partial_path(*args, &block)
end
def dom_class(*args, &block)
RecordIdentifier.dom_class(*args, &block)
end
def dom_id(*args, &block)
RecordIdentifier.dom_id(*args, &block)
end
end
end

View file

@ -1,32 +0,0 @@
module SimplyHelpful
module RecordIdentifier
extend self
def partial_path(record_or_class)
klass = class_from_record_or_class(record_or_class)
"#{klass.name.tableize}/#{klass.name.demodulize.underscore}"
end
def dom_class(record_or_class, prefix = nil)
[ prefix, singular_class_name(record_or_class) ].compact * '_'
end
def dom_id(record, prefix = nil)
prefix ||= 'new' unless record.id
[ prefix, singular_class_name(record), record.id ].compact * '_'
end
def plural_class_name(record_or_class)
singular_class_name(record_or_class).pluralize
end
def singular_class_name(record_or_class)
class_from_record_or_class(record_or_class).name.underscore.tr('/', '_')
end
private
def class_from_record_or_class(record_or_class)
record_or_class.is_a?(Class) ? record_or_class : record_or_class.class
end
end
end

View file

@ -1,76 +0,0 @@
module SimplyHelpful
module RecordTagHelper
# Produces a wrapper DIV element with id and class parameters that
# relate to the specified ActiveRecord object. Usage example:
#
# <% div_for(@person, :class => "foo") do %>
# <%=h @person.name %>
# <% end %>
#
# produces:
#
# <div id="person_123" class="person foo"> Joe Bloggs </div>
#
def div_for(record, *args, &block)
content_tag_for(:div, record, *args, &block)
end
# content_tag_for creates an HTML element with id and class parameters
# that relate to the specified ActiveRecord object. For example:
#
# <% content_tag_for(:tr, @person) do %>
# <td><%=h @person.first_name %></td>
# <td><%=h @person.last_name %></td>
# <% end %>
#
# would produce hthe following HTML (assuming @person is an instance of
# a Person object, with an id value of 123):
#
# <tr id="person_123" class="person">....</tr>
#
# If you require the HTML id attribute to have a prefix, you can specify it:
#
# <% content_tag_for(:tr, @person, :foo) do %> ...
#
# produces:
#
# <tr id="foo_person_123" class="person">...
#
# content_tag_for also accepts a hash of options, which will be converted to
# additional HTML attributes. If you specify a +:class+ value, it will be combined
# with the default class name for your object. For example:
#
# <% content_tag_for(:li, @person, :class => "bar") %>...
#
# produces:
#
# <li id="person_123" class="person bar">...
#
def content_tag_for(tag_name, record, *args, &block)
prefix = args.first.is_a?(Hash) ? nil : args.shift
options = args.first.is_a?(Hash) ? args.shift : {}
concat content_tag(tag_name, capture(&block),
options.merge({ :class => "#{dom_class(record)} #{options[:class]}".strip, :id => dom_id(record, prefix) })),
block.binding
end
end
end
module ActionView
module Helpers
module UrlHelper
def link_to_with_record_identification(attr_name, record = {}, html_options = nil, *parameters_for_method_reference)
case record
when Hash, String, Symbol, NilClass
link_to_without_record_identification(attr_name, record, html_options, *parameters_for_method_reference)
else
url = SimplyHelpful::PolymorphicRoutes.polymorphic_url(record, self)
link_text = record.respond_to?(attr_name) ? record.send(attr_name) : attr_name
link_to_without_record_identification(link_text, url, html_options, *parameters_for_method_reference)
end
end
alias_method_chain :link_to, :record_identification
end
end
end

View file

@ -1,52 +0,0 @@
require File.dirname(__FILE__) + '/test_helper'
require 'ostruct'
class RedirectionTestingController < ActionController::Base
class MockResponse
attr_accessor :redirected_to
def redirect(_)
end
end
def initialize
super
@response = MockResponse.new
@request = OpenStruct.new
@request.protocol= "http://"
@request.host_with_port= "www.example.com"
end
def response
@response
end
def request
@request
end
def post_url(p)
"/posts/#{p.id}"
end
end
class ControllerExtensionsTest < Test::Unit::TestCase
def setup
@record = Post.new
@record.save
@controller = RedirectionTestingController.new
end
def test_redirect_to_record
@controller.send :redirect_to, @record
assert_equal "http://www.example.com/posts/1", @controller.response.redirected_to
end
def test_redirect_to_string
@controller.send :redirect_to, "http://www.yahoo.com"
assert_equal "http://www.yahoo.com", @controller.response.redirected_to
end
end

View file

@ -1,96 +0,0 @@
require File.dirname(__FILE__) + '/test_helper'
class LabelledFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(hidden_field)).each do |selector|
src = <<-END_SRC
def #{selector}(field, *args, &proc)
"<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
end
END_SRC
class_eval src, __FILE__, __LINE__
end
end
class FormHelperExtensionsTest < Test::Unit::TestCase
include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::PrototypeHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include SimplyHelpful::RecordIdentificationHelper
def setup
@record = Post.new
@controller = Class.new do
attr_reader :url_for_options
def url_for(options, *parameters_for_method_reference)
@url_for_options = options
@url_for_options || "http://www.example.com"
end
end
@controller = @controller.new
end
def test_form_for_with_record_identification_with_new_record
_erbout = ''
form_for(@record, {:html => { :id => 'create-post' }}) {}
expected = "<form action='#{posts_url}' class='new_post' id='create-post' method='post'></form>"
assert_dom_equal expected, _erbout
end
def test_form_for_with_record_identification_with_custom_builder
_erbout = ''
form_for(@record, :builder => LabelledFormBuilder) do |f|
_erbout.concat(f.text_field(:name))
end
expected = "<form action='#{posts_url}' class='new_post' id='new_post' method='post'>" +
"<label for='name'>Name:</label>" +
" <input type='text' size='30' name='post[name]' id='post_name' value='new post' /><br />" +
"</form>"
assert_dom_equal expected, _erbout
end
def test_form_for_with_record_identification_without_html_options
_erbout = ''
form_for(@record) {}
expected = "<form action='#{posts_url}' class='new_post' method='post' id='new_post'></form>"
assert_dom_equal expected, _erbout
end
def test_form_for_with_record_identification_with_existing_record
@record.save
_erbout = ''
form_for(@record) {}
expected = "<form action='#{post_url(@record)}' class='edit_post' id='edit_post_1' method='post'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>"
assert_dom_equal expected, _erbout
end
def test_remote_form_for_with_record_identification_with_new_record
_erbout = ''
remote_form_for(@record, {:html => { :id => 'create-post' }}) {}
expected = %(<form action='#{posts_url}' onsubmit="new Ajax.Request('#{posts_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_post' id='create-post' method='post'></form>)
assert_dom_equal expected, _erbout
end
def test_remote_form_for_with_record_identification_without_html_options
_erbout = ''
remote_form_for(@record) {}
expected = %(<form action='#{posts_url}' onsubmit="new Ajax.Request('#{posts_url}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_post' method='post' id='new_post'></form>)
assert_dom_equal expected, _erbout
end
def test_remote_form_for_with_record_identification_with_existing_record
@record.save
_erbout = ''
remote_form_for(@record) {}
expected = %(<form action='#{post_url(@record)}' id='edit_post_1' method='post' onsubmit="new Ajax.Request('#{post_url(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_post'><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div></form>)
assert_dom_equal expected, _erbout
end
end

View file

@ -1,80 +0,0 @@
require File.dirname(__FILE__) + '/test_helper'
class RecordIdentifierTest < Test::Unit::TestCase
include SimplyHelpful
def setup
@klass = Post
@record = @klass.new
@singular = 'post'
@plural = 'posts'
end
def test_dom_id_with_new_record
assert_equal "new_#{@singular}", dom_id(@record)
end
def test_dom_id_with_new_record_and_prefix
assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix)
end
def test_dom_id_with_saved_record
@record.save
assert_equal "#{@singular}_1", dom_id(@record)
end
def test_dom_id_with_prefix
@record.save
assert_equal "edit_#{@singular}_1", dom_id(@record, :edit)
end
def test_partial_path
expected = "#{@plural}/#{@singular}"
assert_equal expected, partial_path(@record)
assert_equal expected, partial_path(Post)
end
def test_dom_class
assert_equal @singular, dom_class(@record)
end
def test_dom_class_with_prefix
assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix)
end
def test_singular_class_name
assert_equal @singular, singular_class_name(@record)
end
def test_singular_class_name_for_class
assert_equal @singular, singular_class_name(@klass)
end
def test_plural_class_name
assert_equal @plural, plural_class_name(@record)
end
def test_plural_class_name_for_class
assert_equal @plural, plural_class_name(@klass)
end
private
def method_missing(method, *args)
RecordIdentifier.send(method, *args)
end
end
class NestedRecordIdentifierTest < RecordIdentifierTest
def setup
@klass = Post::Nested
@record = @klass.new
@singular = 'post_nested'
@plural = 'post_nesteds'
end
def test_partial_path
expected = "post/nesteds/nested"
assert_equal expected, partial_path(@record)
assert_equal expected, partial_path(Post::Nested)
end
end

View file

@ -1,133 +0,0 @@
require File.dirname(__FILE__) + '/test_helper'
class RecordTagHelperTest < Test::Unit::TestCase
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::TextHelper
include SimplyHelpful::RecordTagHelper
include SimplyHelpful::RecordIdentificationHelper
def setup
@record = Post.new
end
def test_content_tag_for_with_new_record
_erbout = ''
content_tag_for(:li, @record) {}
expected = "<li class='post' id='new_post'></li>"
assert_dom_equal expected, _erbout
end
def test_content_tag_for_with_existing_record
@record.save
_erbout = ''
content_tag_for(:li, @record) {}
expected = "<li class='post' id='post_1'></li>"
assert_dom_equal expected, _erbout
end
def test_content_tag_for_merges_given_class_names
_erbout = ''
content_tag_for(:li, @record, :class => 'foo') {}
expected = "<li class='post foo' id='new_post'></li>"
assert_dom_equal expected, _erbout
_erbout = ''
content_tag_for(:li, @record, :class => 'foo bar') {}
expected = "<li class='post foo bar' id='new_post'></li>"
assert_dom_equal expected, _erbout
end
def test_content_tag_for_with_dom_id_prefix_on_new_record
_erbout = ''
content_tag_for(:li, @record, :foo, :class => 'foo') {}
expected = "<li class='post foo' id='foo_post'></li>"
assert_dom_equal expected, _erbout
end
def test_content_tag_for_with_dom_id_prefix_on_existing_record
@record.save
_erbout = ''
content_tag_for(:li, @record, :foo, :class => 'foo') {}
expected = "<li class='post foo' id='foo_post_1'></li>"
assert_dom_equal expected, _erbout
end
def test_div_for_with_new_record
_erbout = ''
div_for(@record) {}
expected = "<div class='post' id='new_post'></div>"
assert_dom_equal expected, _erbout
end
def test_div_for_with_existing_record
@record.save
_erbout = ''
div_for(@record) {}
expected = "<div class='post' id='post_1'></div>"
assert_dom_equal expected, _erbout
end
def test_div_for_merges_given_class_names
_erbout = ''
div_for(@record, :class => 'foo') {}
expected = "<div class='post foo' id='new_post'></div>"
assert_dom_equal expected, _erbout
_erbout = ''
div_for(@record, :class => 'foo bar') {}
expected = "<div class='post foo bar' id='new_post'></div>"
assert_dom_equal expected, _erbout
end
def test_div_for_with_dom_id_prefix_on_new_record
_erbout = ''
div_for(@record, :foo, :class => 'foo') {}
expected = "<div class='post foo' id='foo_post'></div>"
assert_dom_equal expected, _erbout
end
def test_div_for_with_dom_id_prefix_on_existing_record
@record.save
_erbout = ''
div_for(@record, :foo, :class => 'foo') {}
expected = "<div class='post foo' id='foo_post_1'></div>"
assert_dom_equal expected, _erbout
end
def test_link_to_with_new_record
actual = link_to :name, @record
expected = "<a href='http://www.example.com/posts'>new post</a>"
assert_dom_equal expected, actual
end
def test_link_to_with_existing_record
@record.save
actual = link_to :name, @record
expected = "<a href='http://www.example.com/posts/1'>post #1</a>"
assert_dom_equal expected, actual
end
def test_link_to_with_an_existing_method_and_constant_text
@record.save
actual = link_to "Cancel Editing", @record
expected = "<a href='http://www.example.com/posts/1'>Cancel Editing</a>"
assert_dom_equal expected, actual
end
end

View file

@ -1,6 +0,0 @@
require File.dirname(__FILE__) + '/test_helper'
class SimplyHelpfulTest < Test::Unit::TestCase
def default_test
end
end

View file

@ -1,25 +0,0 @@
RAILS_ENV = 'test'
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
require 'action_controller/test_process'
require 'breakpoint'
class Post
attr_reader :id
def save; @id = 1 end
def new_record?; @id.nil? end
def name
@id.nil? ? 'new post' : "post ##{@id}"
end
class Nested < Post; end
end
class Test::Unit::TestCase
protected
def posts_url
'http://www.example.com/posts'
end
def post_url(post)
"http://www.example.com/posts/#{post.id}"
end
end