shakes 0.5.0 → 0.6.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
@@ -33,13 +33,8 @@ This will create default implementations of `index`, `new`, `create`, `show`, `e
33
33
  to work with an `Article` model. Shakes determined the model by looking at the name of the controller, in this case
34
34
  `ArticlesController` because `Article`.
35
35
 
36
- If you want to change it you can pass a `resource` option to `has_the_shakes`. If we had a model named `Post`, but we
37
- want the resources to be served up by an `ArticlesController`, we could do the following:
38
-
39
- class ArticlesController < ApplicationController
40
- has_the_shakes :resource => :post
41
- end
42
-
36
+ Limiting The Actions
37
+ --------------------
43
38
  If you want to limit the actions that are included by default, you can control this by passing the `only` or `except`
44
39
  options to `has_the_shakes`. For example, if you just wanted to use the defaults for `index` and `show` you would use
45
40
  the following code:
@@ -48,12 +43,57 @@ the following code:
48
43
  has_the_shakes :only => [:index, :show]
49
44
  end
50
45
 
46
+ Managing The Model
47
+ ------------------
48
+ If you want to change the model that Shakes uses you can pass a `model` option to `has_the_shakes`. If we had a model
49
+ named `Post`, but we want the resources to be served up by an `ArticlesController`, we could do the following:
50
+
51
+ class ArticlesController < ApplicationController
52
+ has_the_shakes :model => :post
53
+ end
54
+
55
+ Managing Model Attributes
56
+ -------------------------
57
+ Shakes does its best to automatically determine the attributes on your models. If left to its own devices, Shakes will
58
+ use all of the attributes defined in your migrations with the exception of `id`, `created_at` and `updated_at`, but
59
+ sometimes this just isn't enough.
60
+
61
+ You can use the `resource_attributes` hint to help Shakes determine which attributes are available your model. You can
62
+ pass the options `only`, `except` or `with`. The `only` and `except` options work as you would expect. The `with` option
63
+ allows you to specify any additional attributes that are available. If you've defined a virtual attribute on your
64
+ `Article` model named `tag_list`, you would tell Shakes about it like so:
65
+
66
+ class ArticlesController < ApplicationController
67
+ has_the_shakes
68
+ resource_attributes :with => [:tag_list]
69
+ end
70
+
71
+ If you need to control which attributes should be used for a specific view, you can do this as well. The
72
+ `index_attributes`, `show_attributes`, `new_attributes` and `edit_attributes` hints are all available. If you want to
73
+ manage your `new` and `edit` views at the same time, there is a combined `form_attributes` hint as well. All of these
74
+ additional hints take the options `only`, `except` and `with`.
75
+
76
+ For all attribute hints, I recommend using `except` or `with` over `only`. This allows Shakes to pick up changes to your
77
+ models at the migration level.
78
+
79
+ Overriding The Default Action
80
+ -----------------------------
51
81
  Overriding the default implementation for a specific controller is simple. You add your custom action implementation to
52
- your controller as usual. If I wanted to change my create action to create my new `Article` within the scope of the
53
- current user, I could use the following code:
82
+ your controller as usual. If I wanted to change my `new` and `create` actions to create my new `Article` within the
83
+ scope of the current user, I could use the following code:
54
84
 
55
85
  class ArticlesController < ApplicationController
56
86
  has_the_shakes
87
+
88
+ def new
89
+ @article = @current_user.articles.build
90
+
91
+ respond_to do |format|
92
+ format.html # new.html.erb
93
+ format.xml { render :xml => @article }
94
+ format.json { render :json => @article }
95
+ end
96
+ end
57
97
 
58
98
  def create
59
99
  @article = @current_user.articles.build(params[:article])
@@ -72,6 +112,29 @@ current user, I could use the following code:
72
112
  end
73
113
  end
74
114
 
115
+ All we changed in the example above is how the model was created. The bulk of the implementation is fundamentally
116
+ identical to the default `new` and `create` actions. Shakes provides a better way.
117
+
118
+ Overriding Model Instance Creation
119
+ ----------------------------------
120
+ Shakes will only create an instance of your model or model collection if it doesn't already exist. We can create our new
121
+ `Article` in the scope of the current user using a before filter and not have to override the default create action at
122
+ all. We could achieve the same result as above with the following code:
123
+
124
+ class ArticlesController < ApplicationController
125
+ has_the_shakes
126
+
127
+ before_filter :create_article, :only => [:new, :create]
128
+
129
+ private
130
+
131
+ def create_article
132
+ @article = @current_user.articles.build(params[:article])
133
+ end
134
+ end
135
+
136
+ Overriding The Default View
137
+ ---------------------------
75
138
  Overriding the default views for a specific resource is also simple. Like an action, you just create your view as usual.
76
139
  If I wanted my `ArticlesController` to use a custom view for the `show` action, I would just create my custom view at
77
140
  `app/views/article/show.html.erb`:
@@ -92,29 +155,18 @@ If I wanted my `ArticlesController` to use a custom view for the `show` action,
92
155
  </div>
93
156
  </div>
94
157
 
95
- Managing Attributes
96
- -------------------
97
- Shakes does its best to automatically determine the attributes on your models. If left to its own devices, Shakes will
98
- use all of the attributes defined in your migrations with the exception of `id`, `created_at` and `updated_at`, but
99
- sometimes this just isn't enough.
100
-
101
- You can use the `resource_attributes` hint to help Shakes determine which attributes are available your model. You can
102
- pass the options `only`, `except` or `with`. The `only` and `except` options work as you would expect. The `with` option
103
- allows you to specify any additional attributes that are available. If you've defined a virtual attribute on your
104
- `Article` model named `tag_list`, you would tell Shakes about it like so:
158
+ Creating Shakes Aware Layouts
159
+ -----------------------------
160
+ To get the most out of Shakes, I suggest you install and use the stylesheets that are included in the gem. Run the
161
+ following command to install the stylesheets and create a version of `app/views/layouts/application.html.erb` that
162
+ includes them for you:
105
163
 
106
- class ArticlesController < ApplicationController
107
- has_the_shakes
108
- resource_attributes :with => [:tag_list]
109
- end
164
+ script/generate shakes_layout
110
165
 
111
- If you need to control which attributes should be used for a specific view, you can do this as well. The
112
- `index_attributes`, `show_attributes`, `new_attributes` and `edit_attributes` hints are all available. If you want to
113
- manage your `new` and `edit` views at the same time, there is a combined `form_attributes` hint as well. All of these
114
- additional hints take the options `only`, `except` and `with`.
166
+ Additionally, you can create more named layouts by running the command and passing the layout name. For example, the
167
+ following command will create a new Shakes aware layout at `app/views/layouts/article.html.erb`:
115
168
 
116
- For all attribute hints, I recommend using `except` or `with` over `only`. This allows Shakes to pick up changes to your
117
- models at the migration level.
169
+ script/generate shakes_layout article
118
170
 
119
171
  Changing The Default Actions
120
172
  ----------------------------
@@ -176,22 +228,9 @@ Than you can modify `config/initializers/shakes.rb` like so:
176
228
  # Should Shakes attempt to use markdown to display text fields. Default is false
177
229
  Shakes::Helpers::ViewsHelper::Configuration.use_markdown = true
178
230
 
179
-
180
- Creating Shakes Aware Layouts
181
- -----------------------------
182
- To get the most out of Shakes, I suggest you install and use the stylesheets that are included in the gem. Run the
183
- following command to install the stylesheets and create a version of `app/views/layouts/application.html.erb` that
184
- includes them for you:
185
-
186
- script/generate shakes_layout
187
-
188
- Additionally, you can create more named layouts by running the command and passing the layout name. For example, the
189
- following command will create a new Shakes aware layout at `app/views/layouts/article.html.erb`:
190
-
191
- script/generate shakes_layout article
192
-
193
231
  TODO
194
232
  ----
195
233
  * Add validation error messages to forms
196
234
  * Style flash messages
235
+ * Support custom actions and views
197
236
  * Support Rails 3
@@ -2,11 +2,11 @@ module Shakes
2
2
  module Actions
3
3
  module Create
4
4
  def create
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.new(params[self.class.resource_name.to_sym]))
5
+ @resource ||= create_resource
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.resource_class.to_s} was successfully created.") }
9
+ format.html { redirect_to(controller_action_path(self, :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
@@ -16,6 +16,12 @@ module Shakes
16
16
  end
17
17
  end
18
18
  end
19
+
20
+ private
21
+
22
+ def create_resource
23
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.new(params[self.class.model_name.to_sym])))
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -2,7 +2,7 @@ module Shakes
2
2
  module Actions
3
3
  module Destroy
4
4
  def destroy
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.find(params[:id]))
5
+ @resource ||= destroy_resource
6
6
  @resource.destroy
7
7
 
8
8
  respond_to do |format|
@@ -11,6 +11,12 @@ module Shakes
11
11
  format.json { head :ok }
12
12
  end
13
13
  end
14
+
15
+ private
16
+
17
+ def destroy_resource
18
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.find(params[:id])))
19
+ end
14
20
  end
15
21
  end
16
22
  end
@@ -2,7 +2,13 @@ module Shakes
2
2
  module Actions
3
3
  module Edit
4
4
  def edit
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.find(params[:id]))
5
+ @resource ||= edit_resource
6
+ end
7
+
8
+ private
9
+
10
+ def edit_resource
11
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.find(params[:id])))
6
12
  end
7
13
  end
8
14
  end
@@ -2,7 +2,7 @@ module Shakes
2
2
  module Actions
3
3
  module Index
4
4
  def index
5
- @resources = instance_variable_set("@#{self.class.resource_collection_name}", self.class.resource_class.all)
5
+ @resources ||= index_resources
6
6
 
7
7
  respond_to do |format|
8
8
  format.html # index.html.erb
@@ -10,6 +10,12 @@ module Shakes
10
10
  format.json { render :json => @resources }
11
11
  end
12
12
  end
13
+
14
+ private
15
+
16
+ def index_resources
17
+ (instance_variable_get("@#{self.class.model_collection_name}") || instance_variable_set("@#{self.class.model_collection_name}", self.class.model_class.all))
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -2,7 +2,7 @@ module Shakes
2
2
  module Actions
3
3
  module New
4
4
  def new
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.new)
5
+ @resource ||= new_resource
6
6
 
7
7
  respond_to do |format|
8
8
  format.html # new.html.erb
@@ -10,6 +10,12 @@ module Shakes
10
10
  format.json { render :json => @resource }
11
11
  end
12
12
  end
13
+
14
+ private
15
+
16
+ def new_resource
17
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.new()))
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -2,7 +2,7 @@ module Shakes
2
2
  module Actions
3
3
  module Show
4
4
  def show
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.find(params[:id]))
5
+ @resource ||= show_resource
6
6
 
7
7
  respond_to do |format|
8
8
  format.html # show.html.erb
@@ -10,6 +10,12 @@ module Shakes
10
10
  format.json { render :json => @resource }
11
11
  end
12
12
  end
13
+
14
+ private
15
+
16
+ def show_resource
17
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.find(params[:id])))
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -2,11 +2,11 @@ module Shakes
2
2
  module Actions
3
3
  module Update
4
4
  def update
5
- @resource = instance_variable_set("@#{self.class.resource_name}", self.class.resource_class.find(params[:id]))
5
+ @resource ||= update_resource
6
6
 
7
7
  respond_to do |format|
8
- if @resource.update_attributes(params[self.class.resource_name.to_sym])
9
- format.html { redirect_to(controller_action_path(self, :show, @resource), :notice => "#{self.class.resource_class.to_s} was successfully updated.") }
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.") }
10
10
  format.xml { head :ok }
11
11
  format.json { head :ok }
12
12
  else
@@ -16,6 +16,12 @@ module Shakes
16
16
  end
17
17
  end
18
18
  end
19
+
20
+ private
21
+
22
+ def update_resource
23
+ (instance_variable_get("@#{self.class.model_name}") || instance_variable_set("@#{self.class.model_name}", self.class.model_class.find(params[:id])))
24
+ end
19
25
  end
20
26
  end
21
27
  end
@@ -20,16 +20,14 @@ module Shakes
20
20
  module ClassMethods
21
21
  def has_the_shakes(options={})
22
22
 
23
- cattr_accessor :resource_name
24
- self.resource_name ||= options[:resource] ? options[:resource].to_s.singularize : self.controller_name.singularize
25
- cattr_accessor :resource_collection_name
26
- self.resource_collection_name ||= self.resource_name.pluralize
27
- cattr_accessor :resource_class
28
- self.resource_class ||= Object::const_get(self.resource_name.classify)
23
+ cattr_accessor :model_name
24
+ self.model_name ||= options[:model] ? options[:model].to_s.singularize : self.controller_name.singularize
25
+ cattr_accessor :model_collection_name
26
+ self.model_collection_name ||= self.model_name.pluralize
27
+ cattr_accessor :model_class
28
+ self.model_class ||= Object::const_get(self.model_name.classify)
29
29
  send :include, InstanceMethods
30
30
 
31
- # puts "resource: #{self.method(:resource)}"
32
-
33
31
  cattr_accessor :shakes_actions
34
32
  self.shakes_actions = (options[:only] || SHAKES_ACTIONS) - (options[:except] || [])
35
33
  send :include, Configuration.index_action if self.shakes_actions.include? :index
@@ -74,7 +72,7 @@ module Shakes
74
72
  protected
75
73
 
76
74
  def default_resource_attributes
77
- (self.resource_class.column_names - %w{id created_at updated_at}).map { |f| f.to_sym }
75
+ (self.model_class.column_names - %w{id created_at updated_at}).map { |f| f.to_sym }
78
76
  end
79
77
  end
80
78
 
@@ -94,8 +92,8 @@ module Shakes
94
92
  private
95
93
 
96
94
  def locate_resource?
97
- @resources ||= instance_variable_get("@#{self.class.resource_collection_name}")
98
- @resource ||= instance_variable_get("@#{self.class.resource_name}")
95
+ @resources ||= instance_variable_get("@#{self.class.model_collection_name}")
96
+ @resource ||= instance_variable_get("@#{self.class.model_name}")
99
97
  (@resources || @resource)
100
98
  end
101
99
  end
@@ -31,16 +31,16 @@ module Shakes
31
31
 
32
32
 
33
33
  def shakes_field_type_for(field)
34
- columns_hash = controller.class.resource_class.columns_hash
34
+ columns_hash = controller.class.model_class.columns_hash
35
35
  (columns_hash.has_key? field.to_s) ? columns_hash[field.to_s].type : 'string'
36
36
  end
37
37
 
38
38
  def shakes_human_attribute_name_for(field)
39
- controller.class.resource_class.human_attribute_name field
39
+ controller.class.model_class.human_attribute_name field
40
40
  end
41
41
 
42
42
  def shakes_human_name
43
- controller.class.resource_class.human_name
43
+ controller.class.model_class.human_name
44
44
  end
45
45
 
46
46
  def shakes_link_to_for(actions=[], *args)
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: 11
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 0.5.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Stahl