shakes 0.7.0 → 0.8.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.
data/README.md CHANGED
@@ -173,18 +173,14 @@ following command will create a new Shakes aware layout at `app/views/layouts/ar
173
173
 
174
174
  Configuring The Views
175
175
  ---------------------
176
- There are two configuration options available when rendering Shakes views. They tell Shakes to use
177
- [Formtastic](http://github.com/justinfrench/formtastic/) to render forms and Markdown to render text fields
178
- respectively. They both default to `false`. To change one or both to `true` you need to install the Shakes initializer.
179
- You can do that by running the following command (this will also generate a Shakes aware layout):
176
+ There is only one configuration options available at the moment when rendering Shakes views. It tells Shakes to use
177
+ Markdown to render text fields. It defaults to `false`. To change it to `true` you need to install the Shakes
178
+ initializer. You can do that by running the following command (this will also generate a Shakes aware layout):
180
179
 
181
180
  script/generate shakes
182
181
 
183
182
  Than you can modify `config/initializers/shakes.rb` like so:
184
183
 
185
- # Should Shakes attempt to use formtastic to render forms. Default is false
186
- Shakes::Helpers::ViewsHelper::Configuration.use_formtastic = true
187
-
188
184
  # Should Shakes attempt to use markdown to display text fields. Default is false
189
185
  Shakes::Helpers::ViewsHelper::Configuration.use_markdown = true
190
186
 
@@ -246,11 +242,11 @@ Now, if I wanted the show view to render like the original scaffold markup, I co
246
242
 
247
243
  <%- show_attributes.each do |field| -%>
248
244
  <% content_tag :p do %>
249
- <%= content_tag :b, shakes_human_attribute_name_for(field) %>
245
+ <%= content_tag :b, attribute_human_name(field) %>
250
246
  <%= h @resource[field] %>
251
247
  <% end %>
252
248
  <%- end -%>
253
- <%= shakes_link_to_for([:edit, :index], @resource) %>
249
+ <%= links_to_actions([:edit, :index], @resource) %>
254
250
 
255
251
  Alternatively, I could have just created `app/views/shakes/show.html.erb` without running the generator if that's all I
256
252
  wanted to change.
@@ -260,6 +256,7 @@ TODO
260
256
  * Add validation error messages to forms
261
257
  * Style flash messages
262
258
  * Support custom actions and views
259
+ * Support nested resources
263
260
  * Support Rails 3
264
261
 
265
262
  Copyright
@@ -1,10 +1,9 @@
1
- <%- unless Shakes::Helpers::ViewsHelper::Configuration.use_formtastic -%>
2
- <% form_for(@resource, :url => controller_action_path(controller, ((action.eql? :new) ? :create : :update ), @resource)) do |form| %>
1
+ <% form_for(@resource, :url => action_path(((action.eql? :new) ? :create : :update ), @resource)) do |form| %>
3
2
  <fieldset class="fields">
4
3
  <ol>
5
- <%- form_attributes(action).each do |field| -%>
6
- <% content_tag :li, :class => ['field', shakes_field_type_for(field).to_s].join(' ') do -%>
7
- <%= shakes_render_field field, action, @resource, form %>
4
+ <%- form_attributes(action).each do |attribute| -%>
5
+ <% content_tag :li, :class => ['attribute', attribute_type(attribute).to_s].join(' ') do -%>
6
+ <%= render_attribute action, attribute, @resource, form %>
8
7
  <%- end %>
9
8
  <%- end -%>
10
9
  </ol>
@@ -12,14 +11,8 @@
12
11
  <fieldset class="buttons">
13
12
  <ol>
14
13
  <% content_tag :li, :class => 'commit' do -%>
15
- <%= form.submit "#{((action.eql? :new) ? 'Create' : 'Update')} #{shakes_human_name}" %>
14
+ <%= form.submit "#{((action.eql? :new) ? 'Create' : 'Update')} #{controller_human_name}" %>
16
15
  <%- end %>
17
16
  </ol>
18
17
  </fieldset>
19
18
  <% end %>
20
- <%- else -%>
21
- <% semantic_form_for(@resource, :url => controller_action_path(controller, action, @resource)) do |form| %>
22
- <%= form.inputs %>
23
- <%= form.buttons %>
24
- <% end %>
25
- <%- end -%>
@@ -1,6 +1,6 @@
1
1
  <div class="article">
2
2
  <div class="section">
3
- <%= title "Edit #{shakes_human_name}", :h1 %>
3
+ <%= title "Edit #{controller_human_name}", :h1 %>
4
4
  <%= render :partial => 'shakes/form', :locals => { :action => :edit } %>
5
5
  </div>
6
6
  </div>
@@ -8,7 +8,7 @@
8
8
  <div class="section">
9
9
  <h1>Related Links</h1>
10
10
  <ul class="links">
11
- <%- shakes_link_to_for([:show, :destroy, :index], @resource).each do |link| -%>
11
+ <%- links_to_actions([:show, :destroy, :index], @resource).each do |link| -%>
12
12
  <%= content_tag :li, link%>
13
13
  <%- end -%>
14
14
  </ul>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.check_box field %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.check_box attribute %>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.date_select field %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.date_select attribute %>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.datetime_select field %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.datetime_select attribute %>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.text_field field %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.text_field attribute %>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.text_area field, :rows => 10 %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.text_area attribute, :rows => 10 %>
@@ -1,2 +1,2 @@
1
- <%= form.label field, shakes_human_attribute_name_for(field).titleize %>
2
- <%= form.time_select field %>
1
+ <%= form.label attribute, attribute_human_name(attribute).titleize %>
2
+ <%= form.time_select attribute %>
@@ -1,22 +1,22 @@
1
1
  <div class="article">
2
2
  <div class="section">
3
- <%= title "List #{shakes_human_name.pluralize}", :h1 %>
3
+ <%= title "List #{controller_human_name.pluralize}", :h1 %>
4
4
  <table class="collection">
5
5
  <tr class="headers">
6
- <%- index_attributes.each do |field| -%>
7
- <%= content_tag :th, shakes_human_attribute_name_for(field).titleize %>
6
+ <%- index_attributes.each do |attribute| -%>
7
+ <%= content_tag :th, attribute_human_name(attribute).titleize %>
8
8
  <%- end -%>
9
9
  <%= content_tag :th, 'Actions', :class => 'actions' %>
10
10
  </tr>
11
11
  <%- @resources.each do |resource| -%>
12
12
  <tr class="fields">
13
- <%- index_attributes.each do |field| -%>
14
- <%= content_tag :td, shakes_render_field(field, :index, resource),
15
- :class => ['field', shakes_field_type_for(field).to_s].join(' ') %>
13
+ <%- index_attributes.each do |attribute| -%>
14
+ <%= content_tag :td, render_attribute(:index, attribute, resource),
15
+ :class => ['field', attribute_type(attribute).to_s].join(' ') %>
16
16
  <%- end -%>
17
17
  <% content_tag :td, :class => 'actions' do %>
18
18
  <ul class="links">
19
- <% shakes_link_to_for([:show, :edit, :destroy], resource, :short_name => true).each do |link| %>
19
+ <% links_to_actions([:show, :edit, :destroy], resource, :short => true).each do |link| %>
20
20
  <%= content_tag :li, link %>
21
21
  <% end %>
22
22
  </ul>
@@ -30,7 +30,7 @@
30
30
  <div class="section">
31
31
  <h1>Related Links</h1>
32
32
  <ul class="links">
33
- <%- shakes_link_to_for([:new], @resource).each do |link| -%>
33
+ <%- links_to_actions([:new], @resource).each do |link| -%>
34
34
  <%= content_tag :li, link%>
35
35
  <%- end -%>
36
36
  </ul>
@@ -1 +1 @@
1
- <%= h(resource[field]) %>
1
+ <%= h(resource[attribute]) %>
@@ -1 +1 @@
1
- <%= resource[field].length > 255 ? shakedown("#{resource[field][0,252]}...") : shakedown(resource[field]) %>
1
+ <%= resource[attribute].length > 255 ? shakedown("#{resource[attribute][0,252]}...") : shakedown(resource[attribute]) %>
@@ -1,6 +1,6 @@
1
1
  <div class="article">
2
2
  <div class="section">
3
- <%= title "New #{shakes_human_name}", :h1 %>
3
+ <%= title "New #{controller_human_name}", :h1 %>
4
4
  <%= render :partial => 'shakes/form', :locals => { :action => :new } %>
5
5
  </div>
6
6
  </div>
@@ -8,7 +8,7 @@
8
8
  <div class="section">
9
9
  <h1>Related Links</h1>
10
10
  <ul class="links">
11
- <%- shakes_link_to_for([:index], @resource).each do |link| -%>
11
+ <%- links_to_actions([:index], @resource).each do |link| -%>
12
12
  <%= content_tag :li, link%>
13
13
  <%- end -%>
14
14
  </ul>
@@ -1,10 +1,10 @@
1
1
  <div class="article">
2
2
  <div class="section">
3
- <%= title "Show #{shakes_human_name}", :h1 %>
3
+ <%= title "Show #{controller_human_name}", :h1 %>
4
4
  <ol class="fields">
5
- <%- show_attributes.each do |field| -%>
6
- <% content_tag :li, :class => ['field', shakes_field_type_for(field).to_s].join(' ') do -%>
7
- <%= shakes_render_field field, :show, @resource %>
5
+ <%- show_attributes.each do |attribute| -%>
6
+ <% content_tag :li, :class => ['attribute', attribute_type(attribute).to_s].join(' ') do -%>
7
+ <%= render_attribute :show, attribute, @resource %>
8
8
  <%- end %>
9
9
  <%- end -%>
10
10
  </ol>
@@ -14,7 +14,7 @@
14
14
  <div class="section">
15
15
  <h1>Related Links</h1>
16
16
  <ul class="links">
17
- <%- shakes_link_to_for([:edit, :destroy, :index], @resource).each do |link| -%>
17
+ <%- links_to_actions([:edit, :destroy, :index], @resource).each do |link| -%>
18
18
  <%= content_tag :li, link%>
19
19
  <%- end -%>
20
20
  </ul>
@@ -1,2 +1,2 @@
1
- <%= content_tag :label, shakes_human_attribute_name_for(field).titleize %>
2
- <%= content_tag :span, h(resource[field]) %>
1
+ <%= content_tag :label, attribute_human_name(attribute).titleize %>
2
+ <%= content_tag :span, h(resource[attribute]) %>
@@ -1,2 +1,2 @@
1
- <%= content_tag :label, shakes_human_attribute_name_for(field).titleize %>
2
- <%= content_tag :div, shakedown(resource[field]) %>
1
+ <%= content_tag :label, attribute_human_name(attribute).titleize %>
2
+ <%= content_tag :div, shakedown(resource[attribute]) %>
@@ -6,7 +6,7 @@ module Shakes
6
6
 
7
7
  respond_to do |format|
8
8
  if @resource.save
9
- format.html { redirect_to(controller_action_path(self, :show, @resource), :notice => "#{self.class.model_class.to_s} was successfully created.") }
9
+ format.html { redirect_to(action_path(:show, @resource), :notice => "#{self.class.model_class.to_s} was successfully created.") }
10
10
  format.xml { render :xml => @resource, :status => :created, :location => @resource }
11
11
  format.json { render :json => @resource, :status => :created, :location => @resource }
12
12
  else
@@ -6,7 +6,7 @@ module Shakes
6
6
  @resource.destroy
7
7
 
8
8
  respond_to do |format|
9
- format.html { redirect_to(controller_action_path(self, :index)) }
9
+ format.html { redirect_to(action_path(:index)) }
10
10
  format.xml { head :ok }
11
11
  format.json { head :ok }
12
12
  end
@@ -6,7 +6,7 @@ module Shakes
6
6
 
7
7
  respond_to do |format|
8
8
  if @resource.update_attributes(params[self.class.model_name.to_sym])
9
- format.html { redirect_to(controller_action_path(self, :show, @resource), :notice => "#{self.class.model_class.to_s} was successfully updated.") }
9
+ format.html { redirect_to(action_path(:show, @resource), :notice => "#{self.class.model_class.to_s} was successfully updated.") }
10
10
  format.xml { head :ok }
11
11
  format.json { head :ok }
12
12
  else
@@ -88,6 +88,10 @@ module Shakes
88
88
  super "shakes/#{options[:action]}.html.erb"
89
89
  end
90
90
  end
91
+
92
+ def action_path(action, resource=nil)
93
+ controller_action_path(self, action, resource)
94
+ end
91
95
 
92
96
  private
93
97
 
@@ -2,8 +2,7 @@ module Shakes
2
2
  module Helpers
3
3
  module ViewsHelper
4
4
  class Configuration
5
- cattr_accessor :use_formtastic, :use_markdown
6
- self.use_formtastic = false
5
+ cattr_accessor :use_markdown
7
6
  self.use_markdown = false
8
7
  end
9
8
 
@@ -23,56 +22,85 @@ module Shakes
23
22
  def form_attributes(action)
24
23
  (action.eql? :new) ? controller.class.new_attributes : controller.class.edit_attributes
25
24
  end
26
-
27
- def shakedown(content, options={})
28
- Configuration.use_markdown && options[:markdown] ? markdown(h(content)) : h(content)
29
- end
30
-
31
-
32
25
 
33
- def shakes_field_type_for(field)
26
+ def attribute_type(attribute)
34
27
  columns_hash = controller.class.model_class.columns_hash
35
- (columns_hash.has_key? field.to_s) ? columns_hash[field.to_s].type : 'string'
28
+ (columns_hash.has_key? attribute.to_s) ? columns_hash[attribute.to_s].type : 'string'
29
+ end
30
+
31
+ def controller_human_name
32
+ controller.class.model_class.human_name
36
33
  end
37
34
 
38
- def shakes_human_attribute_name_for(field)
39
- controller.class.model_class.human_attribute_name field
35
+ def attribute_human_name(attribute)
36
+ controller.class.model_class.human_attribute_name attribute
37
+ end
38
+
39
+ def shakedown(text, options={})
40
+ Configuration.use_markdown && options[:markdown] ? markdown(h(text)) : h(text)
40
41
  end
41
42
 
42
- def shakes_human_name
43
- controller.class.model_class.human_name
43
+ def controller_action?(action)
44
+ controller.respond_to? action
44
45
  end
45
46
 
46
- def shakes_link_to_for(actions=[], *args)
47
+ def action_path(action, resource=nil)
48
+ controller_action_path(controller, action, resource)
49
+ end
50
+
51
+ def link_to_action(action, *args)
47
52
  resource = (args.empty? or args.first.is_a? Hash) ? nil : args.shift
48
53
  options = (args.empty?) ? {} : args.shift
49
54
  links = []
50
- options[:short_name] ||= false
55
+ options[:short] ||= false
56
+
57
+ case action
58
+ when :index
59
+ link_to(action_label(action, options), action_path(:index))
60
+ when :new
61
+ link_to(action_label(action, options), action_path(:new))
62
+ when :show
63
+ link_to(action_label(action, options), action_path(:show, resource))
64
+ when :edit
65
+ link_to(action_label(action, options), action_path(:edit, resource))
66
+ when :destroy
67
+ link_to(action_label(action, options), action_path(:destroy, resource), :confirm => 'Are you sure?', :method => :delete)
68
+ end
69
+ end
70
+
71
+ def links_to_actions(actions=[], *args)
72
+ links = []
51
73
  (actions.map { |action| action.to_sym }).each do |action|
52
- case action
53
- when :index
54
- links << link_to((options[:short_name] ? 'List' : "List all #{shakes_human_name.pluralize}"), controller_action_path(controller, :index)) if controller.respond_to? :index
55
- when :new
56
- links << link_to((options[:short_name] ? 'New' : "Create a new #{shakes_human_name}"), controller_action_path(controller, :new)) if controller.respond_to? :new
57
- when :show
58
- links << link_to((options[:short_name] ? 'Show' : "Show this #{shakes_human_name}"), controller_action_path(controller, :show, resource)) if controller.respond_to? :show
59
- when :edit
60
- links << link_to((options[:short_name] ? 'Edit' : "Edit this #{shakes_human_name}"), controller_action_path(controller, :edit, resource)) if controller.respond_to? :edit
61
- when :destroy
62
- links << link_to((options[:short_name] ? 'Delete' : "Delete this #{shakes_human_name}"), controller_action_path(controller, :destroy, resource), :confirm => 'Are you sure?', :method => :delete) if controller.respond_to? :destroy
63
- end
74
+ links << link_to_action(action, *args) if controller_action? action
64
75
  end
65
76
  links
66
77
  end
67
78
 
68
- def shakes_render_field(field, action, resource=nil, form=nil)
69
- render :partial => "shakes/#{partial_directory_for action}/#{partial_name_for field, action}",
70
- :locals => { :action => action, :field => field, :resource => resource, :form => form }
79
+ def render_attribute(action, attribute, resource=nil, form=nil)
80
+ render :partial => "shakes/#{partial_directory action}/#{partial_name action, attribute}",
81
+ :locals => { :action => action, :attribute => attribute, :resource => resource, :form => form }
71
82
  end
72
83
 
73
84
  private
74
85
 
75
- def partial_directory_for(action)
86
+ def action_label(action, options={})
87
+ (case action
88
+ when :index
89
+ ['List', "List all #{controller_human_name.pluralize}"]
90
+ when :new
91
+ ['New', "Create a new #{controller_human_name}"]
92
+ when :show
93
+ ['Show', "Show this #{controller_human_name}"]
94
+ when :edit
95
+ ['Edit', "Edit this #{controller_human_name}"]
96
+ when :destroy
97
+ ['Delete', "Delete this #{controller_human_name}"]
98
+ else
99
+ ['Unknown', "Do something unknown"]
100
+ end)[options[:short] ? 0 : 1]
101
+ end
102
+
103
+ def partial_directory(action)
76
104
  case action.to_sym
77
105
  when :new, :edit
78
106
  'form'
@@ -81,20 +109,20 @@ module Shakes
81
109
  end
82
110
  end
83
111
 
84
- def partial_name_for(field, action)
85
- field_type = shakes_field_type_for(field).to_sym
112
+ def partial_name(action, attribute)
113
+ type = attribute_type(attribute).to_sym
86
114
  case action.to_sym
87
115
  when :new, :edit
88
- case field_type
116
+ case type
89
117
  when :boolean, :date, :datetime, :text, :time, :timestamp
90
- field_type.to_s
118
+ type.to_s
91
119
  else
92
120
  'field'
93
121
  end
94
122
  else
95
- case field_type
123
+ case type
96
124
  when :text
97
- field_type.to_s
125
+ type.to_s
98
126
  else
99
127
  'field'
100
128
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shakes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 63
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
8
+ - 8
9
9
  - 0
10
- version: 0.7.0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Stahl