inherited_views 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Greg Bell (www.gregbell.ca)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = Inherited Views
2
+
3
+ NOTE: THIS IS UNDER HEAVY DEV AND IS NOT READY TO BE USED YET
4
+
5
+ Inherited Views is a thin addition to the Inherited Resources project adding default html views which can be later customized on a per controller basis or globally for your rails app.
6
+
7
+ == Dependencies
8
+
9
+ * InheritedResources
10
+ * Formtastic
11
+ * WillPaginate
12
+
13
+ == The Views
14
+
15
+ Inherited Views gives you a default set of templates which you can override in your implementation, or modify to create application specific defaults.
16
+
17
+ The following templates exist:
18
+
19
+ index.html.erb Renders a paginate listing of the collection
20
+ _table.html.erb Does the actual work of generating the table
21
+ show.html.erb Shows the resource
22
+ new.html.erb Renders the _form template for a new resource
23
+ edit.html.erb Renders the _form template for a resource to edit
24
+ _form.html.erb HTML for the actual form
25
+
26
+
27
+ == Note on Patches/Pull Requests
28
+
29
+ * Fork the project.
30
+ * Make your feature addition or bug fix.
31
+ * Add tests for it. This is important so I don't break it in a
32
+ future version unintentionally.
33
+ * Commit, do not mess with rakefile, version, or history.
34
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
35
+ * Send me a pull request. Bonus points for topic branches.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2010 Greg Bell (www.gregbell.ca)
40
+
41
+ See LICENSE for details.
@@ -0,0 +1,110 @@
1
+ require 'inherited_resources'
2
+
3
+ module InheritedViews
4
+ class Base < InheritedResources::Base
5
+
6
+
7
+ # Set the name of the folder of the default views to use
8
+ # within the view path.
9
+ #
10
+ # For example, if I want to set the default views for my
11
+ # application to app/views/my_defaults/*.html.erb I would
12
+ #
13
+ # InheritedViews::Base.default_views = 'my_defaults'
14
+ #
15
+ # You can also override for one specific controller from
16
+ # within it:
17
+ #
18
+ # class UsersController < InheritedViews::Base
19
+ # self.default_views = "my_default_admin_views"
20
+ # end
21
+ #
22
+ # which would then use app/views/my_default_admin_views
23
+ #
24
+ class_inheritable_accessor :default_views
25
+ self.default_views = 'inherited_views_default'
26
+
27
+ # Add our default views to the base path
28
+ ActionController::Base.append_view_path File.expand_path('../default_views', __FILE__)
29
+
30
+ include TableBuilder
31
+
32
+ #
33
+ # Actions
34
+ #
35
+
36
+ def index
37
+ super do |format|
38
+ format.html { render_or_default 'index' }
39
+ end
40
+ end
41
+
42
+ def new
43
+ super do |format|
44
+ format.html { render_or_default 'new' }
45
+ end
46
+ end
47
+
48
+ def create
49
+ create! do |success, failure|
50
+ failure.html { render_or_default 'new' }
51
+ end
52
+ end
53
+
54
+ def show
55
+ super do |format|
56
+ format.html { render_or_default 'show' }
57
+ end
58
+ end
59
+
60
+ def edit
61
+ super do |format|
62
+ format.html { render_or_default 'edit' }
63
+ end
64
+ end
65
+
66
+ def update
67
+ update! do |success, failure|
68
+ failure.html { render_or_default 'edit' }
69
+ end
70
+ end
71
+
72
+
73
+ protected
74
+
75
+ # Overriding so that we have pagination by default
76
+ def collection
77
+ get_collection_ivar || set_collection_ivar(end_of_association_chain.paginate(:page => params[:page]))
78
+ end
79
+
80
+
81
+ def show_view_columns
82
+ resource_class.columns.collect{|column| column.name.to_sym }
83
+ end
84
+ helper_method :show_view_columns
85
+
86
+ def resource_name
87
+ resource_class.human_name
88
+ end
89
+ helper_method :resource_name
90
+
91
+ def resources_name
92
+ resource_name.pluralize
93
+ end
94
+ helper_method :resources_name
95
+
96
+ private
97
+
98
+ # All the magic lives here :)
99
+ #
100
+ # This method tries to render the view you pass it, otherwise it
101
+ # renders the view from the default view folder
102
+ #
103
+ def render_or_default(name, args = {})
104
+ render name, args
105
+ rescue ActionView::MissingTemplate
106
+ render args.merge(:template => "#{self.class.default_views}/#{name}", :prefix => '')
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,4 @@
1
+ <% semantic_form_for resource do |f| %>
2
+ <%= f.inputs %>
3
+ <%= f.buttons %>
4
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <table id="<%= resource_class.name.underscore.pluralize %>_index_table" class="index_table">
2
+ <tr>
3
+ <% index_table_columns.each do |column| %>
4
+ <th><%= column.to_s.titlecase %></th>
5
+ <% end %>
6
+ </tr>
7
+ <% collection.each do |resource| %>
8
+ <tr class="<%= cycle 'odd', 'even' %>">
9
+ <% index_table_columns.each do |column| %>
10
+ <td><%= resource.send(column.to_sym) %></td>
11
+ <% end %>
12
+ <td>
13
+ <%= link_to "Edit", edit_resource_path(resource) %> |
14
+ <%= link_to "Delete", resource_path(resource), :method => :delete, :confirm => "Are you sure you want to delete this?" %>
15
+ </td>
16
+ </tr>
17
+ <% end %>
18
+ </table>
@@ -0,0 +1,2 @@
1
+ <h2>Edit <%= resource_name %></h2>
2
+ <%= render_partial_or_default 'form' %>
@@ -0,0 +1,5 @@
1
+ <h2><%= resources_name %></h2>
2
+
3
+ <%= render_partial_or_default 'table' %>
4
+
5
+ <%= will_paginate collection %>
@@ -0,0 +1,2 @@
1
+ <h2>Create <%= resource_name %></h2>
2
+ <%= render_partial_or_default 'form' %>
@@ -0,0 +1,8 @@
1
+ <h2><%= resource_name %> #<%= resource.id %></h2>
2
+
3
+ <dl id="<%= resource_class.name.underscore.pluralize %>_resource_attributes" class="resource_attributes">
4
+ <% show_view_columns.each do |column| %>
5
+ <dt><%= column.to_s.titlecase %></dt>
6
+ <dd><%= resource.send(column) %></dd>
7
+ <% end %>
8
+ </dl>
@@ -0,0 +1,24 @@
1
+ module InheritedViews
2
+ module Helpers
3
+
4
+
5
+ # Tries to render the partial, if it doesn't exist, we will
6
+ # try to find the partial in the default views folder for this
7
+ # controller.
8
+ #
9
+ # Example:
10
+ #
11
+ # in app/views/users/index.html.erb
12
+ # <%= render_partial_or_default 'item', :collection => @items %>
13
+ #
14
+ # First it will try to reder 'app/views/users/_item.html.erb'. If
15
+ # this file doesn't exist, it will lookup _item.html.erb in the
16
+ # default views folder.
17
+ def render_partial_or_default(name, options = {})
18
+ render options.merge(:partial => name)
19
+ rescue ActionView::MissingTemplate
20
+ render options.merge(:partial => "#{controller.class.default_views}/#{name}")
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,62 @@
1
+ module InheritedViews
2
+ module TableBuilder
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_inheritable_accessor :table_config
7
+ base.table_config = {}
8
+ base.send :helper_method, :index_table_columns
9
+ end
10
+
11
+ def table_config
12
+ self.class.table_config
13
+ end
14
+
15
+ def index_table_columns
16
+ table_config[:columns] || self.class.default_table_columns
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def default_table_columns
22
+ resource_class.content_columns.collect{|column| column.name.to_sym }
23
+ end
24
+
25
+
26
+ # Sets the columns to be displayed when the index table is rendered
27
+ # for this resource.
28
+ #
29
+ # Simple usage:
30
+ #
31
+ # Pass in a list of the methods to call on your resource in the order
32
+ # you wish to see them in the table
33
+ #
34
+ # class UsersController < InheritedViews::Base
35
+ # table :first_name, :last_name
36
+ # end
37
+ #
38
+ #
39
+ # Except:
40
+ #
41
+ # You can also use the default content columns from your ActiveRecord
42
+ # object and only exclude certain columns.
43
+ #
44
+ # class UsersController < InheritedViews::Base
45
+ # table :except => :first_name
46
+ # end
47
+ #
48
+ def table(*columns)
49
+ options = columns.extract_options!
50
+
51
+ if options[:except]
52
+ options[:except] = options[:except].is_a?(Array) ? options[:except] : [options[:except]]
53
+ columns = default_table_columns
54
+ options[:except].each{|c| columns.delete(c) }
55
+ end
56
+
57
+ self.table_config = options.merge(:columns => columns)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module InheritedViews
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'formtastic'
2
+ require 'will_paginate'
3
+
4
+ module InheritedViews
5
+
6
+ autoload :Base, 'inherited_views/base'
7
+ autoload :Helpers, 'inherited_views/helpers'
8
+ autoload :TableBuilder, 'inherited_views/table_builder'
9
+
10
+ end
11
+
12
+
13
+ ActionView::Base.send :include, InheritedViews::Helpers
14
+ ActionView::Base.send :include, Formtastic::SemanticFormHelper
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: inherited_views
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Greg Bell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-14 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: inherited_resources
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 6
31
+ version: 1.0.6
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: formtastic
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 9
44
+ - 8
45
+ version: 0.9.8
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: will_paginate
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 3
58
+ - 12
59
+ version: 2.3.12
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 2
71
+ - 3
72
+ - 5
73
+ version: 2.3.5
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: actionpack
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 2
85
+ - 3
86
+ - 5
87
+ version: 2.3.5
88
+ type: :development
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: mocha
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ type: :development
101
+ version_requirements: *id006
102
+ description: InheritedViews brings the DRY principle to the controller and view layers within Rails applications
103
+ email:
104
+ - gregdbell@gmail.com
105
+ executables: []
106
+
107
+ extensions: []
108
+
109
+ extra_rdoc_files: []
110
+
111
+ files:
112
+ - lib/inherited_views/base.rb
113
+ - lib/inherited_views/default_views/inherited_views_default/_form.html.erb
114
+ - lib/inherited_views/default_views/inherited_views_default/_table.html.erb
115
+ - lib/inherited_views/default_views/inherited_views_default/edit.html.erb
116
+ - lib/inherited_views/default_views/inherited_views_default/index.html.erb
117
+ - lib/inherited_views/default_views/inherited_views_default/new.html.erb
118
+ - lib/inherited_views/default_views/inherited_views_default/show.html.erb
119
+ - lib/inherited_views/helpers.rb
120
+ - lib/inherited_views/table_builder.rb
121
+ - lib/inherited_views/version.rb
122
+ - lib/inherited_views.rb
123
+ - LICENSE
124
+ - README.rdoc
125
+ has_rdoc: true
126
+ homepage: http://github.com/gregbell/inherited_views
127
+ licenses: []
128
+
129
+ post_install_message:
130
+ rdoc_options: []
131
+
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ segments:
146
+ - 0
147
+ version: "0"
148
+ requirements: []
149
+
150
+ rubyforge_project:
151
+ rubygems_version: 1.3.6
152
+ signing_key:
153
+ specification_version: 3
154
+ summary: The simplest way to implement REST controllers and views in rails applications
155
+ test_files: []
156
+