shakes 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ <% semantic_form_for @resource do |form| %>
2
+ <%= form.inputs %>
3
+ <%= form.buttons %>
4
+ <% end %>
@@ -0,0 +1,4 @@
1
+ <h1>Editing <%= @resource_class.human_name.downcase %></h1>
2
+ <%= render :partial => 'shakes/by_convention/form' %>
3
+
4
+ <%= link_to_show_resource_path(@resource) %> | <%= link_to_index_resources_path %>
@@ -0,0 +1,27 @@
1
+ <h1>Listing <%= @resource_class.human_name.downcase.pluralize %></h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <%- @resource_class.column_names.each do |column_name| %>
6
+ <%- unless %W{id created_at updated_at}.include? column_name %>
7
+ <th><%= @resource_class.human_attribute_name column_name.to_sym %></th>
8
+ <% end -%>
9
+ <% end -%>
10
+ </tr>
11
+ <% @resources.each do |resource| %>
12
+ <tr>
13
+ <%- @resource_class.column_names.each do |column_name| %>
14
+ <%- unless %W{id created_at updated_at}.include? column_name %>
15
+ <td><%=h resource[column_name.to_sym]%></td>
16
+ <% end -%>
17
+ <% end -%>
18
+ <td><%= link_to_show_resource_path(resource) %></td>
19
+ <td><%= link_to_edit_resource_path(resource) %></td>
20
+ <td><%= link_to_destroy_resource_path(resource) %></td>
21
+ </tr>
22
+ <% end %>
23
+ </table>
24
+
25
+ <br />
26
+
27
+ <%= link_to_new_resource_path %>
@@ -0,0 +1,4 @@
1
+ <h1>New <%= @resource_class.human_name.downcase %></h1>
2
+ <%= render :partial => 'shakes/by_convention/form' %>
3
+
4
+ <%= link_to_index_resources_path %>
@@ -0,0 +1,11 @@
1
+ <%- @resource_class.column_names.each do |column_name| %>
2
+ <%- unless %W{id created_at updated_at}.include? column_name %>
3
+ <p>
4
+ <b><%= @resource_class.human_attribute_name column_name.to_sym %>:</b>
5
+ <%=h @resource[column_name.to_sym]%>
6
+ </p>
7
+ <% end -%>
8
+ <% end -%>
9
+
10
+
11
+ <%= link_to_edit_resource_path(@resource) %> | <%= link_to_index_resources_path %>
data/lib/shakes.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Shakes
2
+ autoload :ByConvention, 'shakes/by_convention'
3
+ autoload :Helpers, 'shakes/helpers'
4
+ end
5
+ ActionController::Base.send :include, Shakes::ByConvention
6
+ ActionView::Base.send :include, Shakes::Helpers
@@ -0,0 +1,175 @@
1
+ module Shakes
2
+ module ByConvention
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ CONVENTIONAL_ACTIONS = [:index, :new, :create, :show, :edit, :update, :destroy]
8
+
9
+ module ClassMethods
10
+ def by_convention(options={})
11
+ cattr_accessor :resource_type
12
+ self.resource_type = options[:resource_type] ? options[:resource_type].to_s : self.name.sub!(/Controller$/,'')
13
+ send :include, InstanceMethods
14
+
15
+ cattr_accessor :conventional_actions
16
+ self.conventional_actions = (options[:only] || CONVENTIONAL_ACTIONS) - (options[:except] || [])
17
+ send :include, IndexInstanceMethods if self.conventional_actions.include? :index
18
+ send :include, NewInstanceMethods if self.conventional_actions.include? :new
19
+ send :include, CreateInstanceMethods if self.conventional_actions.include? :create
20
+ send :include, ShowInstanceMethods if self.conventional_actions.include? :show
21
+ send :include, EditInstanceMethods if self.conventional_actions.include? :edit
22
+ send :include, UpdateInstanceMethods if self.conventional_actions.include? :update
23
+ send :include, DestroyInstanceMethods if self.conventional_actions.include? :destroy
24
+ end
25
+ end
26
+
27
+ module InstanceMethods
28
+ def controller_class
29
+ @controller_class ||= Object::const_get(self.class.name.classify)
30
+ end
31
+
32
+ def resource_class
33
+ @resource_class ||= Object::const_get(self.class.resource_type.classify)
34
+ end
35
+
36
+ def resource_collection_name
37
+ @resource_collection_name ||= self.class.resource_type.tableize.pluralize
38
+ end
39
+
40
+ def resource_instance_name
41
+ @resource_instance_name ||= self.class.resource_type.tableize.singularize
42
+ end
43
+
44
+ def render(options = nil, extra_options = {}, &block)
45
+ options ||= { :action => params[:action] }
46
+ if options.size == 1 and options[:action]
47
+ unless convetional_view_override? options[:action]
48
+ # make sure instance variables are all available
49
+ controller_class
50
+ resource_class
51
+ resource_collection_name
52
+ resource_instance_name
53
+
54
+ case options[:action]
55
+ when 'index'
56
+ super 'shakes/by_convention/index.html.erb'
57
+ when 'new'
58
+ super 'shakes/by_convention/new.html.erb'
59
+ when 'show'
60
+ super 'shakes/by_convention/show.html.erb'
61
+ when 'edit'
62
+ super 'shakes/by_convention/edit.html.erb'
63
+ else
64
+ super
65
+ end
66
+ else
67
+ super
68
+ end
69
+ else
70
+ super
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def convetional_view_override?(action)
77
+ view_paths.each do |view_path|
78
+ return true if File.exists? File.join(view_path, resource_collection_name, "#{action}.html.erb")
79
+ end
80
+ return false
81
+ end
82
+ end
83
+
84
+ module IndexInstanceMethods
85
+ def index
86
+ @resources = instance_variable_set("@#{resource_collection_name}", resource_class.all)
87
+
88
+ respond_to do |format|
89
+ format.html # index.html.erb
90
+ format.xml { render :xml => @resources }
91
+ format.json { render :json => @resources }
92
+ end
93
+ end
94
+ end
95
+
96
+ module NewInstanceMethods
97
+ def new
98
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.new)
99
+
100
+ respond_to do |format|
101
+ format.html # new.html.erb
102
+ format.xml { render :xml => @resource }
103
+ format.json { render :json => @resource }
104
+ end
105
+ end
106
+ end
107
+
108
+ module CreateInstanceMethods
109
+ def create
110
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.new(params[resource_instance_name.to_sym]))
111
+
112
+ respond_to do |format|
113
+ if @resource.save
114
+ format.html { redirect_to(@resource, :notice => "#{resource_class.to_s} was successfully created.") }
115
+ format.xml { render :xml => @resource, :status => :created, :location => @resource }
116
+ format.json { render :json => @resource, :status => :created, :location => @resource }
117
+ else
118
+ format.html { render :action => "new" }
119
+ format.xml { render :xml => @resource.errors, :status => :unprocessable_entity }
120
+ format.json { render :json => @resource.errors, :status => :unprocessable_entity }
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ module ShowInstanceMethods
127
+ def show
128
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.find(params[:id]))
129
+
130
+ respond_to do |format|
131
+ format.html # show.html.erb
132
+ format.xml { render :xml => @resource }
133
+ format.json { render :json => @resource }
134
+ end
135
+ end
136
+ end
137
+
138
+ module EditInstanceMethods
139
+ def edit
140
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.find(params[:id]))
141
+ end
142
+ end
143
+
144
+ module UpdateInstanceMethods
145
+ def update
146
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.find(params[:id]))
147
+
148
+ respond_to do |format|
149
+ if @resource.update_attributes(params[resource_instance_name.to_sym])
150
+ format.html { redirect_to(@resource, :notice => "#{resource_class.to_s} was successfully updated.") }
151
+ format.xml { head :ok }
152
+ format.json { head :ok }
153
+ else
154
+ format.html { render :action => "edit" }
155
+ format.xml { render :xml => @resource.errors, :status => :unprocessable_entity }
156
+ format.json { render :json => @resource.errors, :status => :unprocessable_entity }
157
+ end
158
+ end
159
+ end
160
+ end
161
+
162
+ module DestroyInstanceMethods
163
+ def destroy
164
+ @resource = instance_variable_set("@#{resource_instance_name}", resource_class.find(params[:id]))
165
+ @resource.destroy
166
+
167
+ respond_to do |format|
168
+ format.html { redirect_to(method("#{resource_collection_name}_url".to_sym).call) }
169
+ format.xml { head :ok }
170
+ format.json { head :ok }
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,33 @@
1
+ module Shakes
2
+ module Helpers
3
+ def link_to_index_resources_path
4
+ if @controller_class.new.respond_to? :index
5
+ link_to "Back", method("#{@resource_collection_name}_path".to_sym).call
6
+ end
7
+ end
8
+
9
+ def link_to_new_resource_path
10
+ if @controller_class.new.respond_to? :new
11
+ link_to "New #{@resource_class.human_name.downcase}", method("new_#{@resource_instance_name}_path".to_sym).call
12
+ end
13
+ end
14
+
15
+ def link_to_show_resource_path(resource)
16
+ if @controller_class.new.respond_to? :show
17
+ link_to 'Show', resource
18
+ end
19
+ end
20
+
21
+ def link_to_edit_resource_path(resource)
22
+ if @controller_class.new.respond_to? :edit
23
+ link_to 'Edit', method("edit_#{@resource_instance_name}_path".to_sym).call(resource)
24
+ end
25
+ end
26
+
27
+ def link_to_destroy_resource_path(resource)
28
+ if @controller_class.new.respond_to? :destroy
29
+ link_to 'Destroy', resource, :confirm => 'Are you sure?', :method => :delete
30
+ end
31
+ end
32
+ end
33
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'shakes'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shakes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Stahl
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-25 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: formtastic
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 43
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 8
34
+ version: 0.9.8
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Shakes provides default implementations for controller actions and views by convention.
38
+ email: jason@jasonstahl.net
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - app/views/shakes/by_convention/_form.html.erb
47
+ - app/views/shakes/by_convention/edit.html.erb
48
+ - app/views/shakes/by_convention/index.html.erb
49
+ - app/views/shakes/by_convention/new.html.erb
50
+ - app/views/shakes/by_convention/show.html.erb
51
+ - lib/shakes/by_convention.rb
52
+ - lib/shakes/helpers.rb
53
+ - lib/shakes.rb
54
+ - rails/init.rb
55
+ has_rdoc: true
56
+ homepage: http://github.com/jfs/shakes
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 57
70
+ segments:
71
+ - 1
72
+ - 8
73
+ - 7
74
+ version: 1.8.7
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 21
81
+ segments:
82
+ - 1
83
+ - 3
84
+ - 7
85
+ version: 1.3.7
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Default implementations for controllers and views
93
+ test_files: []
94
+