Browse files

Initial commit.

  • Loading branch information...
0 parents commit f696b68b144d4b086767398de9c418863370aeff @fredwu committed Aug 5, 2010
1 .gitignore
@@ -0,0 +1 @@
+pkg
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 [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.
54 README.md
@@ -0,0 +1,54 @@
+# Inherited Resources Views
+
+## Introduction
+
+Using [Inherited Resources](http://github.com/josevalim/inherited_resources) is an excellent way to reduce the amount of repetition in your controllers. But what about views? A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!
+
+Brought to you by [Envato](http://envato.com) and [Wuit](http://wuit.com).
+
+## Difference to Other Seemly Similar Projects
+
+If you are confused about the difference to some other similarly named projects, please read on.
+
+### Difference to Inherit Views
+
+[Inherit Views](http://github.com/ianwhite/inherit_views) adds the ability to render views from parent controllers. It does not share views between different resources.
+
+### Difference to Inherited Views
+
+[Inherited Views](http://github.com/gregbell/inherited_views) tries to solve the same problem we're solving, but from a slightly different angle. It is more complex, requires [Formtastic](http://github.com/justinfrench/formtastic) and [WillPaginate](http://github.com/mislav/will_paginate), and it only generates erb templates. **Inherited Resources Views** on the other hand, is extremely simple, is library-agnostic (it only depends on [Inherited Resources](http://github.com/josevalim/inherited_resources)), and it supports both erb and [haml](http://github.com/nex3/haml) templates.
+
+## Todo
+
+* Add tests
+* Make default views more elegant and perhaps customisable
+
+## Dependencies
+
+* **Rails 3.0+**
+* [Inherited Resources](http://github.com/josevalim/inherited_resources)
+
+## Installation
+
+As a Rails plugin:
+
+ rails plugin install git://github.com/fredwu/inherited_resources_views.git
+
+As a gem:
+
+ gem install inherited_resources_views
+
+## Usage
+
+It is *extremely* simple to use Inherited Resources Views. The only step you need to do after the installation is to customise the default views:
+
+ rails generate inherited_resources_views
+
+This will generate a set of views in your `app/views/inherited_resources_views` folder. Edit away!
+
+## Author
+
+Copyright (c) 2010 Fred Wu (<http://fredwu.me>), released under the MIT license
+
+* Envato - <http://envato.com>
+* Wuit - <http://wuit.com>
40 Rakefile
@@ -0,0 +1,40 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the inherited_resources_views plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the inherited_resources_views plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'InheritedResourcesViews'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |s|
+ s.name = "inherited_resources_views"
+ s.version = "0.1.0"
+ s.summary = "A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!"
+ s.description = "Using Inherited Resources is an excellent way to reduce the amount of repetition in your controllers. But what about views? A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!"
+ s.email = "ifredwu@gmail.com"
+ s.homepage = "http://github.com/fredwu/inherited_resources_views"
+ s.authors = ["Fred Wu"]
+ s.add_dependency("inherited_resources", ">= 1.1")
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler not available. Install it with: gem install jeweler"
+end
25 app/views/inherited_resources/_form.html.erb
@@ -0,0 +1,25 @@
+<table>
+<%= form_for(resource) do |f| %>
+ <% if resource.errors.any? %>
+ <tr>
+ <td colspan="2">
+ <h2><%= pluralize(resource.errors.count, "error") %> prohibited this testa from being saved:</h2>
+ <ul>
+ <% resource.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </td>
+ </tr>
+ <% end %>
+ <% resource.attributes.each do |a| %>
+ <tr>
+ <td><%= a[0] %></td>
+ <td><%= f.text_field a[0].to_sym %></td>
+ </tr>
+ <% end %>
+ <tr>
+ <td colspan="2"><%= f.submit %></td>
+ </tr>
+<% end %>
+</table>
4 app/views/inherited_resources/edit.html.erb
@@ -0,0 +1,4 @@
+<%= render "form" %>
+
+<p><%= link_to "Show", resource %></p>
+<p><%= link_to "Back", eval("#{controller_name}_path") %></p>
25 app/views/inherited_resources/index.html.erb
@@ -0,0 +1,25 @@
+<p><%= link_to "New", eval("new_#{controller_name.singularize}_path") %></p>
+
+<% if collection.any? %>
+<table>
+ <thead>
+ <tr>
+ <% collection.first.attributes.each do |a| %>
+ <th><%= a[0] %></th>
+ <% end %>
+ </tr>
+ </thead>
+ <tbody>
+ <% collection.each do |r| %>
+ <tr>
+ <% r.attributes.each do |a| %>
+ <td><%= a[1] %></td>
+ <% end %>
+ <td><%= link_to "Show", r %></td>
+ <td><%= link_to "Edit", eval("edit_#{controller_name.singularize}_path(r)") %></td>
+ <td><%= link_to "Destroy", r, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
+<% end %>
3 app/views/inherited_resources/new.html.erb
@@ -0,0 +1,3 @@
+<%= render "form" %>
+
+<p><%= link_to "Back", eval("#{controller_name}_path") %></p>
11 app/views/inherited_resources/show.html.erb
@@ -0,0 +1,11 @@
+<table>
+<% resource.attributes.each do |a| %>
+ <tr>
+ <th><%= a[0] %></th>
+ <td><%= a[1] %></td>
+ </tr>
+<% end %>
+</table>
+
+<p><%= link_to "Edit", eval("edit_#{controller_name.singularize}_path(resource)") %></p>
+<p><%= link_to "Back", eval("#{controller_name}_path") %></p>
58 inherited_resources_views.gemspec
@@ -0,0 +1,58 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{inherited_resources_views}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Fred Wu"]
+ s.date = %q{2010-08-05}
+ s.description = %q{Using Inherited Resources is an excellent way to reduce the amount of repetition in your controllers. But what about views? A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!}
+ s.email = %q{ifredwu@gmail.com}
+ s.extra_rdoc_files = [
+ "README.md"
+ ]
+ s.files = [
+ ".gitignore",
+ "MIT-LICENSE",
+ "README.md",
+ "Rakefile",
+ "app/views/inherited_resources/_form.html.erb",
+ "app/views/inherited_resources/edit.html.erb",
+ "app/views/inherited_resources/index.html.erb",
+ "app/views/inherited_resources/new.html.erb",
+ "app/views/inherited_resources/show.html.erb",
+ "inherited_resources_views.gemspec",
+ "lib/generators/inherited_resources_views_generator.rb",
+ "lib/inherited_resources_views.rb",
+ "lib/inherited_resources_views/action_view.rb",
+ "test/inherited_resources_views_test.rb",
+ "test/test_helper.rb"
+ ]
+ s.homepage = %q{http://github.com/fredwu/inherited_resources_views}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!}
+ s.test_files = [
+ "test/inherited_resources_views_test.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<inherited_resources>, [">= 1.1"])
+ else
+ s.add_dependency(%q<inherited_resources>, [">= 1.1"])
+ end
+ else
+ s.add_dependency(%q<inherited_resources>, [">= 1.1"])
+ end
+end
+
59 lib/generators/inherited_resources_views_generator.rb
@@ -0,0 +1,59 @@
+class InheritedResourcesViewsGenerator < Rails::Generators::Base
+ desc "Generates inherited_resource_views templates."
+
+ class_option :template_engine, :type => :string, :aliases => "-t", :default => "erb",
+ :desc => "Template engine for the views. Available options are 'erb' and 'haml'."
+
+ def self.source_root
+ @_views_source_root ||= File.expand_path("../../../app/views", __FILE__)
+ end
+
+ def copy_views
+ case options[:template_engine]
+ when "haml"
+ verify_haml_existence
+ verify_haml_version
+ create_and_copy_haml_views
+ else
+ directory "inherited_resources", "app/views/inherited_resources"
+ end
+ end
+
+ protected
+
+ def verify_haml_existence
+ begin
+ require 'haml'
+ rescue LoadError
+ say "HAML is not installed, or it is not specified in your Gemfile."
+ exit
+ end
+ end
+
+ def verify_haml_version
+ unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
+ say "To generate HAML templates, you need to install HAML 2.3 or above."
+ exit
+ end
+ end
+
+ def create_and_copy_haml_views
+ require 'tmpdir'
+ html_root = "#{self.class.source_root}/inherited_resources"
+
+ Dir.mktmpdir("inherited_resources_views-haml.") do |haml_root|
+ Dir["#{html_root}/**/*"].each do |path|
+ relative_path = path.sub(html_root, "")
+ source_path = (haml_root + relative_path).sub(/erb$/, "haml")
+
+ if File.directory?(path)
+ FileUtils.mkdir_p(source_path)
+ else
+ `html2haml -r #{path} #{source_path}`
+ end
+ end
+
+ directory haml_root, "app/views/inherited_resources"
+ end
+ end
+end
10 lib/inherited_resources_views.rb
@@ -0,0 +1,10 @@
+require 'inherited_resources_views/action_view'
+
+class ActionController::Base
+ def self.inherited(base)
+ super
+ if base.respond_to?(:inherit_resources)
+ ActionView::Base.send :include, InheritedResourcesViews::ActionView
+ end
+ end
+end
19 lib/inherited_resources_views/action_view.rb
@@ -0,0 +1,19 @@
+module InheritedResourcesViews
+ module ActionView
+ def self.included(base)
+ base.class_eval do
+ def self.process_view_paths(value)
+ PathSet.new(Array.wrap(value))
+ end
+ end
+ end
+
+ class PathSet < ::ActionView::PathSet
+ def find(path, prefix = nil, partial = false, details = {}, key = nil)
+ super
+ rescue ::ActionView::MissingTemplate
+ super(path, "inherited_resources", partial, details, key)
+ end
+ end
+ end
+end
8 test/inherited_resources_views_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class InheritedResourcesViewsTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
3 test/test_helper.rb
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'test/unit'
+require 'active_support'

0 comments on commit f696b68

Please sign in to comment.