Permalink
Browse files

initial commit

  • Loading branch information...
1 parent d31a923 commit a81ec0519ddd227b89b8f734fda7c281faeb5011 Sven Fuchs committed Sep 11, 2010
View
38 README
@@ -0,0 +1,38 @@
+Based on InheritedResources
+
+# controller#resource
+
+Always returns the current resource instance which depends on the controller
+and the current action:
+
+ posts#index => Post.new (?)
+ posts#show => post
+ posts#new => Post.new (?)
+ posts#create => Post.new (?)
+ posts#edit => post
+ posts#update => post
+ posts#destroy => post
+
+# controller#resources
+
+Returns an array containing the current resource and all of its inherited
+resources as defined by the controller through InheritedResources.
+
+E.g. on admin/sites/1/blogs/1/posts/1 the resources array equals [:admin, site, blog, post]
+
+# resource url helpers
+
+Defined for each get-able action, basically delegates to polymorphic_path with
+the current resources array:
+
+ index_url, alias: collection_url
+ new_url
+ edit_url
+ show_url, aliased resource_url
+
+# resource link helpers
+
+ link_to_index, alias: link_to_collection
+ link_to_new
+ link_to_edit
+ link_to_show
View
@@ -0,0 +1,10 @@
+require 'rake'
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
@@ -0,0 +1,19 @@
+module InheritedResources
+ module Helpers
+ autoload :Resources, 'inherited_resources/helpers/resources'
+ autoload :UrlFor, 'inherited_resources/helpers/url_for'
+ autoload :LinkTo, 'inherited_resources/helpers/link_to'
+ end
+
+ Base.class_eval do
+ class << self
+ def inherited_with_helpers(base)
+ base.send :include, Helpers::Resources
+ base.send :include, Helpers::UrlFor
+ inherited_without_helpers(base)
+ end
+ alias_method_chain :inherited, :helpers # TODO ugh.
+ end
+ end
+end
+
@@ -0,0 +1,11 @@
+module InheritedResources
+ module Helpers
+ module LinkTo
+ [:index, :new, :show, :edit, :destroy].each do |action|
+ define_method(:"link_to_#{action}") do |*args|
+ link_to(t(:".#{action}"), controller.send(:"#{action}_path"), args.extract_options!)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,30 @@
+module InheritedResources
+ module Helpers
+ module Resources
+ def resource
+ member_action? ? super : build_resource
+ end
+
+ def resources
+ @resources ||= with_chain(resource).tap { |r| r.unshift(route_prefix) if route_prefix }
+ @resources.dup
+ end
+
+ def parent_resources
+ resources[0..-2]
+ end
+
+ protected
+
+ MEMBER_ACTIONS = [:show, :edit, :update, :destroy]
+
+ def member_action?
+ MEMBER_ACTIONS.include?(params[:action].to_sym)
+ end
+
+ def route_prefix
+ @route_prefix ||= self.class.resources_configuration[:self][:route_prefix].try(:to_sym)
+ end
+ end
+ end
+end
@@ -0,0 +1,23 @@
+module InheritedResources
+ module Helpers
+ module UrlFor
+ def index_path
+ polymorphic_path(parent_resources << resource_class)
+ end
+
+ def new_path
+ polymorphic_path(parent_resources.unshift(:new) << resource_class.name.underscore)
+ end
+
+ def show_path
+ raise "can't generate show_path because the current resource (#{resource.inspect}) is a new record" if resource.new_record?
+ polymorphic_path(resources)
+ end
+
+ def edit_path
+ raise "can't generate edit_path because the current resource (#{resource.inspect}) is a new record" if resource.new_record?
+ polymorphic_path(resources.unshift(:edit))
+ end
+ end
+ end
+end
@@ -0,0 +1 @@
+require 'inherited_resources/helpers'
View
@@ -0,0 +1 @@
+Dir[File.expand_path('../**/*_test.rb', __FILE__)].each { |file| require file }
View
@@ -0,0 +1,23 @@
+require File.expand_path('../test_helper', __FILE__)
+
+class LinkToTest < Test::Unit::TestCase
+ include ActionView::Helpers::UrlHelper
+ include InheritedResources::Helpers::LinkTo
+
+ attr_reader :controller, :blog, :post
+
+ def setup
+ @blog = Blog.create
+ @post = blog.posts.create!
+ @controller = setup_controller(Admin::PostsController)
+ controller.params = { :action => 'show', :blog_id => blog.id, :id => post.id }
+ end
+
+ def t(*)
+ 'foo'
+ end
+
+ test "link_to_index" do
+ assert_equal %(<a href="/admin/blogs/#{blog.id}/posts" class="bar">foo</a>), link_to_index(:class => :bar)
+ end
+end
View
@@ -0,0 +1,47 @@
+require File.expand_path('../test_helper', __FILE__)
+
+class ResourceTest < Test::Unit::TestCase
+ attr_reader :controller, :blog, :post
+
+ def setup
+ @blog = Blog.create
+ @post = blog.posts.create!
+ @controller = setup_controller(Admin::PostsController)
+ controller.params = { :blog_id => blog.id }
+ end
+
+ test "resource on posts#index returs a new Post" do
+ controller.params.merge! :action => 'index'
+ assert_new_record(Post, controller.resource)
+ end
+
+ test "resource on posts#new returs a new Post" do
+ controller.params.merge! :action => 'new'
+ assert_new_record(Post, controller.resource)
+ end
+
+ test "resource on posts#create returs a post" do
+ controller.params.merge! :action => 'create'
+ assert_new_record(Post, controller.resource)
+ end
+
+ test "resource on posts#show returs a post" do
+ controller.params.merge! :action => 'show', :id => post.id
+ assert_equal post, controller.resource
+ end
+
+ test "resource on posts#edit returs a post" do
+ controller.params.merge! :action => 'edit', :id => post.id
+ assert_equal post, controller.resource
+ end
+
+ test "resource on posts#update returs a post" do
+ controller.params.merge! :action => 'update', :id => post.id
+ assert_equal post, controller.resource
+ end
+
+ test "resource on posts#destroy returs a post" do
+ controller.params.merge! :action => 'destroy', :id => post.id
+ assert_equal post, controller.resource
+ end
+end
View
@@ -0,0 +1,52 @@
+require File.expand_path('../test_helper', __FILE__)
+
+class ResourcesTest < Test::Unit::TestCase
+ attr_reader :controller, :blog, :post
+
+ def setup
+ @blog = Blog.create
+ @post = blog.posts.create!
+ @controller = setup_controller(Admin::PostsController)
+ controller.params = { :blog_id => blog.id }
+ end
+
+ test "resources contains the route_prefix" do
+ controller.params.merge! :action => 'index'
+ assert_equal :admin, controller.resources.first
+ end
+
+ test "resources on posts#index returs an array containing a new Post" do
+ controller.params.merge! :action => 'index'
+ assert_new_record(Post, controller.resources.last)
+ end
+
+ test "resources on posts#new returs an array containing a new Post" do
+ controller.params.merge! :action => 'new'
+ assert_new_record(Post, controller.resources.last)
+ end
+
+ test "resources on posts#create returs an array containing a post" do
+ controller.params.merge! :action => 'create'
+ assert_new_record(Post, controller.resources.last)
+ end
+
+ test "resources on posts#show returs an array containing a post" do
+ controller.params.merge! :action => 'show', :id => post.id
+ assert_equal post, controller.resources.last
+ end
+
+ test "resources on posts#edit returs an array containing a post" do
+ controller.params.merge! :action => 'edit', :id => post.id
+ assert_equal post, controller.resources.last
+ end
+
+ test "resources on posts#update returs an array containing a post" do
+ controller.params.merge! :action => 'update', :id => post.id
+ assert_equal post, controller.resources.last
+ end
+
+ test "resources on posts#destroy returs an array containing a post" do
+ controller.params.merge! :action => 'destroy', :id => post.id
+ assert_equal post, controller.resources.last
+ end
+end
View
@@ -0,0 +1,47 @@
+$:.unshift File.expand_path('../../lib', __FILE__)
+
+require 'rubygems'
+require 'test/unit'
+require 'test_declarative'
+require 'ruby-debug'
+require 'database_cleaner'
+
+require 'active_support'
+require "active_support/core_ext/object/with_options"
+require "active_record"
+require "action_controller"
+require "rails/railtie"
+
+class ApplicationController < ActionController::Base; end
+
+require 'inherited_resources'
+require 'inherited_resources_helpers'
+require File.expand_path('../test_setup', __FILE__)
+
+DatabaseCleaner.strategy = :truncation
+
+Test::Unit::TestCase.class_eval do
+ def setup
+ DatabaseCleaner.start
+ end
+
+ def teardown
+ DatabaseCleaner.clean
+ end
+
+ def setup_controller(klass)
+ klass.new.tap do |controller|
+ controller.request = ActionDispatch::TestRequest.new
+ controller.response = ActionDispatch::TestResponse.new
+ controller.params = { :action => 'show' }
+ yield(controller) if block_given?
+ end
+ end
+
+ def assert_new_record(klass, record)
+ assert record.is_a?(klass), "#{record.inspect} should be a #{klass}"
+ assert record.new_record?, "#{record.inspect} should be a new record (#{klass})"
+ end
+end
+
+
View
@@ -0,0 +1,34 @@
+ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
+ActiveRecord::Migration.verbose = false
+
+ActiveRecord::Schema.define :version => 0 do
+ create_table :blogs, :force => true do |t|
+ t.string :name
+ end
+
+ create_table :posts, :force => true do |t|
+ t.references :blog
+ t.string :title
+ end
+end
+
+class Blog < ActiveRecord::Base
+ has_many :posts
+end
+
+class Post < ActiveRecord::Base
+end
+
+module Admin
+ class PostsController < InheritedResources::Base
+ belongs_to :blog
+
+ routes = ActionDispatch::Routing::RouteSet.new
+ routes.draw { namespace(:admin) { resources(:blogs) { resources(:posts) } } }
+ include routes.url_helpers
+
+ public :resource
+ end
+end
+
+
Oops, something went wrong.

0 comments on commit a81ec05

Please sign in to comment.