model_base_generators 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -0
  3. data/Rakefile +13 -1
  4. data/example/.gitignore +2 -0
  5. data/example/.rspec +2 -0
  6. data/example/Gemfile +72 -0
  7. data/example/Gemfile.lock +304 -0
  8. data/example/Rakefile +6 -0
  9. data/example/app/assets/config/manifest.js +5 -0
  10. data/example/app/assets/images/.keep +0 -0
  11. data/example/app/assets/javascripts/application.js +13 -0
  12. data/example/app/assets/javascripts/cable.js +13 -0
  13. data/example/app/assets/javascripts/channels/.keep +0 -0
  14. data/example/app/assets/stylesheets/application.css +15 -0
  15. data/example/app/channels/application_cable/channel.rb +4 -0
  16. data/example/app/channels/application_cable/connection.rb +4 -0
  17. data/example/app/controllers/application_controller.rb +3 -0
  18. data/example/app/controllers/concerns/.keep +0 -0
  19. data/example/app/controllers/concerns/authentication.rb +12 -0
  20. data/{examples → example}/app/controllers/issues_controller.rb +1 -1
  21. data/example/app/controllers/projects_controller.rb +62 -0
  22. data/example/app/helpers/application_helper.rb +2 -0
  23. data/example/app/helpers/issues_helper.rb +2 -0
  24. data/example/app/helpers/projects_helper.rb +2 -0
  25. data/example/app/jobs/application_job.rb +2 -0
  26. data/example/app/mailers/application_mailer.rb +4 -0
  27. data/example/app/models/ability.rb +35 -0
  28. data/example/app/models/application_record.rb +3 -0
  29. data/example/app/models/concerns/.keep +0 -0
  30. data/example/app/models/issue.rb +16 -0
  31. data/example/app/models/project.rb +6 -0
  32. data/example/app/models/user.rb +25 -0
  33. data/example/app/validations/ar_internal_metadatum_validation.rb +8 -0
  34. data/example/app/validations/issue_validation.rb +9 -0
  35. data/example/app/validations/project_validation.rb +8 -0
  36. data/example/app/validations/user_validation.rb +10 -0
  37. data/{examples → example}/app/views/issues/_form.html.erb +8 -1
  38. data/{examples/app/views/issues/index.html.erb → example/app/views/issues/_table.html.erb} +2 -7
  39. data/{examples → example}/app/views/issues/edit.html.erb +0 -0
  40. data/example/app/views/issues/index.html.erb +9 -0
  41. data/{examples → example}/app/views/issues/new.html.erb +0 -0
  42. data/{examples → example}/app/views/issues/show.html.erb +6 -0
  43. data/example/app/views/layouts/application.html.erb +14 -0
  44. data/example/app/views/layouts/mailer.html.erb +13 -0
  45. data/example/app/views/layouts/mailer.text.erb +1 -0
  46. data/{examples → example}/app/views/projects/_form.html.erb +7 -0
  47. data/{examples/app/views/projects/index.html.erb → example/app/views/projects/_table.html.erb} +2 -9
  48. data/{examples → example}/app/views/projects/edit.html.erb +0 -0
  49. data/example/app/views/projects/index.html.erb +9 -0
  50. data/{examples → example}/app/views/projects/new.html.erb +0 -0
  51. data/{examples → example}/app/views/projects/show.html.erb +2 -0
  52. data/example/bin/bundle +3 -0
  53. data/example/bin/rails +4 -0
  54. data/example/bin/rake +4 -0
  55. data/example/bin/setup +34 -0
  56. data/example/bin/update +29 -0
  57. data/example/config/application.rb +20 -0
  58. data/example/config/boot.rb +5 -0
  59. data/example/config/cable.yml +9 -0
  60. data/example/config/database.yml +25 -0
  61. data/example/config/environment.rb +5 -0
  62. data/example/config/environments/development.rb +56 -0
  63. data/example/config/environments/production.rb +86 -0
  64. data/example/config/environments/test.rb +42 -0
  65. data/example/config/initializers/application_controller_renderer.rb +6 -0
  66. data/example/config/initializers/assets.rb +11 -0
  67. data/example/config/initializers/backtrace_silencers.rb +7 -0
  68. data/example/config/initializers/cookies_serializer.rb +5 -0
  69. data/example/config/initializers/devise.rb +274 -0
  70. data/example/config/initializers/filter_parameter_logging.rb +4 -0
  71. data/example/config/initializers/inflections.rb +16 -0
  72. data/example/config/initializers/mime_types.rb +4 -0
  73. data/example/config/initializers/new_framework_defaults.rb +24 -0
  74. data/example/config/initializers/pretty_validation.rb +5 -0
  75. data/example/config/initializers/session_store.rb +3 -0
  76. data/example/config/initializers/wrap_parameters.rb +14 -0
  77. data/example/config/locales/devise.en.yml +62 -0
  78. data/example/config/locales/en.yml +23 -0
  79. data/example/config/puma.rb +47 -0
  80. data/example/config/routes.rb +6 -0
  81. data/example/config/secrets.yml +22 -0
  82. data/example/config/spring.rb +6 -0
  83. data/example/config.ru +5 -0
  84. data/example/db/schema.rb +34 -0
  85. data/example/lib/assets/.keep +0 -0
  86. data/example/log/.keep +0 -0
  87. data/example/public/404.html +67 -0
  88. data/example/public/422.html +67 -0
  89. data/example/public/500.html +66 -0
  90. data/example/public/apple-touch-icon-precomposed.png +0 -0
  91. data/example/public/apple-touch-icon.png +0 -0
  92. data/example/public/favicon.ico +0 -0
  93. data/{examples → example}/spec/controllers/issues_controller_spec.rb +6 -6
  94. data/{examples → example}/spec/controllers/projects_controller_spec.rb +4 -4
  95. data/example/spec/factories/issues.rb +8 -0
  96. data/example/spec/factories/projects.rb +6 -0
  97. data/example/spec/factories/users.rb +8 -0
  98. data/example/spec/rails_helper.rb +57 -0
  99. data/{examples → example}/spec/routing/issues_routing_spec.rb +0 -0
  100. data/{examples → example}/spec/routing/projects_routing_spec.rb +0 -0
  101. data/example/spec/spec_helper.rb +99 -0
  102. data/example/spec/support/controller_macros.rb +18 -0
  103. data/example/spec/support/devise.rb +16 -0
  104. data/{examples → example}/spec/views/issues/edit.html.erb_spec.rb +4 -1
  105. data/example/spec/views/issues/index.html.erb_spec.rb +21 -0
  106. data/{examples → example}/spec/views/issues/new.html.erb_spec.rb +4 -1
  107. data/example/spec/views/issues/show.html.erb_spec.rb +17 -0
  108. data/{examples → example}/spec/views/projects/edit.html.erb_spec.rb +3 -1
  109. data/example/spec/views/projects/index.html.erb_spec.rb +18 -0
  110. data/{examples → example}/spec/views/projects/new.html.erb_spec.rb +3 -1
  111. data/example/spec/views/projects/show.html.erb_spec.rb +14 -0
  112. data/example/tmp/.keep +0 -0
  113. data/lib/model_base/column_attribute.rb +63 -2
  114. data/lib/model_base/config.rb +13 -2
  115. data/lib/model_base/generators/erb/scaffold.rb +23 -0
  116. data/lib/model_base/generators/factory_girl/model.rb +37 -0
  117. data/lib/model_base/meta_model.rb +77 -13
  118. data/lib/model_base/version.rb +1 -1
  119. data/lib/model_base.rb +4 -0
  120. data/lib/templates/controller.rb +1 -1
  121. data/lib/templates/erb/scaffold/_form.html.erb +1 -1
  122. data/lib/templates/erb/scaffold/_table.html.erb +39 -0
  123. data/lib/templates/erb/scaffold/index.html.erb +1 -38
  124. data/lib/templates/erb/scaffold/show.html.erb +3 -1
  125. data/lib/templates/factory_girl/factory.rb +11 -0
  126. data/lib/templates/rspec/scaffold/controller_spec.rb +8 -13
  127. data/lib/templates/rspec/scaffold/edit_spec.rb +3 -3
  128. data/lib/templates/rspec/scaffold/index_spec.rb +13 -3
  129. data/lib/templates/rspec/scaffold/new_spec.rb +3 -2
  130. data/lib/templates/rspec/scaffold/show_spec.rb +4 -4
  131. metadata +115 -25
  132. data/examples/spec/views/issues/index.html.erb_spec.rb +0 -17
  133. data/examples/spec/views/issues/show.html.erb_spec.rb +0 -14
  134. data/examples/spec/views/projects/index.html.erb_spec.rb +0 -15
  135. data/examples/spec/views/projects/show.html.erb_spec.rb +0 -12
@@ -10,13 +10,19 @@ module ModelBase
10
10
  attr_reader :reference
11
11
  attr_reader :model, :column
12
12
 
13
- def initialize(model, name, type, column: nil, reference: nil, index_type: false, attr_options: {})
13
+ def initialize(model, name, type, column: nil, reference: nil, index_type: false, title: false, attr_options: {})
14
14
  super(name, type, index_type, attr_options)
15
15
  @model = model
16
16
  @reference = reference
17
17
  @column = column
18
+ @title = !!title
18
19
  end
19
20
 
21
+ def title?
22
+ @title
23
+ end
24
+ attr_writer :title
25
+
20
26
  def ref_model
21
27
  unless defined?(@ref_model)
22
28
  @ref_model = reference.nil? ? nil : ModelBase::MetaModel.new(reference.class_name)
@@ -42,6 +48,61 @@ module ModelBase
42
48
  select_renderer ? :select : super
43
49
  end
44
50
 
51
+ LOCALIZED_TYPES = [:time, :datetime].freeze
52
+ def to_be_localized?
53
+ LOCALIZED_TYPES.include?(type)
54
+ end
55
+
56
+ def sample_value(idx = 1, context: nil)
57
+ if name == 'id'
58
+ idx
59
+ elsif name == 'email' && type == :string
60
+ 'user1@example.com'
61
+ elsif title?
62
+ model.full_resource_name + idx.to_s
63
+ elsif ref_model
64
+ if tc = ref_model.title_column
65
+ tc.sample_value(idx)
66
+ else
67
+ 1
68
+ end
69
+ elsif enumerized?
70
+ enum = model.model_class.send(name)
71
+ r = enum.values.first
72
+ context == :factory ? r.to_sym : r.text
73
+ else
74
+ @default ||= case type
75
+ when :integer then idx
76
+ when :float then idx + 0.5
77
+ when :decimal then "#{idx}.99"
78
+ when :datetime, :timestamp, :time then Time.now.to_s(:db)
79
+ when :date then Date.today.to_s(:db)
80
+ when :string then
81
+ case name
82
+ when 'type' then ""
83
+ else "#{model.full_resource_name}_#{name}_#{idx}"
84
+ end
85
+ when :text then "#{model.full_resource_name}_#{name}_#{idx}"
86
+ when :boolean then false
87
+ when :references, :belongs_to then nil
88
+ else
89
+ ""
90
+ end
91
+ end
92
+ end
93
+
94
+ def sample_string(idx = 1)
95
+ "'%s'" % sample_value(idx)
96
+ end
97
+
98
+ def new_attribute_exp
99
+ if enumerized?
100
+ '%s.%s.values[%d]' % [model.name, name, 1] # 2nd value
101
+ else
102
+ "valid_parameters[:#{name}].succ"
103
+ end
104
+ end
105
+
45
106
  class AbstractSelectRenderer
46
107
  attr_reader :column_attr
47
108
  def initialize(column_attr)
@@ -73,7 +134,7 @@ module ModelBase
73
134
 
74
135
  class EnumerizedSelectRenderer < AbstractSelectRenderer
75
136
  def render_core(form_name, target_name, options = {})
76
- "#{form_name}.select :#{column_attr.name}, #{column_attr.model.name}.#{column_attr.name}.optoins"
137
+ "#{form_name}.select :#{column_attr.name}, #{column_attr.model.name}.#{column_attr.name}.options"
77
138
  end
78
139
  end
79
140
  end
@@ -6,11 +6,22 @@ module ModelBase
6
6
 
7
7
  config_accessor(:disabled){ false }
8
8
 
9
- config_accessor(:excluded_columns) do
9
+ base_exclusions =
10
10
  [
11
11
  /.*_checksum/,
12
12
  /.*_count/,
13
- ] + %w[_id _type id created_at updated_at]
13
+ ] + %w[_id _type id]
14
+
15
+ config_accessor(:excluded_columns_of_show) do
16
+ base_exclusions
17
+ end
18
+
19
+ config_accessor(:excluded_columns_of_index) do
20
+ base_exclusions + %w[updated_at]
21
+ end
22
+
23
+ config_accessor(:excluded_columns_of_form) do
24
+ base_exclusions + %w[created_at updated_at]
14
25
  end
15
26
 
16
27
  config_accessor(:title_column_candidates) do
@@ -0,0 +1,23 @@
1
+ require "model_base"
2
+
3
+ require 'rails/generators/erb/scaffold/scaffold_generator'
4
+
5
+ module ModelBase
6
+ module Generators
7
+ module Erb
8
+
9
+ module Scaffold
10
+ def self.enable!
11
+ ::Erb::Generators::ScaffoldGenerator.prepend(self)
12
+ end
13
+
14
+ protected
15
+
16
+ def available_views
17
+ %w(index edit show new _form _table)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ require "model_base"
2
+
3
+ require 'generators/factory_girl/model/model_generator'
4
+
5
+ module ModelBase
6
+ module Generators
7
+ module FactoryGirl
8
+
9
+ module Model
10
+ def self.enable!
11
+ ::FactoryGirl::Generators::ModelGenerator.prepend(self)
12
+ ::FactoryGirl::Generators::ModelGenerator.extend(ClassMethod)
13
+ ::FactoryGirl::Generators::ModelGenerator.instance_eval do
14
+ source_root File.expand_path("../../../../templates/factory_girl", __FILE__)
15
+ end
16
+ end
17
+
18
+ module ClassMethod
19
+ # To overwrite `source_root` again, which is overwritten by factory_girl_rails
20
+ # https://github.com/thoughtbot/factory_girl_rails/blob/master/lib/generators/factory_girl.rb#L6-L8
21
+ # https://github.com/rails/rails/blob/master/railties/lib/rails/generators/base.rb#L22-L26
22
+ def source_root(path=nil)
23
+ @_source_root = path if path
24
+ @_source_root ||= default_source_root
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def create_factory_file
31
+ template 'factory.rb', File.join(options[:dir], "#{filename}.rb")
32
+ end
33
+
34
+ end
35
+ end
36
+ end
37
+ end
@@ -30,10 +30,6 @@ module ModelBase
30
30
  full_resource_name.pluralize
31
31
  end
32
32
 
33
- def columns
34
- @columns ||= retrieve_columns.reject {|c| excluded?(c.name) }
35
- end
36
-
37
33
  def model_class
38
34
  @model_class ||= name.constantize
39
35
  end
@@ -42,6 +38,11 @@ module ModelBase
42
38
  defined?(ActiveRecord) == "constant" && ActiveRecord.class == Module && model_class < ActiveRecord::Base
43
39
  end
44
40
 
41
+ def title_column
42
+ retrieve_columns unless defined?(@title_column)
43
+ @title_column
44
+ end
45
+
45
46
  def retrieve_columns
46
47
  raw_cols = active_record? ? model_class.columns : model_class.fields.map {|c| c[1] }
47
48
  belongs_to_refs = model_class.reflections.values.select{|ref| ref.is_a?(ActiveRecord::Reflection::BelongsToReflection) }
@@ -52,25 +53,88 @@ module ModelBase
52
53
  @title_column = nil
53
54
  ModelBase.config.title_column_candidates.each do |tcc|
54
55
  @title_column = cols.detect{|col| tcc === col.name}
55
- break if @title_column
56
+ if @title_column
57
+ @title_column.title = true
58
+ break
59
+ end
56
60
  end
57
61
  @title_column.linkable = true if @title_column
58
62
  cols
59
63
  end
60
64
 
61
- def excluded?(name)
62
- ModelBase.config.excluded_columns.any?{|c| c === name}
65
+ def raw_columns
66
+ @raw_columns ||= retrieve_columns
63
67
  end
64
68
 
65
- def title_column
66
- retrieve_columns unless defined?(@title_column)
67
- @title_column
69
+ def columns
70
+ @columns ||=
71
+ title_column ? raw_columns : [ColumnAttribute.new(self, :id, :integer, title: true)] + raw_columns
72
+ end
73
+
74
+ SPEC_EXCLUSED_COLS = %w[created_at updated_at]
75
+ def columns_for(type)
76
+ case type
77
+ when :form, :index, :show
78
+ columns.reject{|c| exclude_for?(type, c) }
79
+ when :params then columns_for(:form).reject{|c| c.name == 'id'}
80
+ when :spec_index then columns_for(:index).reject{|c| SPEC_EXCLUSED_COLS.include?(c.name)}
81
+ when :spec_show then columns_for(:show ).reject{|c| SPEC_EXCLUSED_COLS.include?(c.name)}
82
+ when :factory then columns_for(:params).reject{|c| SPEC_EXCLUSED_COLS.include?(c.name)}
83
+ else
84
+ raise "Unknown template type: #{type.inspect}"
85
+ end
68
86
  end
69
87
 
70
- def display_columns
71
- @dispaly_columns ||=
72
- title_column ? columns : [new_attribute(:id, :integer, true)] + columns
88
+ def exclude_for?(type, col_attr)
89
+ excluded_columns = ModelBase.config.send("excluded_columns_of_#{type}")
90
+ excluded_columns.any?{|ex| ex === col_attr.name && !col_attr.title? }
73
91
  end
74
92
 
93
+ def dependencies(required = true)
94
+ # c: c.required?
95
+ # r: required (the argument)
96
+ #
97
+ # | c \ r | true | false |
98
+ # |-------|-------|-------|
99
+ # | true | true | true |
100
+ # | false | false | true |
101
+ raw_columns.select{|c| !required || c.required? }.
102
+ select(&:ref_model).
103
+ each_with_object({}){|c,d| d[c.name] = c.ref_model}
104
+ end
105
+
106
+ def all_dependencies(required = true)
107
+ dependencies.values.map{|m| [m] + m.dependencies(required).values}.flatten.uniq(&:name)
108
+ end
109
+
110
+ def factory_girl_options
111
+ dependencies.map{|attr, model| "#{attr.sub(/_id\z/, '')}: #{model.full_resource_name}"}
112
+ end
113
+
114
+ def factory_girl_create(extra = {})
115
+ factory_girl_method(:create, extra)
116
+ end
117
+
118
+ def factory_girl_build(extra = {})
119
+ factory_girl_method(:build, extra)
120
+ end
121
+
122
+ def factory_girl_method(name, extra)
123
+ extra_str = extra.blank? ? '' : ', ' << extra.map{|k,v| "#{k}: '#{v}'"}.join(', ')
124
+ options = factory_girl_options
125
+ options_str = options.empty? ? '' : ', ' << factory_girl_options.join(', ')
126
+ 'FactoryGirl.%s(:%s%s%s)' % [name, full_resource_name, extra_str, options_str]
127
+ end
128
+
129
+ def factory_girl_let_definition
130
+ 'let(:%s){ %s }' % [full_resource_name, factory_girl_create]
131
+ end
132
+
133
+ def factory_girl_let_definitions(spacer = " ")
134
+ deps = all_dependencies
135
+ r = deps.map(&:factory_girl_let_definition)
136
+ r << "let(:user){ FactoryGirl.create(:user) }" unless deps.any?{|m| m.full_resource_name == 'user' }
137
+ r.join("\n" << spacer)
138
+ end
75
139
  end
76
140
  end
@@ -1,3 +1,3 @@
1
1
  module ModelBase
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/model_base.rb CHANGED
@@ -22,6 +22,10 @@ module ModelBase
22
22
  Rails::Generators.templates_path.unshift(templates_dir)
23
23
  Rails::Generators.lookup(["rails:scaffold_controller"])
24
24
  Rails::Generators::ScaffoldControllerGenerator.source_paths.unshift(templates_dir)
25
+ require 'model_base/generators/erb/scaffold'
26
+ ::ModelBase::Generators::Erb::Scaffold.enable!
27
+ require 'model_base/generators/factory_girl/model'
28
+ ::ModelBase::Generators::FactoryGirl::Model.enable!
25
29
  end
26
30
  end
27
31
  end
@@ -65,7 +65,7 @@ class <%= controller_class_name %>Controller < ApplicationController
65
65
  <%- if model.columns.empty? -%>
66
66
  params[:<%= singular_table_name %>]
67
67
  <%- else -%>
68
- params.require(:<%= singular_table_name %>).permit(<%= model.columns.map { |c| ":#{c.name}" }.join(', ') %>)
68
+ params.require(:<%= singular_table_name %>).permit(<%= model.columns_for(:params).map { |c| ":#{c.name}" }.join(', ') %>)
69
69
  <%- end -%>
70
70
  end
71
71
  end
@@ -15,7 +15,7 @@
15
15
  </div>
16
16
  <%% end %>
17
17
 
18
- <%- model.columns.each do |column| -%>
18
+ <%- model.columns_for(:form).each do |column| -%>
19
19
  <div class="form-group">
20
20
  <%%= f.label :<%= column.name %>, :class => 'control-label col-lg-2' %>
21
21
  <div class="col-lg-10">
@@ -0,0 +1,39 @@
1
+ <%%- model_class = <%= model.name.sub(/\ATemplate::/, '::Template::') %> -%>
2
+ <table class="table table-striped">
3
+ <thead>
4
+ <tr>
5
+ <%- model.columns_for(:index).each do |column| -%>
6
+ <th><%%= model_class.human_attribute_name(:<%= column.name %>) %></th>
7
+ <%- end -%>
8
+ <th><%%=t '.actions', :default => t("helpers.actions") %></th>
9
+ </tr>
10
+ </thead>
11
+ <tbody>
12
+ <%% @<%= model.plural_full_resource_name %>.each do |<%= model.full_resource_name %>| %>
13
+ <tr>
14
+ <%- model.columns_for(:index).each do |column| -%>
15
+ <%- if column.linkable? -%>
16
+ <td><%%= link_to <%= model.full_resource_name %>.<%= column.name %>, <%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>) %></td>
17
+ <%- elsif tcol = column.ref_model.try(:title_column) -%>
18
+ <td><%%= <%= model.full_resource_name %>.<%= column.reference.name %>.<%= tcol.name %> %></td>
19
+ <%- elsif column.enumerized? -%>
20
+ <td><%%= <%= model.full_resource_name %>.<%= column.name %>_text %></td>
21
+ <%- elsif column.to_be_localized? -%>
22
+ <td><%%=l <%= model.full_resource_name %>.<%= column.name %> %></td>
23
+ <%- else -%>
24
+ <td><%%= <%= model.full_resource_name %>.<%= column.name %> %></td>
25
+ <%- end -%>
26
+ <%- end -%>
27
+ <td>
28
+ <%%= link_to t('.edit', :default => t("helpers.links.edit")),
29
+ edit_<%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>), :class => 'btn btn-default btn-xs' %>
30
+ <%%= link_to t('.destroy', :default => t("helpers.links.destroy")),
31
+ <%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>),
32
+ :method => :delete,
33
+ :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
34
+ :class => 'btn btn-xs btn-danger' %>
35
+ </td>
36
+ </tr>
37
+ <%% end %>
38
+ </tbody>
39
+ </table>
@@ -2,44 +2,7 @@
2
2
  <div class="page-header">
3
3
  <h1><%%=t '.title', :default => model_class.model_name.human.pluralize.titleize %></h1>
4
4
  </div>
5
- <table class="table table-striped">
6
- <thead>
7
- <tr>
8
- <%- model.display_columns.each do |column| -%>
9
- <th><%%= model_class.human_attribute_name(:<%= column.name %>) %></th>
10
- <%- end -%>
11
- <th><%%= model_class.human_attribute_name(:created_at) %></th>
12
- <th><%%=t '.actions', :default => t("helpers.actions") %></th>
13
- </tr>
14
- </thead>
15
- <tbody>
16
- <%% @<%= model.plural_full_resource_name %>.each do |<%= model.full_resource_name %>| %>
17
- <tr>
18
- <%- model.columns.each do |column| -%>
19
- <%- if column.linkable? -%>
20
- <td><%%= link_to <%= model.full_resource_name %>.<%= column.name %>, <%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>) %></td>
21
- <%- elsif tcol = column.ref_model.try(:title_column) -%>
22
- <td><%%= <%= model.full_resource_name %>.<%= column.reference.name %>.<%= tcol.name %> %></td>
23
- <%- elsif column.enumerized? -%>
24
- <td><%%= <%= model.full_resource_name %>.<%= column.name %>_text %></td>
25
- <%- else -%>
26
- <td><%%= <%= model.full_resource_name %>.<%= column.name %> %></td>
27
- <%- end -%>
28
- <%- end -%>
29
- <td><%%=l <%= model.full_resource_name %>.created_at %></td>
30
- <td>
31
- <%%= link_to t('.edit', :default => t("helpers.links.edit")),
32
- edit_<%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>), :class => 'btn btn-default btn-xs' %>
33
- <%%= link_to t('.destroy', :default => t("helpers.links.destroy")),
34
- <%= singular_controller_routing_path %>_path(<%= model.full_resource_name %>),
35
- :method => :delete,
36
- :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
37
- :class => 'btn btn-xs btn-danger' %>
38
- </td>
39
- </tr>
40
- <%% end %>
41
- </tbody>
42
- </table>
5
+ <%%= render '<%= plural_table_name %>/table' %>
43
6
 
44
7
  <%%= link_to t('.new', :default => t("helpers.links.new")),
45
8
  new_<%= singular_controller_routing_path %>_path,
@@ -4,12 +4,14 @@
4
4
  </div>
5
5
 
6
6
  <dl class="dl-horizontal">
7
- <%- model.columns.each do |column| -%>
7
+ <%- model.columns_for(:show).each do |column| -%>
8
8
  <dt><strong><%%= model_class.human_attribute_name(:<%= column.name %>) %>:</strong></dt>
9
9
  <%- if tcol = column.ref_model.try(:title_column) -%>
10
10
  <dd><%%= @<%= model.full_resource_name %>.<%= column.reference.name %>.<%= tcol.name %> %></dd>
11
11
  <%- elsif column.enumerized? -%>
12
12
  <dd><%%= @<%= model.full_resource_name %>.<%= column.name %>_text %></dd>
13
+ <%- elsif column.to_be_localized? -%>
14
+ <dd><%%=l @<%= model.full_resource_name %>.<%= column.name %> %></dd>
13
15
  <%- else -%>
14
16
  <dd><%%= @<%= model.full_resource_name %>.<%= column.name %> %></dd>
15
17
  <%- end -%>
@@ -0,0 +1,11 @@
1
+ FactoryGirl.define do
2
+ factory :<%= model.full_resource_name %> do
3
+ <%- model.columns_for(:factory).each do |col| -%>
4
+ <%- if col.ref_model -%>
5
+ association :<%= col.name.sub(/_id\z/, '') %>, factory: :<%= col.ref_model.full_resource_name %>
6
+ <%- else -%>
7
+ <%= col.name.sub(/_id\z/, '') %> <%= col.sample_value(context: :factory).inspect %>
8
+ <%- end -%>
9
+ <%- end -%>
10
+ end
11
+ end
@@ -27,21 +27,16 @@ require 'rails_helper'
27
27
  RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:controller) %> do
28
28
 
29
29
  <%-
30
- required_ref_attrs = model.columns.select{|attr| attr.reference && attr.required? }
31
- required_data_attrs = model.columns.select{|attr| !attr.reference && attr.required? }
30
+ required_ref_attrs = model.columns_for(:params).select{|attr| attr.reference && attr.required? }
31
+ required_data_attrs = model.columns_for(:params).select{|attr| !attr.reference && attr.required? }
32
32
  -%>
33
- <%- required_ref_attrs.each do |attr| -%>
34
- let(:<%= attr.reference.name %>){ FactoryGirl.create(:<%= attr.ref_model.full_resource_name %>) }
35
- <%- end -%>
36
- <%- unless required_ref_attrs.any?{|attr| attr.ref_model.name == 'User' }-%>
37
- let(:user){ FactoryGirl.create(:user) }
38
- <%- end -%>
33
+ <%= model.factory_girl_let_definitions %>
39
34
  before{ devise_user_login(user) }
40
35
 
41
36
  <%-
42
37
  unless required_ref_attrs.empty?
43
- extra_attributes_to_merge = ".merge(%s)" % required_ref_attrs.map{|attr| "#{attr.name}: #{attr.reference.name}.id"}.join(', ')
44
- extra_attributes_for_factory = ", %s" % required_ref_attrs.map{|attr| "#{attr.reference.name}: #{attr.reference.name}"}.join(', ')
38
+ extra_attributes_to_merge = ".merge(%s)" % required_ref_attrs.map{|attr| "#{attr.name}: #{attr.ref_model.full_resource_name}.id"}.join(', ')
39
+ extra_attributes_for_factory = ", %s" % required_ref_attrs.map{|attr| "#{attr.reference.name}: #{attr.ref_model.full_resource_name}"}.join(', ')
45
40
  end
46
41
  -%>
47
42
  let(:<%= file_name %>){ FactoryGirl.create(:<%= file_name %><%= extra_attributes_for_factory %>) }
@@ -137,7 +132,7 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
137
132
  context "with valid params" do
138
133
  <%- if !required_data_attrs.empty? -%>
139
134
  <%- required_data_attrs.each do |required_data_attr| -%>
140
- let(:new_<%= required_data_attr.name %>){ valid_parameters[:<%= required_data_attr.name %>].succ }
135
+ let(:new_<%= required_data_attr.name %>){ <%= required_data_attr.new_attribute_exp %> }
141
136
  <%- end -%>
142
137
  <%- elsif !required_ref_attrs.empty? -%>
143
138
  let(:another_<%= required_ref_attrs.last.name %>){ FactoryGirl.create(:<%= required_ref_attrs.last.name %><%= extra_attributes_for_factory %>) }
@@ -170,13 +165,13 @@ RSpec.describe <%= controller_class_name %>Controller, <%= type_metatag(:control
170
165
 
171
166
  it "assigns the requested <%= file_name %> as @<%= file_name %>" do
172
167
  <%= file_name %> # To create <%= file_name %>
173
- put :update, params: {:id => <%= file_name %>.to_param, :<%= file_name %> => valid_parameters}, session: valid_session
168
+ put :update, params: {:id => <%= file_name %>.to_param, :<%= file_name %> => new_parameters}, session: valid_session
174
169
  expect(assigns(:<%= file_name %>)).to eq(<%= file_name %>)
175
170
  end
176
171
 
177
172
  it "redirects to the <%= file_name %>" do
178
173
  <%= file_name %> # To create <%= file_name %>
179
- put :update, params: {:id => <%= file_name %>.to_param, :<%= file_name %> => valid_parameters}, session: valid_session
174
+ put :update, params: {:id => <%= file_name %>.to_param, :<%= file_name %> => new_parameters}, session: valid_session
180
175
  expect(response).to redirect_to(<%= file_name %>)
181
176
  end
182
177
  end
@@ -1,16 +1,16 @@
1
1
  require 'rails_helper'
2
2
 
3
- <% output_attributes = model.columns.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
3
  RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
4
+ <%= model.factory_girl_let_definitions %>
5
5
  before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, FactoryGirl.create(:<%= ns_file_name %>))
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= model.factory_girl_create %>)
7
7
  end
8
8
 
9
9
  it "renders the edit <%= ns_file_name %> form" do
10
10
  render
11
11
 
12
12
  assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= ns_file_name %>), "post" do
13
- <% for attribute in output_attributes -%>
13
+ <% model.columns_for(:form).each do |attribute| -%>
14
14
  <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name -%>
15
15
  assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
16
16
  <% end -%>
@@ -2,18 +2,28 @@ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = model.columns.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
4
  RSpec.describe "<%= ns_table_name %>/index", <%= type_metatag(:view) %> do
5
+ <%= model.factory_girl_let_definitions %>
5
6
  before(:each) do
6
7
  assign(:<%= table_name %>, [
7
8
  <% [1,2].each_with_index do |id, model_index| -%>
8
- FactoryGirl.create(:<%= ns_file_name %>),
9
+ <%- if tc = model.title_column -%>
10
+ <%= model.factory_girl_create(tc.name.to_sym => tc.sample_value(model_index + 1)) %>,
11
+ <%- else -%>
12
+ <%= model.factory_girl_create %>
13
+ <%- end -%>
9
14
  <% end -%>
10
15
  ])
11
16
  end
12
17
 
13
18
  it "renders a list of <%= ns_table_name %>" do
14
19
  render
15
- <% for attribute in output_attributes -%>
16
- assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
20
+ <% model.columns_for(:spec_index).each do |attribute| -%>
21
+ <%- if attribute.ref_model || attribute.enumerized? -%>
22
+ assert_select "tr>td", :text => <%= attribute.sample_string %>, :count => 2
23
+ <%- else -%>
24
+ assert_select "tr>td", :text => <%= attribute.sample_string(1) %>, :count => 1
25
+ assert_select "tr>td", :text => <%= attribute.sample_string(2) %>, :count => 1
26
+ <%- end -%>
17
27
  <% end -%>
18
28
  end
19
29
  end
@@ -2,15 +2,16 @@ require 'rails_helper'
2
2
 
3
3
  <% output_attributes = model.columns.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
4
  RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
5
+ <%= model.factory_girl_let_definitions %>
5
6
  before(:each) do
6
- assign(:<%= ns_file_name %>, FactoryGirl.build(:<%= ns_file_name %>))
7
+ assign(:<%= ns_file_name %>, <%= model.factory_girl_build %>)
7
8
  end
8
9
 
9
10
  it "renders new <%= ns_file_name %> form" do
10
11
  render
11
12
 
12
13
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
13
- <% for attribute in output_attributes -%>
14
+ <% model.columns_for(:form).each do |attribute| -%>
14
15
  <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name -%>
15
16
  assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
16
17
  <% end -%>
@@ -1,15 +1,15 @@
1
1
  require 'rails_helper'
2
2
 
3
- <% output_attributes = model.columns.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
3
  RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
4
+ <%= model.factory_girl_let_definitions %>
5
5
  before(:each) do
6
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, FactoryGirl.create(:<%= ns_file_name %>))
6
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= model.factory_girl_create %>)
7
7
  end
8
8
 
9
9
  it "renders attributes in <p>" do
10
10
  render
11
- <% for attribute in output_attributes -%>
12
- expect(rendered).to match(/<%= raw_value_for(attribute) %>/)
11
+ <% model.columns_for(:spec_show).each do |attribute| -%>
12
+ expect(rendered).to match(/<%= attribute.sample_value %>/)
13
13
  <% end -%>
14
14
  end
15
15
  end