authorized_rails_scaffolds 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (18) hide show
  1. data/README.md +2 -4
  2. data/lib/authorized_rails_scaffolds/version.rb +1 -1
  3. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/_form.html.erb +14 -5
  4. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/controller.rb +21 -16
  5. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/edit.html.erb +6 -1
  6. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/index.html.erb +23 -14
  7. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/new.html.erb +6 -1
  8. data/lib/generators/authorized_rails_scaffolds/install_scaffold/templates/show.html.erb +37 -12
  9. data/lib/generators/authorized_rails_scaffolds/install_spec/install_spec_generator.rb +28 -7
  10. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/controller_spec.rb +71 -65
  11. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/devise_can_can/USAGE +7 -0
  12. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/devise_can_can/controller_macros.rb +25 -0
  13. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/edit_spec.rb +57 -43
  14. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/index_spec.rb +81 -59
  15. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/model_spec.rb +28 -0
  16. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/new_spec.rb +53 -39
  17. data/lib/generators/authorized_rails_scaffolds/install_spec/templates/show_spec.rb +85 -37
  18. metadata +11 -8
@@ -0,0 +1,7 @@
1
+ #
2
+ # Add the following to spec/spec_helper.rb
3
+ #
4
+ RSpec.configure do |config|
5
+ config.include Devise::TestHelpers, :type => :controller
6
+ config.extend DeviseCanCanControllerMacros, :type => :controller
7
+ end
@@ -0,0 +1,25 @@
1
+ module DeviceCanCanControllerMacros
2
+ def login_unauthorized_user
3
+ before(:each) do
4
+ @ability = Object.new
5
+ @ability.extend(CanCan::Ability)
6
+ @controller.stubs(:current_ability).returns(@ability)
7
+
8
+ @request.env["devise.mapping"] = Devise.mappings[:user]
9
+ @logged_in_user = FactoryGirl.create(:user)
10
+ sign_in @logged_in_user
11
+ end
12
+ end
13
+ def login_user_with_ability(action, subject)
14
+ before(:each) do
15
+ @ability = Object.new
16
+ @ability.extend(CanCan::Ability)
17
+ @ability.can action, subject
18
+ @controller.stubs(:current_ability).returns(@ability)
19
+
20
+ @request.env["devise.mapping"] = Devise.mappings[:user]
21
+ @logged_in_admin = FactoryGirl.create(:user)
22
+ sign_in @logged_in_admin
23
+ end
24
+ end
25
+ end
@@ -1,92 +1,106 @@
1
1
  require 'spec_helper'
2
2
 
3
- <% local_class_name = class_name.split('::')[-1] -%>
4
- <% output_attributes = attributes.reject{|attribute| [:timestamp].index(attribute.type) } -%>
5
- <% standard_attributes = output_attributes.reject{|attribute| [:time, :date, :datetime].index(attribute.type) } -%>
6
- <% date_attributes = output_attributes.reject{|attribute| ![:time, :date, :datetime].index(attribute.type) } -%>
7
- describe "<%= ns_table_name %>/edit" do
8
- before(:each) do
9
- @<%= file_name %> = assign(:<%= file_name %>, FactoryGirl.build_stubbed(:<%= file_name %><%= output_attributes.empty? ? '))' : ',' %>
10
- <% output_attributes.each_with_index do |attribute, attribute_index| -%>
11
3
  <%-
12
- attribute_prefix = ''
13
- attribute_suffix = ')'
14
- if attribute.type == :datetime
15
- attribute_value = value_for(attribute)
16
- attribute_prefix = 'DateTime.parse('
17
- elsif attribute.type == :time
18
- attribute_value = value_for(attribute).to_time.strftime('%T').dump
19
- attribute_prefix = 'Time.parse('
20
- elsif attribute.type == :date
21
- attribute_value = value_for(attribute)
22
- attribute_prefix = 'Date.parse('
23
- else
24
- attribute_value = value_for(attribute)
25
- attribute_suffix = ''
4
+
5
+ local_class_name = class_name.split('::')[-1] # Non-Namespaced class name
6
+ var_name = file_name # Non-namespaced variable name
7
+
8
+ output_attributes = attributes.reject{|attribute| [:timestamp].include? attribute.type }
9
+ standard_attributes = attributes.reject{|attribute| [:time, :date, :datetime].include? attribute.type }
10
+ datetime_attributes = attributes.reject{|attribute| ![:time, :date, :datetime].include? attribute.type }
11
+
12
+ # Returns code that will generate attribute_value as an attribute_type
13
+ def factory_attribute_value(attribute_type, attribute_value)
14
+ case attribute_type
15
+ when :datetime
16
+ "DateTime.parse(#{attribute_value})"
17
+ when :time
18
+ value_as_time = attribute_value.to_time.strftime('%T')
19
+ "Time.parse(#{value_as_time.dump})"
20
+ when :date
21
+ value_as_date = attribute_value.to_time.strftime('%Y-%m-%d')
22
+ "Date.parse(#{value_as_date.dump})"
23
+ else
24
+ attribute_value
25
+ end
26
26
  end
27
+
27
28
  -%>
28
- :<%= attribute.name %> => <%= attribute_prefix %><%= attribute_value %><%= attribute_suffix %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
+ describe "<%= ns_table_name %>/edit" do
30
+ before(:each) do
31
+ @<%= var_name %> = assign(:<%= var_name %>, FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? '))' : ',' %>
32
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
33
+ :<%= attribute.name %> => <%= factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
34
  <% end -%>
30
35
  <%= output_attributes.empty? ? "" : " ))\n" -%>
31
36
  end
32
37
 
33
- it "renders the edit <%= file_name %> form" do
38
+ it "renders the edit <%= var_name %> form" do
34
39
  render
35
40
 
36
41
  <% if webrat? -%>
37
- rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= file_name %>), :method => "post") do |form|
42
+ rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= var_name %>), :method => "post") do |form|
38
43
  <% for attribute in standard_attributes -%>
39
- form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
44
+ <%- if attribute.type == :references -%>
45
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>_id", :name => "<%= var_name %>[<%= attribute.name %>_id]")
46
+ <%- else -%>
47
+ form.should have_selector("<%= attribute.input_type -%>#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
48
+ <%- end -%>
40
49
  <% end -%>
41
50
  end
42
51
  <% else -%>
43
52
  # Run the generator again with the --webrat flag if you want to use webrat matchers
44
- assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= file_name %>), "post" do
53
+ assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= var_name %>), "post" do
45
54
  <% for attribute in standard_attributes -%>
46
- assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]", "<%= file_name %>[<%= attribute.name %>]"
55
+ <%- if attribute.type == :references -%>
56
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_id[name=?]", "<%= var_name %>[<%= attribute.name %>_id]"
57
+ <%- else -%>
58
+ assert_select "<%= attribute.input_type -%>#<%= var_name %>_<%= attribute.name %>[name=?]", "<%= var_name %>[<%= attribute.name %>]"
59
+ <%- end -%>
47
60
  <% end -%>
48
61
  end
49
62
  <% end -%>
50
63
  end
51
64
 
52
- <% if date_attributes.any? -%>
65
+ <% if datetime_attributes.any? -%>
53
66
  it "renders all date/time form elements" do
54
67
  render
55
68
 
56
69
  <% if webrat? -%>
57
- rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= file_name %>), :method => "post") do |form|
58
- <% for attribute in date_attributes -%>
70
+ rendered.should have_selector("form", :action => <%= ns_file_name %>_path(@<%= var_name %>), :method => "post") do |form|
71
+ <% for attribute in datetime_attributes -%>
59
72
  <%- if [:date, :datetime].include? attribute.type -%>
60
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
61
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
62
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
73
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
74
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
75
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
63
76
  <%- end -%>
64
77
  <%- if [:time, :datetime].include? attribute.type -%>
65
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
66
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
78
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
79
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
67
80
  <%- end -%>
68
81
  <% end -%>
69
82
  end
70
83
  <% else -%>
71
84
  # Run the generator again with the --webrat flag if you want to use webrat matchers
72
- assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= file_name %>), "post" do
73
- <% for attribute in date_attributes -%>
85
+ assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= var_name %>), "post" do
86
+ <% for attribute in datetime_attributes -%>
87
+ # <%= attribute.name %> values
74
88
  <%- if [:date, :datetime].include? attribute.type -%>
75
- assert_select "select#<%= file_name %>_<%= attribute.name %>_1i[name=?]", "<%= file_name %>[<%= attribute.name %>(1i)]" do
89
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_1i[name=?]", "<%= var_name %>[<%= attribute.name %>(1i)]" do
76
90
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%Y').dump %>, :count => 1
77
91
  end
78
- assert_select "select#<%= file_name %>_<%= attribute.name %>_2i[name=?]", "<%= file_name %>[<%= attribute.name %>(2i)]" do
92
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_2i[name=?]", "<%= var_name %>[<%= attribute.name %>(2i)]" do
79
93
  assert_select "option[selected=selected][value=?]", <%= DateTime.parse(attribute.default).strftime('%-m').dump %>, :text => <%= DateTime.parse(attribute.default).strftime('%B').dump %>, :count => 1
80
94
  end
81
- assert_select "select#<%= file_name %>_<%= attribute.name %>_3i[name=?]", "<%= file_name %>[<%= attribute.name %>(3i)]" do
95
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_3i[name=?]", "<%= var_name %>[<%= attribute.name %>(3i)]" do
82
96
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%d').dump %>, :count => 1
83
97
  end
84
98
  <%- end -%>
85
99
  <%- if [:time, :datetime].include? attribute.type -%>
86
- assert_select "select#<%= file_name %>_<%= attribute.name %>_4i[name=?]", "<%= file_name %>[<%= attribute.name %>(4i)]" do
100
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_4i[name=?]", "<%= var_name %>[<%= attribute.name %>(4i)]" do
87
101
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%H').dump %>, :count => 1
88
102
  end
89
- assert_select "select#<%= file_name %>_<%= attribute.name %>_5i[name=?]", "<%= file_name %>[<%= attribute.name %>(5i)]" do
103
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_5i[name=?]", "<%= var_name %>[<%= attribute.name %>(5i)]" do
90
104
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%M').dump %>, :count => 1
91
105
  end
92
106
  <%- end -%>
@@ -1,38 +1,61 @@
1
1
  require 'spec_helper'
2
2
 
3
- <%- local_class_name = class_name.split('::')[-1] -%>
4
- <%- output_attributes = attributes.reject{|attribute| [:timestamp].index(attribute.type) } -%>
3
+ <%-
4
+
5
+ local_class_name = class_name.split('::')[-1] # Non-Namespaced class name
6
+ var_name = file_name # Non-namespaced variable name
7
+ plural_var_name = var_name.pluralize # Pluralized non-namespaced variable name
8
+
9
+ output_attributes = attributes.reject{|attribute| [:timestamp].include? attribute.type }
10
+
11
+ # Returns code that will generate attribute_value as an attribute_type
12
+ def factory_attribute_value(attribute_type, attribute_value)
13
+ case attribute_type
14
+ when :datetime
15
+ "DateTime.parse(#{attribute_value})"
16
+ when :time
17
+ value_as_time = attribute_value.to_time.strftime('%T')
18
+ "Time.parse(#{value_as_time.dump})"
19
+ when :date
20
+ value_as_date = attribute_value.to_time.strftime('%Y-%m-%d')
21
+ "Date.parse(#{value_as_date.dump})"
22
+ else
23
+ attribute_value
24
+ end
25
+ end
26
+
27
+ # Returns the expected output string of attribute_value if it is an attribute_type
28
+ def factory_attribute_string(attribute_type, attribute_value)
29
+ case attribute_type
30
+ when :datetime
31
+ attribute_value_as_date = DateTime.parse(attribute_value)
32
+ I18n.l(attribute_value_as_date, :format => :long).dump
33
+ when :time
34
+ attribute_value_as_time = Time.parse(attribute_value)
35
+ I18n.l(attribute_value_as_time, :format => :short).dump
36
+ when :date
37
+ attribute_value_as_date = Date.parse(attribute_value)
38
+ I18n.l(attribute_value_as_date).dump
39
+ else
40
+ attribute_value
41
+ end
42
+ end
43
+
44
+ -%>
5
45
  describe "<%= ns_table_name %>/index" do
6
46
  before(:each) do
7
47
  <% [1,2].each_with_index do |id, model_index| -%>
8
- @<%= file_name %>_<%= model_index + 1 %> = FactoryGirl.build_stubbed(:<%= file_name %><%= output_attributes.empty? ? ')' : ',' %>
48
+ @<%= var_name %>_<%= model_index + 1 %> = FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
9
49
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
10
- <%-
11
- attribute_prefix = ''
12
- attribute_suffix = ')'
13
- if attribute.type == :datetime
14
- attribute_value = value_for(attribute)
15
- attribute_prefix = 'DateTime.parse('
16
- elsif attribute.type == :time
17
- attribute_value = value_for(attribute).to_time.strftime('%T').dump
18
- attribute_prefix = 'Time.parse('
19
- elsif attribute.type == :date
20
- attribute_value = value_for(attribute)
21
- attribute_prefix = 'Date.parse('
22
- else
23
- attribute_value = value_for(attribute)
24
- attribute_suffix = ''
25
- end
26
- -%>
27
- :<%= attribute.name %> => <%= attribute_prefix %><%= attribute_value %><%= attribute_suffix %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
50
+ :<%= attribute.name %> => <%= factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
28
51
  <% end -%>
29
52
  <% if !output_attributes.empty? -%>
30
53
  )
31
54
  <% end -%>
32
55
  <% end -%>
33
- assign(:<%= file_name.pluralize %>, [
56
+ assign(:<%= plural_var_name %>, [
34
57
  <% [1,2].each_with_index do |id, model_index| -%>
35
- @<%= file_name %>_<%= id %><%= model_index == 1 ? '' : ',' %>
58
+ @<%= var_name %>_<%= id %><%= model_index == 1 ? '' : ',' %>
36
59
  <% end -%>
37
60
  ])
38
61
  @ability = Object.new
@@ -44,72 +67,71 @@ end
44
67
  it 'includes a h1 title' do
45
68
  render
46
69
  <% if webrat? -%>
47
- rendered.should have_selector(".page-header>h1", :content => <%= file_name.humanize.pluralize.dump %>, :count => 1)
70
+ rendered.should have_selector(".page-header>h1", :content => <%= var_name.humanize.pluralize.dump %>, :count => 1)
48
71
  <% else -%>
49
- assert_select ".page-header>h1", :text => <%= file_name.humanize.pluralize.dump %>, :count => 1
72
+ assert_select ".page-header>h1", :text => <%= var_name.humanize.pluralize.dump %>, :count => 1
50
73
  <% end -%>
51
74
  end
52
75
  end
53
76
 
54
- describe "<%= file_name.pluralize %> table" do
55
- it 'includes a row for each <%= file_name %>' do
77
+ describe "<%= plural_var_name %> table" do
78
+ it 'includes a row for each <%= var_name %>' do
56
79
  render
57
80
  <% unless webrat? -%>
58
81
  # Run the generator again with the --webrat flag if you want to use webrat matchers
59
82
  <% end -%>
60
83
  <% [1,2].each do |model_index| -%>
61
84
  <% if webrat? -%>
62
- rendered.should have_selector("table>tbody>tr.<%= file_name %>_#{@<%= file_name %>_<%= model_index %>.id}", :count => 1)
85
+ rendered.should have_selector("table>tbody>tr.<%= var_name %>_#{@<%= var_name %>_<%= model_index %>.id}", :count => 1)
63
86
  <% else -%>
64
- assert_select "table>tbody>tr.<%= file_name %>_#{@<%= file_name %>_<%= model_index %>.id}", :count => 1
87
+ assert_select "table>tbody>tr.<%= var_name %>_#{@<%= var_name %>_<%= model_index %>.id}", :count => 1
65
88
  <% end -%>
66
89
  <% end -%>
67
90
  end
68
91
 
69
- it "contains a list of <%= file_name.pluralize %>" do
92
+ it "contains a list of <%= plural_var_name %>" do
70
93
  render
71
94
  <% unless webrat? -%>
72
95
  # Run the generator again with the --webrat flag if you want to use webrat matchers
73
96
  <% end -%>
74
97
  <% [1,2].each do |model_index| -%>
75
98
  <% if webrat? -%>
76
- rendered.should have_selector("tr>td.id-column", :content => @<%= file_name %>_<%= model_index %>.id.to_s, :count => 1)
99
+ rendered.should have_selector("tr>td.id-column", :content => @<%= var_name %>_<%= model_index %>.id.to_s, :count => 1)
77
100
  <% else -%>
78
- assert_select "tr>td.id-column", :text => @<%= file_name %>_<%= model_index %>.id.to_s, :count => 1
101
+ assert_select "tr>td.id-column", :text => @<%= var_name %>_<%= model_index %>.id.to_s, :count => 1
79
102
  <% end -%>
80
103
  <% end -%>
81
104
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
82
- <%-
83
- if attribute.type == :datetime
84
- date_time_value = DateTime.parse(value_for(attribute))
85
- attribute_value = I18n.l(date_time_value, :format => :long).dump
86
- elsif attribute.type == :time
87
- time_value = Time.parse(value_for(attribute))
88
- attribute_value = I18n.l(time_value, :format => :short).dump
89
- elsif attribute.type == :date
90
- date_value = Date.parse(value_for(attribute))
91
- attribute_value = I18n.l(date_value).dump
92
- else
93
- attribute_value = value_for(attribute)
94
- end
95
- -%>
96
105
  <% if webrat? -%>
97
- rendered.should have_selector("tr>td<% if attribute_index == 0 %>>a<% end %>", :content => <%= attribute_value %>.to_s, :count => 2)
106
+ rendered.should have_selector("tr>td", :content => <%= factory_attribute_string attribute.type, value_for(attribute) %>.to_s, :count => 2)
98
107
  <% else -%>
99
- assert_select "tr>td<% if attribute_index == 0 %>>a<% end %>", :text => <%= attribute_value %>.to_s, :count => 2
108
+ assert_select "tr>td", :text => <%= factory_attribute_string attribute.type, value_for(attribute) %>.to_s, :count => 2
100
109
  <% end -%>
101
110
  <% end -%>
102
111
  end
103
112
 
104
- describe 'edit <%= file_name %> link' do
113
+ describe 'show <%= var_name %> link' do
114
+ it "renders a link to <%= ns_file_name %>_path" do
115
+ render
116
+ <% [1,2].each do |model_index| -%>
117
+ <% if webrat? -%>
118
+ rendered.should have_selector("td>a[href]:not([data-method])", :href => <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1)
119
+ <% else -%>
120
+ assert_select "td>a[href=?]:not([data-method])", <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1
121
+ <% end -%>
122
+ <% end -%>
123
+ end
124
+ end
125
+
126
+ describe 'edit <%= var_name %> link' do
105
127
  context 'without update permissions' do
106
128
  it "renders a disabled link to edit_<%= ns_file_name %>_path" do
107
129
  render
108
130
  <% [1,2].each do |model_index| -%>
109
131
  <% if webrat? -%>
110
- rendered.should_not have_selector("td>a[href][disabled=disabled]", :href => edit_<%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1)
132
+ rendered.should_not have_selector("td>a[href][disabled=disabled]", :href => edit_<%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1)
111
133
  <% else -%>
112
- assert_select "td>a[href=?][disabled=disabled]", edit_<%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1
134
+ assert_select "td>a[href=?][disabled=disabled]", edit_<%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1
113
135
  <% end -%>
114
136
  <% end -%>
115
137
  end
@@ -120,24 +142,24 @@ end
120
142
  render
121
143
  <% [1,2].each do |model_index| -%>
122
144
  <% if webrat? -%>
123
- rendered.should have_selector("td>a[href]:not([disabled])", :href => edit_<%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1)
145
+ rendered.should have_selector("td>a[href]:not([disabled])", :href => edit_<%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1)
124
146
  <% else -%>
125
- assert_select "td>a[href=?]:not([disabled])", edit_<%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1
147
+ assert_select "td>a[href=?]:not([disabled])", edit_<%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1
126
148
  <% end -%>
127
149
  <% end -%>
128
150
  end
129
151
  end
130
152
  end
131
153
 
132
- describe 'destroy <%= file_name %> link' do
154
+ describe 'destroy <%= var_name %> link' do
133
155
  context 'without destroy permissions' do
134
156
  it "renders a disabled link to <%= ns_file_name %>_path" do
135
157
  render
136
158
  <% [1,2].each do |model_index| -%>
137
159
  <% if webrat? -%>
138
- rendered.should_not have_selector("td>a[href][data-method=delete][disabled=disabled]", :href => <%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1)
160
+ rendered.should_not have_selector("td>a[href][data-method=delete][disabled=disabled]", :href => <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1)
139
161
  <% else -%>
140
- assert_select "td>a[href=?][data-method=delete][disabled=disabled]", <%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1
162
+ assert_select "td>a[href=?][data-method=delete][disabled=disabled]", <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1
141
163
  <% end -%>
142
164
  <% end -%>
143
165
  end
@@ -148,9 +170,9 @@ end
148
170
  render
149
171
  <% [1,2].each do |model_index| -%>
150
172
  <% if webrat? -%>
151
- rendered.should have_selector("td>a[href][data-method=delete]:not([disabled])", :href => <%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1)
173
+ rendered.should have_selector("td>a[href][data-method=delete]:not([disabled])", :href => <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1)
152
174
  <% else -%>
153
- assert_select "td>a[href=?][data-method=delete]:not([disabled])", <%= ns_file_name %>_path(@<%= file_name %>_<%= model_index %>), :count => 1
175
+ assert_select "td>a[href=?][data-method=delete]:not([disabled])", <%= ns_file_name %>_path(@<%= var_name %>_<%= model_index %>), :count => 1
154
176
  <% end -%>
155
177
  <% end -%>
156
178
  end
@@ -158,7 +180,7 @@ end
158
180
  end
159
181
  end
160
182
 
161
- describe 'new <%= file_name %> link' do
183
+ describe 'new <%= var_name %> link' do
162
184
  context 'without create permissions' do
163
185
  it "does not render a link to new_<%= ns_file_name %>_path" do
164
186
  render
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ <% module_namespacing do -%>
4
+ describe <%= class_name %> do
5
+ <%- attributes.each do |attribute| -%>
6
+ <%- unless attribute.type == :references -%>
7
+ it { should allow_mass_assignment_of(:<%= attribute.name %>)}
8
+ <%- end -%>
9
+ <%- end -%>
10
+ <%- attributes.each_with_index do |attribute, attribute_index| -%>
11
+
12
+ # <%= attribute.name %>:<%= attribute.type %>
13
+ <%- if attribute_index == 0 -%>
14
+ it { should validate_presence_of(:<%= attributes[0].name %>) }
15
+ <%- end -%>
16
+ <%- if attribute.type == :references -%>
17
+ it { should belong_to(:<%= attribute.name %>) }
18
+ <%- elsif attribute.type == :boolean -%>
19
+ it { should allow_value(true).for(:<%= attribute.name %>) }
20
+ it { should allow_value(false).for(:<%= attribute.name %>) }
21
+ <%- elsif [:double, :decimal].include? attribute.type -%>
22
+ it { should validate_numericality_of(:<%= attribute.name %>) }
23
+ <%- elsif attribute.type == :integer -%>
24
+ it { should validate_numericality_of(:<%= attribute.name %>).only_integer }
25
+ <%- end -%>
26
+ <%- end -%>
27
+ end
28
+ <% end -%>
@@ -1,91 +1,105 @@
1
1
  require 'spec_helper'
2
2
 
3
- <% local_class_name = class_name.split('::')[-1] -%>
4
- <% output_attributes = attributes.reject{|attribute| [:timestamp].index(attribute.type) } -%>
5
- <% standard_attributes = output_attributes.reject{|attribute| [:time, :date, :datetime].index(attribute.type) } -%>
6
- <% date_attributes = output_attributes.reject{|attribute| ![:time, :date, :datetime].index(attribute.type) } -%>
7
- describe "<%= ns_table_name %>/new" do
8
- before(:each) do
9
- assign(:<%= file_name %>, FactoryGirl.build(:<%= file_name %><%= output_attributes.empty? ? '))' : ',' %>
10
- <% output_attributes.each_with_index do |attribute, attribute_index| -%>
11
3
  <%-
12
- attribute_prefix = ''
13
- attribute_suffix = ')'
14
- if attribute.type == :datetime
15
- attribute_value = value_for(attribute)
16
- attribute_prefix = 'DateTime.parse('
17
- elsif attribute.type == :time
18
- attribute_value = value_for(attribute).to_time.strftime('%T').dump
19
- attribute_prefix = 'Time.parse('
20
- elsif attribute.type == :date
21
- attribute_value = value_for(attribute)
22
- attribute_prefix = 'Date.parse('
23
- else
24
- attribute_value = value_for(attribute)
25
- attribute_suffix = ''
4
+
5
+ local_class_name = class_name.split('::')[-1] # Non-Namespaced class name
6
+ var_name = file_name # Non-namespaced variable name
7
+
8
+ output_attributes = attributes.reject{|attribute| [:timestamp].include? attribute.type }
9
+ standard_attributes = attributes.reject{|attribute| [:time, :date, :datetime].include? attribute.type }
10
+ datetime_attributes = attributes.reject{|attribute| ![:time, :date, :datetime].include? attribute.type }
11
+
12
+ # Returns code that will generate attribute_value as an attribute_type
13
+ def factory_attribute_value(attribute_type, attribute_value)
14
+ case attribute_type
15
+ when :datetime
16
+ "DateTime.parse(#{attribute_value})"
17
+ when :time
18
+ value_as_time = attribute_value.to_time.strftime('%T')
19
+ "Time.parse(#{value_as_time.dump})"
20
+ when :date
21
+ value_as_date = attribute_value.to_time.strftime('%Y-%m-%d')
22
+ "Date.parse(#{value_as_date.dump})"
23
+ else
24
+ attribute_value
25
+ end
26
26
  end
27
+
27
28
  -%>
28
- :<%= attribute.name %> => <%= attribute_prefix %><%= attribute_value %><%= attribute_suffix %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
+ describe "<%= ns_table_name %>/new" do
30
+ before(:each) do
31
+ assign(:<%= var_name %>, FactoryGirl.build(:<%= var_name %><%= output_attributes.empty? ? '))' : ',' %>
32
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
33
+ :<%= attribute.name %> => <%= factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
34
  <% end -%>
30
35
  <%= !output_attributes.empty? ? " ))\n end" : " end" %>
31
36
 
32
- it "renders new <%= file_name %> form" do
37
+ it "renders new <%= var_name %> form" do
33
38
  render
34
39
 
35
40
  <% if webrat? -%>
36
41
  rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
37
42
  <% for attribute in standard_attributes -%>
38
- form.should have_selector("<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
43
+ <%- if attribute.type == :references -%>
44
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>_id", :name => "<%= var_name %>[<%= attribute.name %>_id]")
45
+ <%- else -%>
46
+ form.should have_selector("<%= attribute.input_type -%>#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
47
+ <%- end -%>
39
48
  <% end -%>
40
49
  end
41
50
  <% else -%>
42
51
  # Run the generator again with the --webrat flag if you want to use webrat matchers
43
52
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
44
53
  <% for attribute in standard_attributes -%>
45
- assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>[name=?]", "<%= file_name %>[<%= attribute.name %>]"
54
+ <%- if attribute.type == :references -%>
55
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_id[name=?]", "<%= var_name %>[<%= attribute.name %>_id]"
56
+ <%- else -%>
57
+ assert_select "<%= attribute.input_type -%>#<%= var_name %>_<%= attribute.name %>[name=?]", "<%= var_name %>[<%= attribute.name %>]"
58
+ <%- end -%>
46
59
  <% end -%>
47
60
  end
48
61
  <% end -%>
49
62
  end
50
63
 
51
- <% if date_attributes.any? -%>
64
+ <% if datetime_attributes.any? -%>
52
65
  it "renders all date/time form elements" do
53
66
  render
54
67
 
55
68
  <% if webrat? -%>
56
69
  rendered.should have_selector("form", :action => <%= table_name %>_path, :method => "post") do |form|
57
- <% for attribute in date_attributes -%>
70
+ <% for attribute in datetime_attributes -%>
58
71
  <%- if [:date, :datetime].include? attribute.type -%>
59
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
60
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
61
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
72
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
73
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
74
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
62
75
  <%- end -%>
63
76
  <%- if [:time, :datetime].include? attribute.type -%>
64
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
65
- form.should have_selector("select#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]")
77
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
78
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
66
79
  <%- end -%>
67
80
  <% end -%>
68
81
  end
69
82
  <% else -%>
70
83
  # Run the generator again with the --webrat flag if you want to use webrat matchers
71
84
  assert_select "form[action=?][method=?]", <%= table_name %>_path, "post" do
72
- <% for attribute in date_attributes -%>
85
+ <% for attribute in datetime_attributes -%>
86
+ # <%= attribute.name %> values
73
87
  <%- if [:date, :datetime].include? attribute.type -%>
74
- assert_select "select#<%= file_name %>_<%= attribute.name %>_1i[name=?]", "<%= file_name %>[<%= attribute.name %>(1i)]" do
88
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_1i[name=?]", "<%= var_name %>[<%= attribute.name %>(1i)]" do
75
89
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%Y').dump %>, :count => 1
76
90
  end
77
- assert_select "select#<%= file_name %>_<%= attribute.name %>_2i[name=?]", "<%= file_name %>[<%= attribute.name %>(2i)]" do
91
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_2i[name=?]", "<%= var_name %>[<%= attribute.name %>(2i)]" do
78
92
  assert_select "option[selected=selected][value=?]", <%= DateTime.parse(attribute.default).strftime('%-m').dump %>, :text => <%= DateTime.parse(attribute.default).strftime('%B').dump %>, :count => 1
79
93
  end
80
- assert_select "select#<%= file_name %>_<%= attribute.name %>_3i[name=?]", "<%= file_name %>[<%= attribute.name %>(3i)]" do
94
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_3i[name=?]", "<%= var_name %>[<%= attribute.name %>(3i)]" do
81
95
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%d').dump %>, :count => 1
82
96
  end
83
97
  <%- end -%>
84
98
  <%- if [:time, :datetime].include? attribute.type -%>
85
- assert_select "select#<%= file_name %>_<%= attribute.name %>_4i[name=?]", "<%= file_name %>[<%= attribute.name %>(4i)]" do
99
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_4i[name=?]", "<%= var_name %>[<%= attribute.name %>(4i)]" do
86
100
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%H').dump %>, :count => 1
87
101
  end
88
- assert_select "select#<%= file_name %>_<%= attribute.name %>_5i[name=?]", "<%= file_name %>[<%= attribute.name %>(5i)]" do
102
+ assert_select "select#<%= var_name %>_<%= attribute.name %>_5i[name=?]", "<%= var_name %>[<%= attribute.name %>(5i)]" do
89
103
  assert_select "option[selected=selected]", :text => <%= DateTime.parse(attribute.default).strftime('%M').dump %>, :count => 1
90
104
  end
91
105
  <%- end -%>