mitio-inherited_resources_views 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +27 -0
  4. data/MIT-LICENSE +19 -0
  5. data/README.md +72 -0
  6. data/Rakefile +29 -0
  7. data/VERSION +1 -0
  8. data/inherited_resources_views.gemspec +19 -0
  9. data/init.rb +1 -0
  10. data/lib/generators/inherited_resources_views/inherited_resources_views_generator.rb +84 -0
  11. data/lib/generators/inherited_resources_views/templates/locales/en.yml +24 -0
  12. data/lib/generators/inherited_resources_views/templates/views/basic/_detail.html.erb +8 -0
  13. data/lib/generators/inherited_resources_views/templates/views/basic/_edit_navigation.html.erb +4 -0
  14. data/lib/generators/inherited_resources_views/templates/views/basic/_form.html.erb +32 -0
  15. data/lib/generators/inherited_resources_views/templates/views/basic/_index_navigation.html.erb +3 -0
  16. data/lib/generators/inherited_resources_views/templates/views/basic/_listing.html.erb +24 -0
  17. data/lib/generators/inherited_resources_views/templates/views/basic/_new_navigation.html.erb +1 -0
  18. data/lib/generators/inherited_resources_views/templates/views/basic/_show_navigation.html.erb +4 -0
  19. data/lib/generators/inherited_resources_views/templates/views/basic/edit.html.erb +5 -0
  20. data/lib/generators/inherited_resources_views/templates/views/basic/index.html.erb +5 -0
  21. data/lib/generators/inherited_resources_views/templates/views/basic/new.html.erb +5 -0
  22. data/lib/generators/inherited_resources_views/templates/views/basic/show.html.erb +5 -0
  23. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_detail.html.erb +17 -0
  24. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_edit_navigation.html.erb +7 -0
  25. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_form.html.erb +35 -0
  26. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_index_navigation.html.erb +6 -0
  27. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_listing.html.erb +32 -0
  28. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_new_navigation.html.erb +6 -0
  29. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_show_navigation.html.erb +7 -0
  30. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/_sidebar.html.erb +13 -0
  31. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/edit.html.erb +11 -0
  32. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/index.html.erb +11 -0
  33. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/new.html.erb +11 -0
  34. data/lib/generators/inherited_resources_views/templates/views/web-app-theme/show.html.erb +11 -0
  35. data/lib/inherited_resources_views.rb +44 -0
  36. data/lib/inherited_resources_views/action_view.rb +28 -0
  37. data/lib/inherited_resources_views/helper.rb +163 -0
  38. data/lib/inherited_resources_views/i18n_helper.rb +65 -0
  39. data/test/inherited_resources_views_test.rb +8 -0
  40. data/test/test_helper.rb +3 -0
  41. metadata +116 -0
@@ -0,0 +1,2 @@
1
+ .bundle
2
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ inherited_resources_views (0.4.0)
5
+ inherited_resources (>= 1.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ has_scope (0.5.0)
11
+ hpricot (0.8.3)
12
+ inherited_resources (1.1.2)
13
+ has_scope (~> 0.5.0)
14
+ responders (~> 0.6.0)
15
+ responders (0.6.2)
16
+ ruby_parser (2.0.5)
17
+ sexp_processor (~> 3.0)
18
+ sexp_processor (3.0.5)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ hpricot
25
+ inherited_resources (>= 1.0)
26
+ inherited_resources_views!
27
+ ruby_parser
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Jonhnny Weslley <http://jonhnnyweslley.net>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # Inherited Resources Views
2
+
3
+ ## Introduction
4
+
5
+ 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!
6
+
7
+
8
+ ## Difference to Other Seemly Similar Projects
9
+
10
+ If you are confused about the difference to some other similarly named projects, please read on.
11
+
12
+ ### Difference to Inherit Views
13
+
14
+ [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.
15
+
16
+ ### Difference to Inherited Views
17
+
18
+ [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][]), and it supports both erb and [haml](http://github.com/nex3/haml) templates.
19
+
20
+
21
+ ## Dependencies
22
+
23
+ * [Inherited Resources][]
24
+
25
+
26
+ ## Installation
27
+
28
+ Add it to your `Gemfile`:
29
+
30
+ gem "mitio-inherited_resources_views"
31
+
32
+ Install using `bundle`:
33
+
34
+ bundle install
35
+
36
+
37
+ ## Usage
38
+
39
+ 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:
40
+
41
+ rails generate inherited_resources_views
42
+
43
+ This will generate a set of views in your `app/views/inherited_resources` folder. Edit away!
44
+
45
+ **Note:** Please remember to restart your server!
46
+
47
+
48
+ ## Bugs and Feedback
49
+
50
+ If you discover any bugs or have some idea, feel free to create an issue on GitHub:
51
+
52
+ * here: <https://github.com/mitio/inherited_resources_views/issues>
53
+ * or here: <https://github.com/jweslley/inherited_resources_views/issues>
54
+
55
+
56
+ ## Authors
57
+
58
+ * Fred Wu <http://fredwu.me>
59
+ * Envato - <http://envato.com>
60
+ * Wuit - <http://wuit.com>
61
+ * Jonhnny Weslley - <http://jonhnnyweslley.net>
62
+
63
+
64
+ ## License
65
+
66
+ Copyright (c) 2011 Jonhnny Weslley (<http://jonhnnyweslley.net>), released under the MIT license.
67
+
68
+ See the [MIT-LICENSE][] file provided with the source distribution for full details.
69
+
70
+
71
+ [Inherited Resources]: http://github.com/josevalim/inherited_resources
72
+ [MIT-LICENSE]: https://github.com/jweslley/inherited_resources_views/blob/master/MIT-LICENSE
@@ -0,0 +1,29 @@
1
+ begin
2
+ require 'bundler'
3
+ Bundler::GemHelper.install_tasks
4
+ rescue Exception => e
5
+ end
6
+
7
+ require 'rake'
8
+ require 'rake/testtask'
9
+ require 'rake/rdoctask'
10
+
11
+ desc 'Default: run unit tests.'
12
+ task :default => :test
13
+
14
+ desc 'Test the inherited_resources_views plugin.'
15
+ Rake::TestTask.new(:test) do |t|
16
+ t.libs << 'lib'
17
+ t.libs << 'test'
18
+ t.pattern = 'test/**/*_test.rb'
19
+ t.verbose = true
20
+ end
21
+
22
+ desc 'Generate documentation for the inherited_resources_views plugin.'
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'InheritedResourcesViews'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.1
@@ -0,0 +1,19 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.name = 'mitio-inherited_resources_views'
4
+ s.version = `git tag | tail -1`.strip.gsub(/^v/,'')
5
+ s.platform = Gem::Platform::RUBY
6
+ s.authors = ["Fred Wu", "Jonhnny Weslley"]
7
+ s.email = "jw@jonhnnyweslley.net"
8
+ s.homepage = "http://github.com/jweslley/inherited_resources_views"
9
+
10
+ s.summary = "A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!"
11
+ 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!"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- test/*`.split("\n")
15
+ s.extra_rdoc_files = ["README.md"]
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency("inherited_resources", "~> 1.2.1")
19
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "inherited_resources_views"
@@ -0,0 +1,84 @@
1
+ class InheritedResourcesViewsGenerator < Rails::Generators::Base
2
+ source_root File.expand_path("../templates", __FILE__)
3
+
4
+ desc "Generates inherited_resources_views templates."
5
+
6
+ class_option :layout, :type => :string, :aliases => "-l", :default => :basic,
7
+ :desc => "Generate views using the specified LAYOUT. Available options are 'basic' and 'web-app-theme'."
8
+
9
+ class_option :template_engine, :type => :string, :aliases => "-t", :default => :erb,
10
+ :desc => "Template engine for the views. Available options are 'erb' and 'haml'."
11
+
12
+ def copy_views
13
+ case options[:template_engine].to_s
14
+ when 'haml'
15
+ verify_haml_existence
16
+ verify_haml_version
17
+ create_and_copy_haml_views
18
+ else
19
+ directory "views/#{layout}", "app/views/inherited_resources"
20
+ end
21
+ end
22
+
23
+ def copy_locale
24
+ copy_file "locales/en.yml", "config/locales/inherited_resources.en.yml"
25
+ end
26
+
27
+ protected
28
+
29
+ def layout
30
+ return options[:layout] if ['basic', 'web-app-theme'].include? options[:layout].to_s
31
+ say "Invalid layout: #{options[:layout]}.\nAvailable options are 'basic' and 'web-app-theme'."
32
+ exit 42
33
+ end
34
+
35
+ def verify_haml_existence
36
+ begin
37
+ require 'haml'
38
+ rescue LoadError
39
+ say "HAML is not installed, or it is not specified in your Gemfile."
40
+ exit
41
+ end
42
+ end
43
+
44
+ def verify_haml_version
45
+ unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
46
+ say "To generate HAML templates, you need to install HAML 2.3 or above."
47
+ exit
48
+ end
49
+ end
50
+
51
+ def create_and_copy_haml_views
52
+ begin
53
+ require 'hpricot'
54
+ rescue LoadError
55
+ say "Hpricot is not installed, or it is not specified in your Gemfile."
56
+ exit
57
+ end
58
+
59
+ begin
60
+ require 'ruby_parser'
61
+ rescue LoadError
62
+ say "Ruby_parser is not installed, or it is not specified in your Gemfile."
63
+ exit
64
+ end
65
+
66
+ require 'tmpdir'
67
+ html_root = "#{self.class.source_root}/views/#{layout}"
68
+
69
+ Dir.mktmpdir("inherited_resources_views-haml.") do |haml_root|
70
+ Dir["#{html_root}/**/*"].each do |path|
71
+ relative_path = path.sub(html_root, "")
72
+ source_path = (haml_root + relative_path).sub(/erb$/, "haml")
73
+
74
+ if File.directory?(path)
75
+ FileUtils.mkdir_p(source_path)
76
+ else
77
+ `html2haml -r #{path} #{source_path}`
78
+ end
79
+ end
80
+
81
+ directory haml_root, "app/views/inherited_resources"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,24 @@
1
+ en:
2
+ boolean:
3
+ :true: 'Yes'
4
+ :false: 'No'
5
+ helpers:
6
+ titles:
7
+ index: "Listing %{model}"
8
+ show: "%{model}"
9
+ new: "New %{model}"
10
+ edit: "Editing %{model}"
11
+ actions:
12
+ index: "List"
13
+ show: "Show"
14
+ new: "New"
15
+ edit: "Edit"
16
+ back: "Back"
17
+ cancel: "Cancel"
18
+ destroy: "Destroy"
19
+ options: "Options"
20
+ messages:
21
+ destroy_confirm: "Are you sure?"
22
+ errors:
23
+ one: "1 error prohibited this %{model} from being saved:"
24
+ other: "%{count} errors prohibited this %{model} from being saved:"
@@ -0,0 +1,8 @@
1
+ <table class="<%= resource_instance_name %> detail">
2
+ <% show_fields.each do |field| %>
3
+ <tr>
4
+ <th><%= resource.class.human_attribute_name(field) %></th>
5
+ <td><%= display_field(resource, field) %></td>
6
+ </tr>
7
+ <% end %>
8
+ </table>
@@ -0,0 +1,4 @@
1
+ <% if action_defined?(:show) %>
2
+ <p><%= link_to human_action(:show), resource_url(resource) %></p>
3
+ <% end %>
4
+ <p><%= link_to human_action(:back), collection_url %></p>
@@ -0,0 +1,32 @@
1
+ <table>
2
+ <% form_for(resource,
3
+ :as => resource_instance_name,
4
+ :url => resource.new_record? ? collection_url : resource_url(resource),
5
+ :html => { :method => (resource.new_record? ? "post" : "put") }) do |form| %>
6
+ <% if resource.errors.any? %>
7
+ <tr>
8
+ <td colspan="2">
9
+ <div id="error_explanation">
10
+ <h2><%= t('helpers.messages.errors', :count => resource.errors.count, :model => resource.class.model_name.human.downcase) %></h2>
11
+ <ul>
12
+ <% resource.errors.full_messages.each do |msg| %>
13
+ <li><%= msg %></li>
14
+ <% end %>
15
+ </ul>
16
+ </div>
17
+ </td>
18
+ </tr>
19
+ <% end %>
20
+ <% form_fields.each do |field| %>
21
+ <% if form_input = form_field(form, resource, field) %>
22
+ <tr>
23
+ <td><%= form.label(field) %></td>
24
+ <td><%= form_input %></td>
25
+ </tr>
26
+ <% end %>
27
+ <% end %>
28
+ <tr>
29
+ <td colspan="2"><%= form.submit %></td>
30
+ </tr>
31
+ <% end %>
32
+ </table>
@@ -0,0 +1,3 @@
1
+ <% if action_defined?(:new) %>
2
+ <p><%= link_to human_action(:new), new_resource_url %></p>
3
+ <% end %>
@@ -0,0 +1,24 @@
1
+ <table class="<%= resource_collection_name %> collection">
2
+ <thead>
3
+ <tr>
4
+ <% index_fields.each do |field| %>
5
+ <th><%= resource_class.human_attribute_name(field) %></th>
6
+ <% end %>
7
+ <th colspan="3"><%= human_action(:options) %></th>
8
+ </tr>
9
+ </thead>
10
+ <tbody>
11
+ <% collection.each do |resource| %>
12
+ <tr>
13
+ <% index_fields.each do |field| %>
14
+ <td><%= display_field(resource, field) %></td>
15
+ <% end %>
16
+ <td><%= link_to_if(action_defined?(:show), human_action(:show), resource_url(resource)){} %></td>
17
+ <td><%= link_to_if(action_defined?(:edit), human_action(:edit), edit_resource_url(resource)){} %></td>
18
+ <td><%= link_to_if(action_defined?(:destroy), human_action(:destroy), resource_url(resource),
19
+ :confirm => t('helpers.messages.destroy_confirm', :default => 'Are you sure?'),
20
+ :method => :delete){} %></td>
21
+ </tr>
22
+ <% end %>
23
+ </tbody>
24
+ </table>
@@ -0,0 +1 @@
1
+ <p><%= link_to human_action(:back), collection_url %></p>
@@ -0,0 +1,4 @@
1
+ <% if action_defined?(:edit) %>
2
+ <p><%= link_to human_action(:edit), edit_resource_url(resource) %></p>
3
+ <% end %>
4
+ <p><%= link_to human_action(:back), collection_url %></p>
@@ -0,0 +1,5 @@
1
+ <h1><%= action_title(:edit) %></h1>
2
+
3
+ <%= render "form" %>
4
+
5
+ <%= render "edit_navigation" %>
@@ -0,0 +1,5 @@
1
+ <h1><%= action_title(:index) %></h1>
2
+
3
+ <%= render "index_navigation" %>
4
+
5
+ <%= render "listing" if collection.any? %>
@@ -0,0 +1,5 @@
1
+ <h1><%= action_title(:new) %></h1>
2
+
3
+ <%= render "form" %>
4
+
5
+ <%= render "new_navigation" %>
@@ -0,0 +1,5 @@
1
+ <h1><%= action_title(:show) %></h1>
2
+
3
+ <%= render "detail" %>
4
+
5
+ <%= render "show_navigation" %>
@@ -0,0 +1,17 @@
1
+ <% show_fields.each do |field| %>
2
+ <p>
3
+ <b><%= resource.class.human_attribute_name(field) %>:</b>
4
+ <%= display_field(resource, field) %>
5
+ </p>
6
+ <% end %>
7
+
8
+ <div class="wat-cf">
9
+ <%= link_to_if(action_defined?(:edit),
10
+ image_tag("web-app-theme/icons/application_edit.png", :alt => human_action(:edit)) +
11
+ " " + human_action(:edit), edit_resource_url, :class => "button"){} %>
12
+
13
+ <%= link_to_if(action_defined?(:destroy),
14
+ image_tag("web-app-theme/icons/cross.png", :alt => human_action(:destroy)) +
15
+ " " + human_action(:destroy), resource_url, :method => "delete", :class => "button",
16
+ :confirm => t('helpers.messages.destroy_confirm', :default => 'Are you sure?')){} %>
17
+ </div>
@@ -0,0 +1,7 @@
1
+ <div class="secondary-navigation">
2
+ <ul class="wat-cf">
3
+ <li class="first"><%= link_to_if(action_defined?(:index), human_action(:index), collection_url){} %></li>
4
+ <li><%= link_to_if(action_defined?(:new), human_action(:new), new_resource_url){} %></li>
5
+ <li class="active"><%= link_to_if(action_defined?(:edit), human_action(:edit), edit_resource_url){} %></li>
6
+ </ul>
7
+ </div>
@@ -0,0 +1,35 @@
1
+ <%= form_for(resource,
2
+ :as => resource_instance_name,
3
+ :url => resource.new_record? ? collection_url : resource_url(resource),
4
+ :html => { :method => (resource.new_record? ? "post" : "put"), :class => :form }) do |form| %>
5
+ <% if resource.errors.any? %>
6
+ <div id="error_explanation">
7
+ <h2><%= t('helpers.messages.errors', :count => resource.errors.count, :model => resource.class.model_name.human.downcase) %></h2>
8
+ <ul>
9
+ <% resource.errors.full_messages.each do |msg| %>
10
+ <li><%= msg %></li>
11
+ <% end %>
12
+ </ul>
13
+ </div>
14
+ <% end %>
15
+ <% form_fields.each do |field| %>
16
+ <% if form_input = form_field(form, resource, field) %>
17
+ <div class="group">
18
+ <%= form.label(field, :class => :label) %>
19
+ <%= form_input %>
20
+ <span class="description">
21
+ <%= t("activerecord.descriptions.#{resource_instance_name}.#{field}", :default => '') %>
22
+ </span>
23
+ </div>
24
+ <% end %>
25
+ <% end %>
26
+
27
+ <div class="group navform wat-cf">
28
+ <button class="button" type="submit">
29
+ <%= image_tag("web-app-theme/icons/tick.png") %>
30
+ <%= t("helpers.submit.#{resource.new_record? ? :create : :update}", :model => human_resource, :default => "Save") %>
31
+ </button>
32
+ <span class="text_button_padding"><%= t("web-app-theme.or", :default => "or") %></span>
33
+ <%= link_to human_action(:cancel), collection_url, :class => "text_button_padding link_button" %>
34
+ </div>
35
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <div class="secondary-navigation">
2
+ <ul class="wat-cf">
3
+ <li class="first active"><%= link_to_if(action_defined?(:index), human_action(:index), collection_url){} %></li>
4
+ <li><%= link_to_if(action_defined?(:new), human_action(:new), new_resource_url){} %></li>
5
+ </ul>
6
+ </div>
@@ -0,0 +1,32 @@
1
+ <table class="table">
2
+ <tr>
3
+ <% index_fields.each do |field| %>
4
+ <th><%= resource_class.human_attribute_name(field) %></th>
5
+ <% end %>
6
+ <th class="last">&nbsp;</th>
7
+ </tr>
8
+ <% collection.each do |resource| %>
9
+ <%= content_tag_for(:tr, resource, :class => cycle("odd", "even")) do %>
10
+ <% index_fields.each do |field| %>
11
+ <td><%= display_field(resource, field) %></td>
12
+ <% end %>
13
+ <td class="last">
14
+ <% if action_defined?(:show) %>
15
+ <%= link_to human_action(:show), resource_url(resource) %>
16
+ <% end %>
17
+ <% if action_defined?(:edit) %>
18
+ | <%= link_to human_action(:edit), edit_resource_url(resource) %>
19
+ <% end %>
20
+ <% if action_defined?(:destroy) %>
21
+ | <%= link_to human_action(:destroy), resource_url(resource), :method => :delete,
22
+ :confirm => t('helpers.messages.destroy_confirm', :default => 'Are you sure?') %>
23
+ <% end %>
24
+ </td>
25
+ <% end %>
26
+ <% end %>
27
+ </table>
28
+
29
+ <div class="actions-bar wat-cf">
30
+ <div class="actions">
31
+ </div>
32
+ </div>
@@ -0,0 +1,6 @@
1
+ <div class="secondary-navigation">
2
+ <ul class="wat-cf">
3
+ <li class="first"><%= link_to_if(action_defined?(:index), human_action(:index), collection_url){} %></li>
4
+ <li class="active"><%= link_to_if(action_defined?(:new), human_action(:new), new_resource_url){} %></li>
5
+ </ul>
6
+ </div>
@@ -0,0 +1,7 @@
1
+ <div class="secondary-navigation">
2
+ <ul class="wat-cf">
3
+ <li class="first"><%= link_to_if(action_defined?(:index), human_action(:index), collection_url){} %></li>
4
+ <li><%= link_to_if(action_defined?(:new), human_action(:new), new_resource_url){} %></li>
5
+ <li class="active"><%= link_to_if(action_defined?(:show), human_action(:show), resource_url){} %></li>
6
+ </ul>
7
+ </div>
@@ -0,0 +1,13 @@
1
+ <div class="block notice">
2
+ <h3>Simple Block</h3>
3
+ <div class="content">
4
+ <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
5
+ </div>
6
+ </div>
7
+ <div class="block">
8
+ <h3>Links</h3>
9
+ <ul class="navigation">
10
+ <li><a href="#">Link 1</a></li>
11
+ <li><a href="#">Link 2</a></li>
12
+ </ul>
13
+ </div>
@@ -0,0 +1,11 @@
1
+ <div class="block">
2
+ <%= render "edit_navigation" %>
3
+ <div class="content">
4
+ <h2 class="title"><%= action_title(:edit) %></h2>
5
+ <div class="inner">
6
+ <%= render "form" %>
7
+ </div>
8
+ </div>
9
+ </div>
10
+
11
+ <% content_for :sidebar, render(:partial => 'sidebar') %>
@@ -0,0 +1,11 @@
1
+ <div class="block">
2
+ <%= render "index_navigation" %>
3
+ <div class="content">
4
+ <h2 class="title"><%= action_title(:index) %></h2>
5
+ <div class="inner">
6
+ <%= render "listing" if collection.any? %>
7
+ </div>
8
+ </div>
9
+ </div>
10
+
11
+ <% content_for :sidebar, render(:partial => 'sidebar') %>
@@ -0,0 +1,11 @@
1
+ <div class="block">
2
+ <%= render "new_navigation" %>
3
+ <div class="content">
4
+ <h2 class="title"><%= action_title(:new) %></h2>
5
+ <div class="inner">
6
+ <%= render "form" %>
7
+ </div>
8
+ </div>
9
+ </div>
10
+
11
+ <% content_for :sidebar, render(:partial => 'sidebar') %>
@@ -0,0 +1,11 @@
1
+ <div class="block">
2
+ <%= render "show_navigation" %>
3
+ <div class="content">
4
+ <h2 class="title"><%= action_title(:show) %></h2>
5
+ <div class="inner">
6
+ <%= render "detail" %>
7
+ </div>
8
+ </div>
9
+ </div>
10
+
11
+ <% content_for :sidebar, render(:partial => 'sidebar') %>
@@ -0,0 +1,44 @@
1
+ require 'inherited_resources_views/action_view'
2
+ require 'inherited_resources_views/i18n_helper'
3
+ require 'inherited_resources_views/helper'
4
+
5
+ # Clears up all existing helpers in this class, only keeping
6
+ # the helper with the same name as this class, ApplicationHelper
7
+ # and InheritedResourcesViews::Helper.
8
+ #
9
+ # By default, in Rails 3, if you derive your class from
10
+ # ActionController::Base, all helpers will be included.
11
+ # Since InheritedResourcesViews::Helper module relies on different
12
+ # helpers using the same method names, this is a very bad thing.
13
+ #
14
+ class InheritedResources::Base
15
+ instance_eval do
16
+ alias :inherit_resources_original :inherit_resources
17
+ end
18
+
19
+ def self.inherit_resources(base)
20
+ inherit_resources_original(base)
21
+ include_helpers(base)
22
+ end
23
+
24
+ def self.inherited(base)
25
+ super
26
+ include_helpers(base)
27
+ end
28
+
29
+ private
30
+ def self.include_helpers(base)
31
+ base.class_eval do
32
+ clear_helpers
33
+ helper ::ApplicationHelper,
34
+ InheritedResourcesViews::I18nHelper,
35
+ InheritedResourcesViews::Helper
36
+
37
+ helper_method :action_defined?
38
+ private
39
+ def action_defined?(action)
40
+ respond_to? action and respond_to? "#{action}!"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ module InheritedResourcesViews
2
+ module ActionView
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def self.process_view_paths(value)
7
+ PathSet.new(Array.wrap(value))
8
+ end
9
+ end
10
+
11
+ class PathSet < ::ActionView::PathSet
12
+ def find(path, prefix = nil, partial = false, details = {}, key = nil)
13
+ super
14
+ rescue ::ActionView::MissingTemplate
15
+ super(path, "inherited_resources", partial, details, key)
16
+ end
17
+
18
+ def find_template(original_template_path, format = nil, html_fallback = true)
19
+ super
20
+ rescue ::ActionView::MissingTemplate
21
+ original_template_path.sub!(/^[\w]+/, "inherited_resources")
22
+ super
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ ActionView::Base.send :include, InheritedResourcesViews::ActionView
@@ -0,0 +1,163 @@
1
+ module InheritedResourcesViews
2
+ module Helper
3
+ extend ActiveSupport::Concern
4
+
5
+ module InstanceMethods
6
+
7
+ def fields
8
+ resource_class.column_names - hidden_fields
9
+ end
10
+
11
+ def hidden_fields
12
+ %w(id created_at updated_at)
13
+ end
14
+
15
+ def index_fields
16
+ fields
17
+ end
18
+
19
+ def show_fields
20
+ fields
21
+ end
22
+
23
+ def form_fields
24
+ fields
25
+ end
26
+
27
+ def display_field(resource, field)
28
+ if respond_to?(helper = "#{field}_display")
29
+ send(helper, resource)
30
+ else
31
+ default_display_field(resource, field)
32
+ end
33
+ end
34
+
35
+ def form_field(form, resource, field)
36
+ if respond_to?(helper = "#{field}_form_field")
37
+ send(helper, form, resource)
38
+ else
39
+ default_form_field(form, resource, field)
40
+ end
41
+ end
42
+
43
+ protected
44
+
45
+ def default_display_field(resource, field)
46
+ case column_type(resource, field)
47
+ when :string
48
+ if field.include?('password')
49
+ '******'
50
+ elsif field.include?('email')
51
+ mail_to(resource.send(field))
52
+ else
53
+ resource.send(field)
54
+ end
55
+ when :boolean
56
+ value = resource.send(field)
57
+ I18n.t("boolean.#{value}", :default => value)
58
+ when :text, :integer
59
+ resource.send(field)
60
+ when :float, :decimal
61
+ number_with_precision(resource.send(field))
62
+ when :date, :time, :datetime, :timestamp
63
+ value = resource.send(field)
64
+ I18n.l(value) if value
65
+ else
66
+ resource.send(field)
67
+ end
68
+ end
69
+
70
+ def default_form_field(form, resource, field)
71
+ case column_type(resource, field)
72
+ when :string
73
+ field.include?('password') ? form.password_field(field) : form.text_field(field)
74
+ when :text
75
+ form.text_area(field)
76
+ when :integer, :float, :decimal
77
+ form.text_field(field)
78
+ when :date
79
+ form.date_select(field)
80
+ when :datetime, :timestamp
81
+ form.datetime_select(field)
82
+ when :time
83
+ form.time_select(field)
84
+ when :boolean
85
+ form.check_box(field)
86
+ else
87
+ form.text_field(field)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def column_type(resource, field)
94
+ resource.column_for_attribute(field).type
95
+ end
96
+ end
97
+
98
+ module ClassMethods
99
+
100
+ # Defines which fields to use on views.
101
+ #
102
+ # fields :title, :body, :category, :comments
103
+ # fields :all, :except => :comments
104
+ #
105
+ # or on specific views (index, show, form):
106
+ #
107
+ # index_fields :title, :category
108
+ # show_fields :all
109
+ # form_fields :all, :except => :comments
110
+ #
111
+ # or which fields must be hidden:
112
+ #
113
+ # hidden_fields :id, :created_at, :updated_at
114
+ #
115
+ # Hidden fields also can be used on +:except+ option,
116
+ # which is very useful when using the +:all+ option:
117
+ #
118
+ # fields :title, :body, :category, :comments
119
+ # show_fields :all # show all fields including the hidden ones (id, created_at and updated_at).
120
+ # index_fields :all, :except => [:hidden, :body, :comments] # just show both title and category fields.
121
+ #
122
+ instance_eval do
123
+ InstanceMethods.instance_methods.select {|method| method.ends_with?('fields')}.each do |method|
124
+ define_method(method) do |*fields|
125
+ define_method(method) do
126
+ raise ArgumentError, 'Wrong number of arguments. You have to provide which fields you want to use.' if fields.empty?
127
+ options = fields.extract_options!
128
+ fields = resource_class.column_names.map(&:to_sym) if fields.first == :all
129
+ fields_to_hide = Array(options[:except])
130
+ fields_to_hide += hidden_fields.map(&:to_sym) if fields_to_hide.delete(:hidden)
131
+ fields_to_hide.map!(&:to_sym).uniq!
132
+ fields -= fields_to_hide
133
+ fields.map(&:to_s)
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ # Creates helper methods from FormHelpers.
140
+ #
141
+ # module PostsHelper
142
+ # text_field :title, :class => 'strong'
143
+ # text_area :body
144
+ # collection_select :category_id, Category.all, :id, :name
145
+ # end
146
+ #
147
+ # From the above code, will be created the following methods:
148
+ # title_form_field, body_form_field and category_id_form_field.
149
+ #
150
+ def method_missing(method_name, *args)
151
+ field = args.first
152
+
153
+ define_method("#{field}_form_field") do |form, resource|
154
+ if form.respond_to? method_name
155
+ form.send(method_name, *args)
156
+ else
157
+ raise NoMethodError, "This form builder doesn't respond to #{method_name}"
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
@@ -0,0 +1,65 @@
1
+ module InheritedResourcesViews
2
+ module I18nHelper
3
+
4
+ # Transform the resource class name into a more human format, using I18n.
5
+ # By default, it will underscore then humanize the class name.
6
+ #
7
+ # human_resource # => "Project manager"
8
+ #
9
+ def human_resource(pluralize=false)
10
+ human = I18n.t(resource_instance_name,
11
+ :scope => [:activerecord, :models],
12
+ :default => resource_instance_name.to_s.humanize)
13
+ pluralize ? human.pluralize : human
14
+ end
15
+
16
+ # Transform the action name into a more human format, using I18n.
17
+ #
18
+ # human_action(:show) # => "Show"
19
+ #
20
+ # This helper is useful when creating links:
21
+ #
22
+ # <%= link_to human_action(:show), resource_url(blog) %>
23
+ #
24
+ # renders:
25
+ #
26
+ # <a href="/blogs/1">Show</a>
27
+ #
28
+ def human_action(action, pluralize=false)
29
+ translate_action(:actions, action, pluralize) do |model|
30
+ "#{action.to_s.humanize}"
31
+ end
32
+ end
33
+
34
+ # Transform the action name into a more human format for page headings, using I18n.
35
+ #
36
+ # action_title(:index) # => "Listing projects"
37
+ #
38
+ # This helper is useful when creating page headings:
39
+ #
40
+ # <h1><%= action_title(:index) %></h1>
41
+ #
42
+ # renders:
43
+ #
44
+ # <h1>Listing projects</h1>
45
+ #
46
+ def action_title(action, pluralize=false)
47
+ translate_action(:titles, action, pluralize) do |model|
48
+ "#{action.to_s.humanize} #{model}"
49
+ end
50
+ end
51
+
52
+ private
53
+ def translate_action(prefix, action, pluralize=false)
54
+ model = human_resource((action.to_s == 'index') || pluralize)
55
+ instance = params[:id] ? resource : nil
56
+
57
+ defaults = []
58
+ defaults << :"helpers.#{prefix}.#{resource_instance_name}.#{action}"
59
+ defaults << :"helpers.#{prefix}.#{action}"
60
+ defaults << yield(model) if block_given?
61
+
62
+ I18n.t(defaults.shift, :model => model, :resource => instance, :default => defaults)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class InheritedResourcesViewsTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mitio-inherited_resources_views
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 1
9
+ version: 0.4.1
10
+ platform: ruby
11
+ authors:
12
+ - Fred Wu
13
+ - Jonhnny Weslley
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-23 00:00:00 +03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: inherited_resources
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 1
32
+ version: 1.2.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ 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!
36
+ email: jw@jonhnnyweslley.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - MIT-LICENSE
48
+ - README.md
49
+ - Rakefile
50
+ - VERSION
51
+ - inherited_resources_views.gemspec
52
+ - init.rb
53
+ - lib/generators/inherited_resources_views/inherited_resources_views_generator.rb
54
+ - lib/generators/inherited_resources_views/templates/locales/en.yml
55
+ - lib/generators/inherited_resources_views/templates/views/basic/_detail.html.erb
56
+ - lib/generators/inherited_resources_views/templates/views/basic/_edit_navigation.html.erb
57
+ - lib/generators/inherited_resources_views/templates/views/basic/_form.html.erb
58
+ - lib/generators/inherited_resources_views/templates/views/basic/_index_navigation.html.erb
59
+ - lib/generators/inherited_resources_views/templates/views/basic/_listing.html.erb
60
+ - lib/generators/inherited_resources_views/templates/views/basic/_new_navigation.html.erb
61
+ - lib/generators/inherited_resources_views/templates/views/basic/_show_navigation.html.erb
62
+ - lib/generators/inherited_resources_views/templates/views/basic/edit.html.erb
63
+ - lib/generators/inherited_resources_views/templates/views/basic/index.html.erb
64
+ - lib/generators/inherited_resources_views/templates/views/basic/new.html.erb
65
+ - lib/generators/inherited_resources_views/templates/views/basic/show.html.erb
66
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_detail.html.erb
67
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_edit_navigation.html.erb
68
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_form.html.erb
69
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_index_navigation.html.erb
70
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_listing.html.erb
71
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_new_navigation.html.erb
72
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_show_navigation.html.erb
73
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/_sidebar.html.erb
74
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/edit.html.erb
75
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/index.html.erb
76
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/new.html.erb
77
+ - lib/generators/inherited_resources_views/templates/views/web-app-theme/show.html.erb
78
+ - lib/inherited_resources_views.rb
79
+ - lib/inherited_resources_views/action_view.rb
80
+ - lib/inherited_resources_views/helper.rb
81
+ - lib/inherited_resources_views/i18n_helper.rb
82
+ - test/inherited_resources_views_test.rb
83
+ - test/test_helper.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/jweslley/inherited_resources_views
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.3.6
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A lot of times resources share the same views, so why not DRY 'em up using Inherited Resources Views!
114
+ test_files:
115
+ - test/inherited_resources_views_test.rb
116
+ - test/test_helper.rb