power_resource 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -2
- data/README.md +77 -10
- data/app/controllers/power_resource/base_controller.rb +19 -7
- data/app/helpers/power_resource/action_helper.rb +147 -0
- data/app/helpers/power_resource/base_helper.rb +51 -103
- data/app/helpers/power_resource/collection_helper.rb +20 -0
- data/app/helpers/power_resource/configuration_helper.rb +11 -0
- data/app/helpers/power_resource/rendering_helper.rb +26 -0
- data/app/views/power_resource/base/_actions.html.erb +3 -1
- data/app/views/power_resource/base/_collection.html.erb +5 -11
- data/app/views/power_resource/base/edit.html.erb +1 -1
- data/app/views/power_resource/base/index.html.erb +1 -1
- data/app/views/power_resource/base/new.html.erb +1 -1
- data/app/views/power_resource/builders/_form_for.html.erb +12 -3
- data/lib/power_resource.rb +1 -0
- data/lib/power_resource/configuration.rb +19 -0
- data/lib/power_resource/version.rb +1 -1
- data/power_resource.gemspec +9 -7
- data/spec/dummy/app/assets/javascripts/application.js +2 -0
- data/spec/dummy/app/assets/javascripts/categories.js +2 -0
- data/spec/dummy/app/assets/javascripts/comments.js +2 -0
- data/spec/dummy/app/assets/stylesheets/categories.css +4 -0
- data/spec/dummy/app/assets/stylesheets/comments.css +4 -0
- data/spec/dummy/app/controllers/backend/categories_controller.rb +7 -0
- data/spec/dummy/app/controllers/backend/comments_controller.rb +9 -0
- data/spec/dummy/app/controllers/backend/posts_controller.rb +7 -0
- data/spec/dummy/app/controllers/categories_controller.rb +5 -0
- data/spec/dummy/app/controllers/comments_controller.rb +7 -0
- data/spec/dummy/app/controllers/posts_controller.rb +3 -0
- data/spec/dummy/app/helpers/categories_helper.rb +2 -0
- data/spec/dummy/app/helpers/comments_helper.rb +2 -0
- data/spec/dummy/app/models/category.rb +5 -0
- data/spec/dummy/app/models/comment.rb +3 -0
- data/spec/dummy/app/models/post.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +23 -11
- data/spec/dummy/config/initializers/secret_token.rb +1 -1
- data/spec/dummy/config/locales/en.yml +11 -22
- data/spec/dummy/config/locales/fi.yml +38 -0
- data/spec/dummy/config/routes.rb +13 -1
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/migrate/{20130722165046_create_posts.rb → 20130915095616_create_posts.rb} +1 -0
- data/spec/dummy/db/migrate/20130916174608_create_categories.rb +9 -0
- data/spec/dummy/db/migrate/20130916180755_create_comments.rb +11 -0
- data/spec/dummy/db/schema.rb +18 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +17445 -674
- data/spec/dummy/log/test.log +20826 -2
- data/spec/features/posts_spec.rb +8 -0
- data/spec/helpers/action_helper_spec.rb +111 -0
- data/spec/helpers/base_helper_spec.rb +111 -1
- data/spec/helpers/collection_helper_spec.rb +53 -0
- data/spec/helpers/rendering_helper_spec.rb +162 -0
- data/spec/spec_helper.rb +11 -0
- metadata +92 -26
- data/app/views/power_resource/builders/_formtastic.html.erb +0 -4
- data/spec/dummy/spec/controllers/posts_controller_spec.rb +0 -5
- data/spec/dummy/spec/helpers/posts_helper_spec.rb +0 -15
- data/spec/dummy/spec/models/post_spec.rb +0 -5
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PowerResource::ActionHelper do
|
4
|
+
let(:post) { Post.create(title: 'Test post', content: 'Lorem ipsum dolor') }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@controller = PostsController.new
|
8
|
+
@controller.request = ActionDispatch::TestRequest.new
|
9
|
+
@controller.instance_variable_set('@post', post)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#resource_action_human_name' do
|
13
|
+
it 'returns humanized collection name for an index action' do
|
14
|
+
expect(helper.resource_action_human_name(:index)).to eq('Posts')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns humanized name for a new action' do
|
18
|
+
expect(helper.resource_action_human_name(:new)).to eq('New Post')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'returns humanized name for an edit action' do
|
22
|
+
expect(helper.resource_action_human_name(:edit)).to eq('Edit Post')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'returns humanized name for a delete action' do
|
26
|
+
expect(helper.resource_action_human_name(:delete)).to eq('Delete Post')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns humanized name for a custom action' do
|
30
|
+
expect(helper.resource_action_human_name(:duplicate)).to eq('Duplicate Post')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#resource_action_title' do
|
35
|
+
it 'returns humanized collection name for an index action' do
|
36
|
+
expect(helper.resource_action_title(:index)).to eq('Posts')
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns a title for a new action' do
|
40
|
+
expect(helper.resource_action_title(:new)).to eq('New Post')
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'returns a title for an edit action' do
|
44
|
+
expect(helper.resource_action_title(:edit)).to eq('Edit Post')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns a title for a delete action' do
|
48
|
+
expect(helper.resource_action_title(:delete)).to eq('Delete Post')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns a title for a custom action' do
|
52
|
+
expect(helper.resource_action_title(:duplicate)).to eq('Duplicate Post')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
### Localization specs
|
57
|
+
|
58
|
+
context 'when locale is set' do
|
59
|
+
before :each do
|
60
|
+
I18n.locale = :fi
|
61
|
+
end
|
62
|
+
|
63
|
+
after :each do
|
64
|
+
I18n.locale = I18n.default_locale
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#resource_action_human_name' do
|
68
|
+
it 'returns localized collection name for an index action' do
|
69
|
+
expect(helper.resource_action_human_name(:index)).to eq('Artikkelit')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns localized name for a new action' do
|
73
|
+
expect(helper.resource_action_human_name(:new)).to eq('Uusi artikkeli')
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'returns localized name for an edit action' do
|
77
|
+
expect(helper.resource_action_human_name(:edit)).to eq('Muokkaa Artikkeli')
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'returns localized name for a delete action' do
|
81
|
+
expect(helper.resource_action_human_name(:delete)).to eq('Poista Artikkeli')
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'returns localized name for a custom action' do
|
85
|
+
expect(helper.resource_action_human_name(:duplicate)).to eq('Kopioi Artikkeli')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#resource_action_title' do
|
90
|
+
it 'returns localized collection name for an index action' do
|
91
|
+
expect(helper.resource_action_title(:index)).to eq('Artikkelit')
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'returns localized title for a new action' do
|
95
|
+
expect(helper.resource_action_title(:new)).to eq('Uusi artikkeli')
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'returns localized title for an edit action' do
|
99
|
+
expect(helper.resource_action_title(:edit)).to eq('Muokkaa artikkelia')
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'returns localized title for a delete action' do
|
103
|
+
expect(helper.resource_action_title(:delete)).to eq('Poista Artikkeli')
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'returns localized title for a custom action' do
|
107
|
+
expect(helper.resource_action_title(:duplicate)).to eq('Kopioi Artikkeli')
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -1,8 +1,118 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PowerResource::BaseHelper do
|
4
|
+
let(:post) { Post.create(title: 'Test post', content: 'Lorem ipsum dolor') }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@controller = PostsController.new
|
8
|
+
@controller.request = ActionDispatch::TestRequest.new
|
9
|
+
@controller.instance_variable_set('@post', post)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#resource_name' do
|
13
|
+
it 'returns a name for a current resource' do
|
14
|
+
expect(helper.resource_name).to eq('post')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#resource_human_name' do
|
19
|
+
it 'returns humanized name for a current resource' do
|
20
|
+
expect(helper.resource_human_name).to eq('Post')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#resource_human_name_for' do
|
25
|
+
it 'returns humanized name for a specific resource' do
|
26
|
+
expect(helper.resource_human_name_for('Post')).to eq('Post')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#resource_title' do
|
31
|
+
it 'returns an unique title for a current resource' do
|
32
|
+
expected_title = "Post #{post.id}"
|
33
|
+
expect(helper.resource_title).to eq(expected_title)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#resource_attributes' do
|
38
|
+
it 'returns all attributes for a resource' do
|
39
|
+
expected_attributes = %w(id category_id title content created_at updated_at)
|
40
|
+
expect(helper.resource_attributes).to be == expected_attributes
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#resource_non_human_attributes' do
|
45
|
+
it 'returns attributes that should be invisible for end-users' do
|
46
|
+
expected_attributes = %w(id created_at updated_at)
|
47
|
+
expect(helper.resource_non_human_attributes).to be == expected_attributes
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#resource_human_attributes' do
|
52
|
+
it 'returns attributes for a resource without non-human attributes' do
|
53
|
+
expected_attributes = %w(category_id title content)
|
54
|
+
expect(helper.resource_human_attributes).to be == expected_attributes
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#resource_attribute_human_name_for' do
|
59
|
+
it 'returns humanized attribute name' do
|
60
|
+
expect(helper.resource_attribute_human_name_for(:title)).to eq('Title')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#attribute_value_for' do
|
65
|
+
it 'returns preformatted attribute value of a specific resource' do
|
66
|
+
expect(helper.attribute_value_for(post, :title)).to eq('Test post')
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
4
70
|
describe '#resource_form_path' do
|
5
|
-
it 'returns
|
71
|
+
it 'returns a resource path for an existing resource' do
|
72
|
+
expect(helper.resource_form_path).to eq("/posts/#{post.id}")
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns a collection path for a new resource' do
|
76
|
+
@controller.request.action = 'new'
|
77
|
+
@controller.instance_variable_set('@post', Post.new)
|
78
|
+
expect(helper.resource_form_path).to eq('/posts')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
### Localization specs
|
83
|
+
|
84
|
+
context 'when locale is set' do
|
85
|
+
before :each do
|
86
|
+
I18n.locale = :fi
|
87
|
+
end
|
88
|
+
|
89
|
+
after :each do
|
90
|
+
I18n.locale = I18n.default_locale
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#resource_human_name' do
|
94
|
+
it 'returns localized name for a current resource' do
|
95
|
+
expect(helper.resource_human_name).to eq('Artikkeli')
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#resource_human_name_for' do
|
100
|
+
it 'returns localized name for a specific resource' do
|
101
|
+
expect(helper.resource_human_name_for('Post')).to eq('Artikkeli')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '#resource_title' do
|
106
|
+
it 'returns an unique localized title for a current resource' do
|
107
|
+
expected_title = "Artikkeli nro #{post.id}"
|
108
|
+
expect(helper.resource_title(resource_id: post.id)).to eq(expected_title)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe '#resource_attribute_human_name_for' do
|
113
|
+
it 'returns localized attribute name' do
|
114
|
+
expect(helper.resource_attribute_human_name_for(:title)).to eq('Otsikko')
|
115
|
+
end
|
6
116
|
end
|
7
117
|
end
|
8
118
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PowerResource::CollectionHelper do
|
4
|
+
let(:post) { Post.create(title: 'Test post', content: 'Lorem ipsum dolor') }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@controller = PostsController.new
|
8
|
+
@controller.request = ActionDispatch::TestRequest.new
|
9
|
+
@controller.instance_variable_set('@post', post)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#collection_name' do
|
13
|
+
it 'returns a name for a current collection' do
|
14
|
+
expect(helper.collection_name).to eq('posts')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#collection_human_name' do
|
19
|
+
it 'returns humanized name for a current collection' do
|
20
|
+
expect(helper.collection_human_name).to eq('Posts')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#collection_title' do
|
25
|
+
it 'returns a title for a current collection' do
|
26
|
+
expect(helper.collection_title).to eq('Posts')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
### Localization specs
|
31
|
+
|
32
|
+
context 'when locale is set' do
|
33
|
+
before :each do
|
34
|
+
I18n.locale = :fi
|
35
|
+
end
|
36
|
+
|
37
|
+
after :each do
|
38
|
+
I18n.locale = I18n.default_locale
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#collection_human_name' do
|
42
|
+
it 'returns localized name for a current collection' do
|
43
|
+
expect(helper.collection_human_name).to eq('Artikkelit')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#collection_title' do
|
48
|
+
it 'returns localized title for a current collection' do
|
49
|
+
expect(helper.collection_title).to eq('Lista artikkeleista')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PowerResource::RenderingHelper do
|
4
|
+
let(:post) { Post.create(title: 'Test post', content: 'Lorem ipsum dolor') }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@controller = PostsController.new
|
8
|
+
@controller.request = ActionDispatch::TestRequest.new
|
9
|
+
@controller.instance_variable_set('@post', post)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#render_collection_table' do
|
13
|
+
it 'renders collection table' do
|
14
|
+
expected = '
|
15
|
+
<table class="table">
|
16
|
+
<thead>
|
17
|
+
<tr>
|
18
|
+
<th>Category</th>
|
19
|
+
<th>Title</th>
|
20
|
+
<th>Content</th>
|
21
|
+
<th> </th>
|
22
|
+
</tr>
|
23
|
+
</thead>
|
24
|
+
<tbody>
|
25
|
+
<tr>
|
26
|
+
<td></td>
|
27
|
+
<td>Test post</td>
|
28
|
+
<td>Lorem ipsum dolor</td>
|
29
|
+
<td>
|
30
|
+
<a class="btn btn-default btn-xs" href="/posts/1/comments">Comments</a>
|
31
|
+
<a class="btn btn-default btn-xs" href="/posts/1/edit">Edit</a>
|
32
|
+
<a class="btn btn-default btn-xs" data-confirm="Are you sure?" data-method="delete"
|
33
|
+
href="/posts/1" rel="nofollow">Delete</a>
|
34
|
+
</td>
|
35
|
+
</tr>
|
36
|
+
</tbody>
|
37
|
+
</table>
|
38
|
+
'
|
39
|
+
output = helper.render_collection_table.gsub(/(^ +|\n)/, '')
|
40
|
+
expect(output).to eq(expected.gsub(/(^ +|\n)/, ''))
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when form builder is form_for' do
|
45
|
+
context 'with action :edit' do
|
46
|
+
describe '#render_form' do
|
47
|
+
it 'renders edit form using form_for' do
|
48
|
+
expected = '
|
49
|
+
<form accept-charset="UTF-8" action="/posts/1" class="edit_post"
|
50
|
+
id="edit_post_1" method="post">
|
51
|
+
<div style="margin:0;padding:0;display:inline">
|
52
|
+
<input name="utf8" type="hidden" value="✓" />
|
53
|
+
<input name="_method" type="hidden" value="patch" />
|
54
|
+
</div>
|
55
|
+
<ul>
|
56
|
+
<li>
|
57
|
+
<label for="post_category_id">Category</label>
|
58
|
+
<select id="post_category_id" name="post[category_id]"></select>
|
59
|
+
</li>
|
60
|
+
<li>
|
61
|
+
<label for="post_title">Title</label>
|
62
|
+
<input id="post_title" name="post[title]" type="text"
|
63
|
+
value="Test post" />
|
64
|
+
</li>
|
65
|
+
<li>
|
66
|
+
<label for="post_content">Content</label>
|
67
|
+
<textarea id="post_content" name="post[content]">
|
68
|
+
Lorem ipsum dolor
|
69
|
+
</textarea>
|
70
|
+
</li>
|
71
|
+
</ul>
|
72
|
+
<input name="commit" type="submit" value="Update Post" />
|
73
|
+
</form>
|
74
|
+
'
|
75
|
+
output = helper.render_form.gsub(/(^ +|\n)/, '')
|
76
|
+
expect(output).to eq(expected.gsub(/(^ +|\n)/, ''))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'with action :new' do
|
82
|
+
before :each do
|
83
|
+
@controller.request.action = 'new'
|
84
|
+
@controller.instance_variable_set('@post', Post.new)
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '#render_form' do
|
88
|
+
it 'renders new form using form_for' do
|
89
|
+
expected = '
|
90
|
+
<form accept-charset="UTF-8" action="/posts" class="new_post"
|
91
|
+
id="new_post" method="post">
|
92
|
+
<div style="margin:0;padding:0;display:inline">
|
93
|
+
<input name="utf8" type="hidden" value="✓" />
|
94
|
+
</div>
|
95
|
+
<ul>
|
96
|
+
<li>
|
97
|
+
<label for="post_category_id">Category</label>
|
98
|
+
<select id="post_category_id" name="post[category_id]"></select>
|
99
|
+
</li>
|
100
|
+
<li>
|
101
|
+
<label for="post_title">Title</label>
|
102
|
+
<input id="post_title" name="post[title]" type="text" />
|
103
|
+
</li>
|
104
|
+
<li>
|
105
|
+
<label for="post_content">Content</label>
|
106
|
+
<textarea id="post_content" name="post[content]"></textarea>
|
107
|
+
</li>
|
108
|
+
</ul>
|
109
|
+
<input name="commit" type="submit" value="Create Post" />
|
110
|
+
</form>
|
111
|
+
'
|
112
|
+
output = helper.render_form.gsub(/(^ +|\n)/, '')
|
113
|
+
expect(output).to eq(expected.gsub(/(^ +|\n)/, ''))
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
### Localization specs
|
120
|
+
|
121
|
+
context 'when locale is set' do
|
122
|
+
before :each do
|
123
|
+
I18n.locale = :fi
|
124
|
+
end
|
125
|
+
|
126
|
+
after :each do
|
127
|
+
I18n.locale = I18n.default_locale
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#render_collection_table' do
|
131
|
+
it 'renders localized collection table' do
|
132
|
+
expected = '
|
133
|
+
<table class="table">
|
134
|
+
<thead>
|
135
|
+
<tr>
|
136
|
+
<th>Kategoria</th>
|
137
|
+
<th>Otsikko</th>
|
138
|
+
<th>Sisältö</th>
|
139
|
+
<th> </th>
|
140
|
+
</tr>
|
141
|
+
</thead>
|
142
|
+
<tbody>
|
143
|
+
<tr>
|
144
|
+
<td></td>
|
145
|
+
<td>Test post</td>
|
146
|
+
<td>Lorem ipsum dolor</td>
|
147
|
+
<td>
|
148
|
+
<a class="btn btn-default btn-xs" href="/posts/1/comments">Kommentit</a>
|
149
|
+
<a class="btn btn-default btn-xs" href="/posts/1/edit">Muokkaa</a>
|
150
|
+
<a class="btn btn-default btn-xs" data-confirm="Oletko varma?" data-method="delete"
|
151
|
+
href="/posts/1" rel="nofollow">Poista</a>
|
152
|
+
</td>
|
153
|
+
</tr>
|
154
|
+
</tbody>
|
155
|
+
</table>
|
156
|
+
'
|
157
|
+
output = helper.render_collection_table.gsub(/(^ +|\n)/, '')
|
158
|
+
expect(output).to eq(expected.gsub(/(^ +|\n)/, ''))
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|