Skip to content

Commit 167d5a3

Browse files
committed
cleaned up dependencies between i18n hyper-model and errors class
1 parent bb7bf38 commit 167d5a3

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

ruby/hyper-i18n/spec/hyper_i18n_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class TestComponent
2626
mount 'Components::TestComponent', {}, render_on: flag
2727
expect(find('#tp1')).to have_content('I am a key')
2828
expect(find('#tp2')).to have_content('Hello world')
29-
expect(find('#tp3')).to have_content('Mon, 01 Jan 2018 12:45:00')
30-
expect(find('#tp4')).to have_content('January 01, 2018 at 12:45 pm')
29+
expect(find('#tp3')).to have_content(::I18n.l(Time.parse('1/1/2018 12:45')))
30+
expect(find('#tp4')).to have_content(::I18n.l(Time.parse('1/1/2018 12:45'), format: '%B %d, %Y at %l:%M %P'))
3131
expect(find('#tp5')).to have_content('My Model')
3232
expect(find('#tp6')).to have_content('The Attribute')
3333
end

ruby/hyper-model/lib/reactive_record/active_record/class_methods.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ def alias_attribute(new_name, old_name)
185185
]
186186

187187
def method_missing(name, *args, &block)
188-
if args.count == 1 && name.start_with?("find_by_") && !block
188+
if name == 'human_attribute_name'
189+
opts = args[1] || {}
190+
opts[:default] || args[0]
191+
elsif args.count == 1 && name.start_with?("find_by_") && !block
189192
find_by(name.sub(/^find_by_/, '') => args[0])
190193
elsif [].respond_to?(name)
191194
all.send(name, *args, &block)
@@ -336,10 +339,6 @@ def server_method(name, default: nil)
336339
end
337340
end
338341

339-
def human_attribute_name(attribute, opts = {})
340-
opts[:default] || attribute
341-
end
342-
343342
def define_attribute_methods
344343
columns_hash.each do |name, column_hash|
345344
next if name == primary_key

ruby/hyper-model/lib/reactive_record/active_record/errors.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def full_message(attribute, message)
105105
attr_name =
106106
attribute.to_s.tr('.', '_').tr('_', ' ').gsub(/_id$/, '').capitalize
107107
# attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
108-
# if @base.class.respond_to?(:human_attribute_name)
108+
if @base.class.respond_to?(:human_attribute_name)
109109
attr_name = @base.class.human_attribute_name(attribute, default: attr_name)
110-
# end
110+
end
111111
# I18n.t(:"errors.format",
112112
# default: "%{attribute} %{message}",
113113
# attribute: attr_name,

ruby/hyper-model/spec/batch1/misc/errors_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def validate!
2222
# send(attr)
2323
# end
2424

25-
class << self
26-
def human_attribute_name(attr, options = {})
27-
attr
28-
end
29-
end
25+
# class << self
26+
# def human_attribute_name(attr, options = {})
27+
# attr
28+
# end
29+
# end
3030

3131
# def self.lookup_ancestors
3232
# [self]
@@ -174,7 +174,7 @@ def human_attribute_name(attr, options = {})
174174
person = Person.new
175175
person.validate!
176176
person.errors.full_messages
177-
end.to eq ['name cannot be nil']
177+
end.to eq ['Name cannot be nil']
178178

179179
expect_evaluate_ruby do
180180
person = Person.new
@@ -312,7 +312,7 @@ def human_attribute_name(attr, options = {})
312312
person.errors.add(:name, 'cannot be blank')
313313
person.errors.add(:name, 'cannot be nil')
314314
person.errors.to_a
315-
end.to eq ['name cannot be blank', 'name cannot be nil']
315+
end.to eq ['Name cannot be blank', 'Name cannot be nil']
316316
end
317317

318318
it 'to_hash returns the error messages hash' do
@@ -343,7 +343,7 @@ def human_attribute_name(attr, options = {})
343343
person.errors.add(:name, 'cannot be blank')
344344
person.errors.add(:name, 'cannot be nil')
345345
person.errors.full_messages
346-
end.to eq ['name cannot be blank', 'name cannot be nil']
346+
end.to eq ['Name cannot be blank', 'Name cannot be nil']
347347
end
348348

349349
it 'full_messages_for contains all the error messages for the given attribute indifferent' do
@@ -352,7 +352,7 @@ def human_attribute_name(attr, options = {})
352352
person.errors.add(:name, 'cannot be blank')
353353
person.errors.add(:name, 'cannot be nil')
354354
person.errors.full_messages_for(:name)
355-
end.to eq ['name cannot be blank', 'name cannot be nil']
355+
end.to eq ['Name cannot be blank', 'Name cannot be nil']
356356
end
357357

358358
it 'full_messages_for does not contain error messages from other attributes' do
@@ -361,7 +361,7 @@ def human_attribute_name(attr, options = {})
361361
person.errors.add(:name, 'cannot be blank')
362362
person.errors.add(:email, 'cannot be blank')
363363
person.errors.full_messages_for(:name)
364-
end.to eq ['name cannot be blank']
364+
end.to eq ['Name cannot be blank']
365365
end
366366

367367
it 'full_messages_for returns an empty list in case there are no errors for the given attribute' do
@@ -383,7 +383,7 @@ def human_attribute_name(attr, options = {})
383383
expect_evaluate_ruby do
384384
person = Person.new
385385
person.errors.full_message(:name_test, 'cannot be blank')
386-
end.to eq 'name_test cannot be blank'
386+
end.to eq 'Name test cannot be blank'
387387
end
388388

389389
it 'as_json creates a json formatted representation of the errors hash' do
@@ -399,7 +399,7 @@ def human_attribute_name(attr, options = {})
399399
person = Person.new
400400
person.validate!
401401
person.errors.as_json(full_messages: true)
402-
end.to eq('name' => ['name cannot be nil'])
402+
end.to eq('name' => ['Name cannot be nil'])
403403
end
404404

405405
it 'generate_message works without i18n_scope', skip: true do

0 commit comments

Comments
 (0)