shakes 0.1.0 → 0.1.1

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.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +78 -0
  3. data/lib/shakes/by_convention.rb +28 -30
  4. metadata +7 -5
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jason Stahl
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.
@@ -0,0 +1,78 @@
1
+ Welcome to Shakes
2
+ =================
3
+ Using `script/generate scaffold` creates a bunch of boilerplate code that you then have to modify by hand every time you
4
+ generate if you want something slightly different. Worse yet, it doesn't respond to changes in your model as you add and
5
+ remove attributes. I found this tedious and went looking for a better solution.
6
+
7
+ Shakes provides default implementations for your Rails controller actions and views that put this boilerplate code in
8
+ once place for all controllers. You can add the boilerplate by adding just a single line to your controller. By
9
+ necessity, Shakes views are dynamic so as you make changes to your models those changes are reflected in the views.
10
+
11
+ Installing Shakes
12
+ -----------------
13
+ Add it to your environment.rb configuration as a gem dependency:
14
+
15
+ config.gem 'shakes'
16
+
17
+ Tell Rails to install the required gems:
18
+
19
+ rake gems:install
20
+
21
+ Using Shakes
22
+ ------------
23
+ Using shakes is simple. If we were working with a new Article resource, we could add the following code to our
24
+ controller and instantly have default actions and views for our new resource:
25
+
26
+ class ArticlesController < ApplicationController
27
+ by_convention
28
+ end
29
+
30
+ Overriding the default implementation is simple. You can just add your custom action implementation to your controller
31
+ as usual:
32
+
33
+ class ArticlesController < ApplicationController
34
+ by_convention
35
+
36
+ def create
37
+ # my custom create implementation
38
+ end
39
+ end
40
+
41
+ You can also limit the actions that are implemented by default passing the `only` or `except` option to `by_convention`.
42
+ For example, if you just wanted to have default implementations for `index` and `show` you would use the following code:
43
+
44
+ class ArticlesController < ApplicationController
45
+ by_convention :only => [:index, :show]
46
+ end
47
+
48
+ Override the default views is just as simple. If I wanted to change the view for show, I could just create my custom
49
+ view at `app/views/article/show.html.erb` and Shakes use that view. For example, my `app/views/article/show.html.erb`
50
+ could be:
51
+
52
+ <h1><%=h @article.title %></h1>
53
+ <p><%=h @article.content%></p>
54
+
55
+ Formtastic
56
+ ----------
57
+ The default `new` and `edit` view implementations use Formtastic to create the form HTML. To get the most out of
58
+ Formtastic, I suggest you install and use the stylesheets that are included in the gem.
59
+
60
+ Run the following command to install the stylesheets:
61
+
62
+ script/generate formtastic
63
+
64
+ Then add the following code to your layout to include the stylesheets:
65
+
66
+ <head>
67
+ ...
68
+ <%= formtastic_stylesheet_link_tag %>
69
+ ...
70
+ </head>
71
+
72
+ See the [Formtastic](http://github.com/justinfrench/formtastic) project page for more information.
73
+
74
+ TODO
75
+ ----
76
+ * Test against Rails 3 (needs GA'ed Formtastic support for Rails 3)
77
+ * Allow for application specific overrides of default implementations
78
+ * Support Haml
@@ -9,7 +9,7 @@ module Shakes
9
9
  module ClassMethods
10
10
  def by_convention(options={})
11
11
  cattr_accessor :resource_type
12
- self.resource_type = options[:resource_type] ? options[:resource_type].to_s : self.name.sub!(/Controller$/,'')
12
+ self.resource_type = options[:resource] ? options[:resource].to_s : self.name.sub!(/Controller$/,'')
13
13
  send :include, InstanceMethods
14
14
 
15
15
  cattr_accessor :conventional_actions
@@ -42,42 +42,40 @@ module Shakes
42
42
  end
43
43
 
44
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
45
+ begin
46
+ super
47
+ rescue ActionView::MissingTemplate
48
+ raise unless locate_resource?
49
+
50
+ options ||= {}
51
+ options[:action] ||= params[:action]
52
+
53
+ controller_class
54
+ resource_class
55
+ resource_collection_name
56
+ resource_instance_name
53
57
 
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
58
+ case options[:action]
59
+ when 'index'
60
+ super 'shakes/by_convention/index.html.erb'
61
+ when 'new'
62
+ super 'shakes/by_convention/new.html.erb'
63
+ when 'show'
64
+ super 'shakes/by_convention/show.html.erb'
65
+ when 'edit'
66
+ super 'shakes/by_convention/edit.html.erb'
66
67
  else
67
- super
68
+ raise
68
69
  end
69
- else
70
- super
71
70
  end
72
71
  end
73
72
 
74
- private
73
+ private
75
74
 
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
75
+ def locate_resource?
76
+ @resources ||= instance_variable_get("@#{resource_collection_name}")
77
+ @resource ||= instance_variable_get("@#{resource_instance_name}")
78
+ (@resources || @resource)
81
79
  end
82
80
  end
83
81
 
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Stahl
@@ -34,7 +34,7 @@ dependencies:
34
34
  version: 0.9.8
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
- description: Shakes provides default implementations for controller actions and views by convention.
37
+ description: Shakes provides default implementations for Rails controller actions and views.
38
38
  email: jason@jasonstahl.net
39
39
  executables: []
40
40
 
@@ -52,6 +52,8 @@ files:
52
52
  - lib/shakes/helpers.rb
53
53
  - lib/shakes.rb
54
54
  - rails/init.rb
55
+ - README.md
56
+ - LICENSE
55
57
  has_rdoc: true
56
58
  homepage: http://github.com/jfs/shakes
57
59
  licenses: []
@@ -89,6 +91,6 @@ rubyforge_project:
89
91
  rubygems_version: 1.3.7
90
92
  signing_key:
91
93
  specification_version: 3
92
- summary: Default implementations for controllers and views
94
+ summary: Default implementations for Rails controllers and views
93
95
  test_files: []
94
96