model_base_generators 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 377e6ba19f399cf524fa5c25a8fa56c983724462
4
- data.tar.gz: c7a89884e9ab54a5e4edacf078aa25f2ad9a28eb
3
+ metadata.gz: 68314ef8b406587412d78bdddaf77afac98fafd5
4
+ data.tar.gz: 4989519ef484404842ade4d6322048c4205fbe1f
5
5
  SHA512:
6
- metadata.gz: 7c67f7275d7db7d492b66c2b825909f8bc3933088a4b66caf16f7af5491fd63a33484c19bfae33fbd0c1e1a5151848d2ca52dabf3bb8a25b82bc4da2805d848c
7
- data.tar.gz: e3fcc7532cc2ea27e36b7c49b2f1ac9fa828b878d634e70e9739f1d2124caa7ed62b11cbec3c9f96bf5d345c8cf23702f0e85782f5f2a48ad3ec137527d1ea4e
6
+ metadata.gz: bb3fb2d839116e34fd7fad805a2f8e988c1b7f0f4db1f909c61fb9fe41cf3c6fffb41f13e085ab56a6f09af3657a1df31329d12ed9a55acf6612ff45fb9b35a9
7
+ data.tar.gz: 4c3462e95b70e2ba2dd8479e044a0bbe2da42323336ba2511e31cb5ae67beee6d50c6c90d4ccfddc642ad9d4a272eeb61c5b0530f271ecdf144d9058ccb76cf3
@@ -57,6 +57,6 @@ class ProjectAssignmentsController < ApplicationController
57
57
 
58
58
  # Only allow a trusted parameter "white list" through.
59
59
  def project_assignment_params
60
- params.require(:project_assignment).permit(:project_id, :user_id)
60
+ params.require(:project_assignment).permit(:project_id, :user_id, :started_at, :finished_at)
61
61
  end
62
62
  end
@@ -36,6 +36,20 @@
36
36
  </div>
37
37
  <%=f.error_span(:user_id) %>
38
38
  </div>
39
+ <div class="form-group">
40
+ <%= f.label :started_at, :class => 'control-label col-lg-2' %>
41
+ <div class="col-lg-10">
42
+ <%= f.datetime_select :started_at, :class => 'form-control' %>
43
+ </div>
44
+ <%=f.error_span(:started_at) %>
45
+ </div>
46
+ <div class="form-group">
47
+ <%= f.label :finished_at, :class => 'control-label col-lg-2' %>
48
+ <div class="col-lg-10">
49
+ <%= f.datetime_select :finished_at, :class => 'form-control' %>
50
+ </div>
51
+ <%=f.error_span(:finished_at) %>
52
+ </div>
39
53
 
40
54
  <div class="form-group">
41
55
  <div class="col-lg-offset-2 col-lg-10">
@@ -5,6 +5,8 @@
5
5
  <th><%= model_class.human_attribute_name(:id) %></th>
6
6
  <th><%= model_class.human_attribute_name(:project_id) %></th>
7
7
  <th><%= model_class.human_attribute_name(:user_id) %></th>
8
+ <th><%= model_class.human_attribute_name(:started_at) %></th>
9
+ <th><%= model_class.human_attribute_name(:finished_at) %></th>
8
10
  <th><%= model_class.human_attribute_name(:created_at) %></th>
9
11
  <th><%=t '.actions', :default => t("helpers.actions") %></th>
10
12
  </tr>
@@ -15,6 +17,8 @@
15
17
  <td><%= project_assignment.id %></td>
16
18
  <td><%= project_assignment.project.name %></td>
17
19
  <td><%= project_assignment.user.email %></td>
20
+ <td><%=l project_assignment.started_at %></td>
21
+ <td><%=l project_assignment.finished_at %></td>
18
22
  <td><%=l project_assignment.created_at %></td>
19
23
  <td>
20
24
  <%= link_to t('.edit', :default => t("helpers.links.edit")),
@@ -10,6 +10,10 @@
10
10
  <dd><%= @project_assignment.project.name %></dd>
11
11
  <dt><strong><%= model_class.human_attribute_name(:user_id) %>:</strong></dt>
12
12
  <dd><%= @project_assignment.user.email %></dd>
13
+ <dt><strong><%= model_class.human_attribute_name(:started_at) %>:</strong></dt>
14
+ <dd><%=l @project_assignment.started_at %></dd>
15
+ <dt><strong><%= model_class.human_attribute_name(:finished_at) %>:</strong></dt>
16
+ <dd><%=l @project_assignment.finished_at %></dd>
13
17
  <dt><strong><%= model_class.human_attribute_name(:created_at) %>:</strong></dt>
14
18
  <dd><%=l @project_assignment.created_at %></dd>
15
19
  </dl>
data/example/db/schema.rb CHANGED
@@ -26,6 +26,8 @@ ActiveRecord::Schema.define(version: 20161013025452) do
26
26
  create_table :project_assignments do |t|
27
27
  t.references :project, null: false, foreign_key: true
28
28
  t.references :user , null: false, foreign_key: true
29
+ t.datetime :started_at
30
+ t.datetime :finished_at
29
31
  t.datetime :created_at, null: false
30
32
  end
31
33
 
@@ -2,5 +2,7 @@ FactoryGirl.define do
2
2
  factory :project_assignment do
3
3
  association :project, factory: :project
4
4
  association :user, factory: :user
5
+ started_at "2020-03-22 09:50:00"
6
+ finished_at "2020-03-22 23:40:00"
5
7
  end
6
8
  end
@@ -0,0 +1,5 @@
1
+ def assert_select_datetime_field(object, field, n = 5)
2
+ (1..n).each do |i|
3
+ assert_select "select##{object}_#{field}_#{i}i[name=?]", "#{object}[#{field}(#{i}i)]"
4
+ end
5
+ end
@@ -14,6 +14,8 @@ RSpec.describe "project_assignments/edit", type: :view do
14
14
  assert_select "input#project_assignment_id[name=?]", "project_assignment[id]"
15
15
  assert_select "select#project_assignment_project_id[name=?]", "project_assignment[project_id]"
16
16
  assert_select "select#project_assignment_user_id[name=?]", "project_assignment[user_id]"
17
+ assert_select_datetime_field :project_assignment, :started_at
18
+ assert_select_datetime_field :project_assignment, :finished_at
17
19
  end
18
20
  end
19
21
  end
@@ -14,5 +14,7 @@ RSpec.describe "project_assignments/index", type: :view do
14
14
  render
15
15
  assert_select "tr>td", :text => 'project1', :count => 2
16
16
  assert_select "tr>td", :text => 'user1@example.com', :count => 2
17
+ assert_select "tr>td", :text => localize(Time.zone.parse('2020-03-22 09:50:00')), :count => 2
18
+ assert_select "tr>td", :text => localize(Time.zone.parse('2020-03-22 23:40:00')), :count => 2
17
19
  end
18
20
  end
@@ -14,6 +14,8 @@ RSpec.describe "project_assignments/new", type: :view do
14
14
  assert_select "input#project_assignment_id[name=?]", "project_assignment[id]"
15
15
  assert_select "select#project_assignment_project_id[name=?]", "project_assignment[project_id]"
16
16
  assert_select "select#project_assignment_user_id[name=?]", "project_assignment[user_id]"
17
+ assert_select_datetime_field :project_assignment, :started_at
18
+ assert_select_datetime_field :project_assignment, :finished_at
17
19
  end
18
20
  end
19
21
  end
@@ -11,5 +11,7 @@ RSpec.describe "project_assignments/show", type: :view do
11
11
  render
12
12
  expect(rendered).to match(/project1/)
13
13
  expect(rendered).to match(/user1@example.com/)
14
+ expect(rendered).to match(Regexp.new(Regexp.escape(localize(Time.zone.parse('2020-03-22 09:50:00')))))
15
+ expect(rendered).to match(Regexp.new(Regexp.escape(localize(Time.zone.parse('2020-03-22 23:40:00')))))
14
16
  end
15
17
  end
@@ -31,7 +31,7 @@ module ModelBase
31
31
  end
32
32
 
33
33
  def required?
34
- !column.try(:null)
34
+ column ? !column.null : false
35
35
  end
36
36
 
37
37
  def enumerized?
@@ -53,6 +53,14 @@ module ModelBase
53
53
  LOCALIZED_TYPES.include?(type)
54
54
  end
55
55
 
56
+ def single_sample_only?
57
+ ref_model || enumerized? ||
58
+ case type
59
+ when :boolean, :datetime, :timestamp, :time, :date then true
60
+ else false
61
+ end
62
+ end
63
+
56
64
  def sample_value(idx = 1, context: nil)
57
65
  if name == 'id'
58
66
  idx
@@ -71,12 +79,12 @@ module ModelBase
71
79
  r = enum.values.first
72
80
  context == :factory ? r.to_sym : r.text
73
81
  else
74
- @default ||= case type
82
+ case type
75
83
  when :integer then idx
76
84
  when :float then idx + 0.5
77
85
  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)
86
+ when :datetime, :timestamp, :time then sample_time(idx).to_s(:db)
87
+ when :date then sample_time(idx).to_date.to_s(:db)
80
88
  when :string then
81
89
  case name
82
90
  when 'type' then ""
@@ -91,8 +99,42 @@ module ModelBase
91
99
  end
92
100
  end
93
101
 
94
- def sample_string(idx = 1)
95
- "'%s'" % sample_value(idx)
102
+ def base_sample_value
103
+ name.split('').map(&:ord).sum
104
+ end
105
+
106
+ def sample_time(idx = 1)
107
+ ModelBase.base_time +
108
+ model.sample_value.hours +
109
+ (base_sample_value.minutes * 10 * idx)
110
+ end
111
+
112
+ def sample_value_regexp_exp(idx = 1)
113
+ case type
114
+ when :datetime, :timestamp, :time
115
+ 'Regexp.new(Regexp.escape(%s))' % sample_string_exp(idx)
116
+ else
117
+ '/%s/' % sample_value(idx)
118
+ end
119
+ end
120
+
121
+ def assert_select_exp
122
+ model_name = model.full_resource_name
123
+ case type
124
+ when :datetime, :timestamp, :time
125
+ "assert_select_datetime_field :#{model_name}, :#{name}"
126
+ else
127
+ "assert_select \"#{ input_type }##{ model_name }_#{ name }[name=?]\", \"#{ model_name }[#{ name }]\""
128
+ end
129
+ end
130
+
131
+ def sample_string_exp(idx = 1)
132
+ case type
133
+ when :datetime, :timestamp, :time
134
+ 'localize(Time.zone.parse(\'%s\'))' % sample_value(idx)
135
+ else
136
+ "'%s'" % sample_value(idx)
137
+ end
96
138
  end
97
139
 
98
140
  def new_attribute_exp
@@ -28,5 +28,9 @@ module ModelBase
28
28
  ['name', 'title', 'email', 'display_name', 'display_title', /_name\z/, /_title\z/]
29
29
  end
30
30
 
31
+ config_accessor(:base_time) do
32
+ '2020-01-01 00:00:00'
33
+ end
34
+
31
35
  end
32
36
  end
@@ -48,7 +48,7 @@ module ModelBase
48
48
  belongs_to_refs = model_class.reflections.values.select{|ref| ref.is_a?(ActiveRecord::Reflection::BelongsToReflection) }
49
49
  cols = raw_cols.map do |col|
50
50
  ref = belongs_to_refs.detect{|ref| ref.foreign_key == col.name}
51
- ColumnAttribute.new(self, col.name, col.type, reference: ref)
51
+ ColumnAttribute.new(self, col.name, col.type, column: col, reference: ref)
52
52
  end
53
53
  @title_column = nil
54
54
  ModelBase.config.title_column_candidates.each do |tcc|
@@ -66,6 +66,10 @@ module ModelBase
66
66
  @raw_columns ||= retrieve_columns
67
67
  end
68
68
 
69
+ def [](name)
70
+ raw_columns.detect{|c| c.name == name.to_s}
71
+ end
72
+
69
73
  def columns
70
74
  @columns ||=
71
75
  title_column ? raw_columns : [ColumnAttribute.new(self, 'id', :integer, title: true)] + raw_columns
@@ -136,5 +140,9 @@ module ModelBase
136
140
  r << "let(:user){ FactoryGirl.create(:user) }" unless deps.any?{|m| m.full_resource_name == 'user' }
137
141
  r.join("\n" << spacer)
138
142
  end
143
+
144
+ def sample_value
145
+ @sample_value ||= name.split('').map(&:ord).sum
146
+ end
139
147
  end
140
148
  end
@@ -1,3 +1,3 @@
1
1
  module ModelBase
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
data/lib/model_base.rb CHANGED
@@ -27,6 +27,10 @@ module ModelBase
27
27
  require 'model_base/generators/factory_girl/model'
28
28
  ::ModelBase::Generators::FactoryGirl::Model.enable!
29
29
  end
30
+
31
+ def base_time
32
+ Time.zone.parse(config.base_time)
33
+ end
30
34
  end
31
35
  end
32
36
  require 'model_base/railtie' if defined?(Rails)
@@ -11,8 +11,7 @@ RSpec.describe "<%= ns_table_name %>/edit", <%= type_metatag(:view) %> do
11
11
 
12
12
  assert_select "form[action=?][method=?]", <%= ns_file_name %>_path(@<%= ns_file_name %>), "post" do
13
13
  <% model.columns_for(:form).each do |attribute| -%>
14
- <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name -%>
15
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
14
+ <%= attribute.assert_select_exp %>
16
15
  <% end -%>
17
16
  end
18
17
  end
@@ -18,11 +18,11 @@ RSpec.describe "<%= ns_table_name %>/index", <%= type_metatag(:view) %> do
18
18
  it "renders a list of <%= ns_table_name %>" do
19
19
  render
20
20
  <% model.columns_for(:spec_index).each do |attribute| -%>
21
- <%- if attribute.ref_model || attribute.enumerized? || attribute.type == :boolean -%>
22
- assert_select "tr>td", :text => <%= attribute.sample_string %>, :count => 2
21
+ <%- if attribute.single_sample_only? -%>
22
+ assert_select "tr>td", :text => <%= attribute.sample_string_exp %>, :count => 2
23
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
24
+ assert_select "tr>td", :text => <%= attribute.sample_string_exp(1) %>, :count => 1
25
+ assert_select "tr>td", :text => <%= attribute.sample_string_exp(2) %>, :count => 1
26
26
  <%- end -%>
27
27
  <% end -%>
28
28
  end
@@ -12,8 +12,7 @@ RSpec.describe "<%= ns_table_name %>/new", <%= type_metatag(:view) %> do
12
12
 
13
13
  assert_select "form[action=?][method=?]", <%= index_helper %>_path, "post" do
14
14
  <% model.columns_for(:form).each do |attribute| -%>
15
- <%- name = attribute.respond_to?(:column_name) ? attribute.column_name : attribute.name -%>
16
- assert_select "<%= attribute.input_type -%>#<%= ns_file_name %>_<%= name %>[name=?]", "<%= ns_file_name %>[<%= name %>]"
15
+ <%= attribute.assert_select_exp %>
17
16
  <% end -%>
18
17
  end
19
18
  end
@@ -9,7 +9,7 @@ RSpec.describe "<%= ns_table_name %>/show", <%= type_metatag(:view) %> do
9
9
  it "renders attributes in <p>" do
10
10
  render
11
11
  <% model.columns_for(:spec_show).each do |attribute| -%>
12
- expect(rendered).to match(/<%= attribute.sample_value %>/)
12
+ expect(rendered).to match(<%= attribute.sample_value_regexp_exp %>)
13
13
  <% end -%>
14
14
  end
15
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_base_generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - akm
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-22 00:00:00.000000000 Z
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -237,6 +237,7 @@ files:
237
237
  - example/spec/spec_helper.rb
238
238
  - example/spec/support/controller_macros.rb
239
239
  - example/spec/support/devise.rb
240
+ - example/spec/support/field_assertions.rb
240
241
  - example/spec/views/issues/edit.html.erb_spec.rb
241
242
  - example/spec/views/issues/index.html.erb_spec.rb
242
243
  - example/spec/views/issues/new.html.erb_spec.rb