shakes 0.6.0 → 0.7.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 +67 -36
- data/lib/shakes/has_the_shakes.rb +12 -12
- metadata +3 -3
data/README.md
CHANGED
|
@@ -58,14 +58,14 @@ Shakes does its best to automatically determine the attributes on your models. I
|
|
|
58
58
|
use all of the attributes defined in your migrations with the exception of `id`, `created_at` and `updated_at`, but
|
|
59
59
|
sometimes this just isn't enough.
|
|
60
60
|
|
|
61
|
-
You can use the `
|
|
61
|
+
You can use the `model_attributes` hint to help Shakes determine which attributes are available your model. You can
|
|
62
62
|
pass the options `only`, `except` or `with`. The `only` and `except` options work as you would expect. The `with` option
|
|
63
63
|
allows you to specify any additional attributes that are available. If you've defined a virtual attribute on your
|
|
64
64
|
`Article` model named `tag_list`, you would tell Shakes about it like so:
|
|
65
65
|
|
|
66
66
|
class ArticlesController < ApplicationController
|
|
67
67
|
has_the_shakes
|
|
68
|
-
|
|
68
|
+
model_attributes :with => [:tag_list]
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
If you need to control which attributes should be used for a specific view, you can do this as well. The
|
|
@@ -118,21 +118,76 @@ identical to the default `new` and `create` actions. Shakes provides a better wa
|
|
|
118
118
|
Overriding Model Instance Creation
|
|
119
119
|
----------------------------------
|
|
120
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
|
|
122
|
-
all. We could achieve the same result as above with the following code:
|
|
121
|
+
`Article` in the scope of the current user using a before filter and not have to override the default `new` and `create`
|
|
122
|
+
actions at all. We could achieve the same result as above with the following code:
|
|
123
123
|
|
|
124
124
|
class ArticlesController < ApplicationController
|
|
125
125
|
has_the_shakes
|
|
126
126
|
|
|
127
|
-
before_filter :
|
|
128
|
-
|
|
127
|
+
before_filter :new_article, :only => [:new, :create]
|
|
128
|
+
|
|
129
129
|
private
|
|
130
|
-
|
|
131
|
-
def
|
|
130
|
+
|
|
131
|
+
def new_article
|
|
132
132
|
@article = @current_user.articles.build(params[:article])
|
|
133
133
|
end
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
+
If I wanted work exclusively with the `Article` model under the scope of the current user for all actions, my code would
|
|
137
|
+
look like the following:
|
|
138
|
+
|
|
139
|
+
class ArticlesController < ApplicationController
|
|
140
|
+
has_the_shakes
|
|
141
|
+
|
|
142
|
+
before_filter :find_articles, :only => [:index]
|
|
143
|
+
before_filter :new_article, :only => [:new, :create]
|
|
144
|
+
before_filter :find_article, :except => [:index, :new, :create]
|
|
145
|
+
|
|
146
|
+
private
|
|
147
|
+
|
|
148
|
+
def find_articles
|
|
149
|
+
@articles = @current_user.articles
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def new_article
|
|
153
|
+
@article = @current_user.articles.build(params[:article])
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def find_article
|
|
157
|
+
@article = @current_user.articles.find(params[:id])
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
Creating Shakes Aware Layouts
|
|
162
|
+
-----------------------------
|
|
163
|
+
To get the most out of Shakes, I suggest you install and use the stylesheets that are included in the gem. Run the
|
|
164
|
+
following command to install the stylesheets and create a version of `app/views/layouts/application.html.erb` that
|
|
165
|
+
includes them for you:
|
|
166
|
+
|
|
167
|
+
script/generate shakes_layout
|
|
168
|
+
|
|
169
|
+
Additionally, you can create more named layouts by running the command and passing the layout name. For example, the
|
|
170
|
+
following command will create a new Shakes aware layout at `app/views/layouts/article.html.erb`:
|
|
171
|
+
|
|
172
|
+
script/generate shakes_layout article
|
|
173
|
+
|
|
174
|
+
Configuring The Views
|
|
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):
|
|
180
|
+
|
|
181
|
+
script/generate shakes
|
|
182
|
+
|
|
183
|
+
Than you can modify `config/initializers/shakes.rb` like so:
|
|
184
|
+
|
|
185
|
+
# Should Shakes attempt to use formtastic to render forms. Default is false
|
|
186
|
+
Shakes::Helpers::ViewsHelper::Configuration.use_formtastic = true
|
|
187
|
+
|
|
188
|
+
# Should Shakes attempt to use markdown to display text fields. Default is false
|
|
189
|
+
Shakes::Helpers::ViewsHelper::Configuration.use_markdown = true
|
|
190
|
+
|
|
136
191
|
Overriding The Default View
|
|
137
192
|
---------------------------
|
|
138
193
|
Overriding the default views for a specific resource is also simple. Like an action, you just create your view as usual.
|
|
@@ -155,19 +210,6 @@ If I wanted my `ArticlesController` to use a custom view for the `show` action,
|
|
|
155
210
|
</div>
|
|
156
211
|
</div>
|
|
157
212
|
|
|
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:
|
|
163
|
-
|
|
164
|
-
script/generate shakes_layout
|
|
165
|
-
|
|
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`:
|
|
168
|
-
|
|
169
|
-
script/generate shakes_layout article
|
|
170
|
-
|
|
171
213
|
Changing The Default Actions
|
|
172
214
|
----------------------------
|
|
173
215
|
You can change the default actions that Shakes uses. First you need to install the Shakes initializer at
|
|
@@ -213,24 +255,13 @@ Now, if I wanted the show view to render like the original scaffold markup, I co
|
|
|
213
255
|
Alternatively, I could have just created `app/views/shakes/show.html.erb` without running the generator if that's all I
|
|
214
256
|
wanted to change.
|
|
215
257
|
|
|
216
|
-
Finally, there are two configuration options available when rendering Shakes views. They tell Shakes to use
|
|
217
|
-
[Formtastic](http://github.com/justinfrench/formtastic/) to render forms and Markdown to render text fields
|
|
218
|
-
respectively. They both default to `false`. To change one or both to `true` you need to install the Shakes initializer.
|
|
219
|
-
You can do that by running the following command (this will also generate a Shakes aware layout):
|
|
220
|
-
|
|
221
|
-
script/generate shakes
|
|
222
|
-
|
|
223
|
-
Than you can modify `config/initializers/shakes.rb` like so:
|
|
224
|
-
|
|
225
|
-
# Should Shakes attempt to use formtastic to render forms. Default is false
|
|
226
|
-
Shakes::Helpers::ViewsHelper::Configuration.use_formtastic = true
|
|
227
|
-
|
|
228
|
-
# Should Shakes attempt to use markdown to display text fields. Default is false
|
|
229
|
-
Shakes::Helpers::ViewsHelper::Configuration.use_markdown = true
|
|
230
|
-
|
|
231
258
|
TODO
|
|
232
259
|
----
|
|
233
260
|
* Add validation error messages to forms
|
|
234
261
|
* Style flash messages
|
|
235
262
|
* Support custom actions and views
|
|
236
263
|
* Support Rails 3
|
|
264
|
+
|
|
265
|
+
Copyright
|
|
266
|
+
---------
|
|
267
|
+
Copyright (c) 2010 Jason Stahl. See LICENSE for details.
|
|
@@ -39,39 +39,39 @@ module Shakes
|
|
|
39
39
|
send :include, Configuration.destroy_action if self.shakes_actions.include? :destroy
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
-
def
|
|
43
|
-
@
|
|
44
|
-
@
|
|
42
|
+
def model_attributes(options={})
|
|
43
|
+
@model_attributes = (options[:only] || default_model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
44
|
+
@model_attributes || default_model_attributes
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
def index_attributes(options={})
|
|
48
|
-
@index_attributes = (options[:only] ||
|
|
49
|
-
@index_attributes ||
|
|
48
|
+
@index_attributes = (options[:only] || model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
49
|
+
@index_attributes || model_attributes
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
def show_attributes(options={})
|
|
53
|
-
@show_attributes = (options[:only] ||
|
|
54
|
-
@show_attributes ||
|
|
53
|
+
@show_attributes = (options[:only] || model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
54
|
+
@show_attributes || model_attributes
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
def form_attributes(options={})
|
|
58
|
-
@form_attributes = (options[:only] ||
|
|
59
|
-
@form_attributes ||
|
|
58
|
+
@form_attributes = (options[:only] || model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
59
|
+
@form_attributes || model_attributes
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
def new_attributes(options={})
|
|
63
|
-
@new_attributes = (options[:only] ||
|
|
63
|
+
@new_attributes = (options[:only] || model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
64
64
|
@new_attributes || form_attributes
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
def edit_attributes(options={})
|
|
68
|
-
@edit_attributes = (options[:only] ||
|
|
68
|
+
@edit_attributes = (options[:only] || model_attributes) + (options[:with] || []) - (options[:except] || []) unless options.empty?
|
|
69
69
|
@edit_attributes || form_attributes
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
protected
|
|
73
73
|
|
|
74
|
-
def
|
|
74
|
+
def default_model_attributes
|
|
75
75
|
(self.model_class.column_names - %w{id created_at updated_at}).map { |f| f.to_sym }
|
|
76
76
|
end
|
|
77
77
|
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:
|
|
4
|
+
hash: 3
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
8
|
+
- 7
|
|
9
9
|
- 0
|
|
10
|
-
version: 0.
|
|
10
|
+
version: 0.7.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Jason Stahl
|