Skip to content

Commit bb7bf38

Browse files
committed
misc fixes
1 parent 7253146 commit bb7bf38

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def find_inverse(model) # private
163163
the_klass.reflect_on_all_associations.each do |association|
164164
next if association.association_foreign_key != @association_foreign_key
165165
next if association.attribute == attribute
166-
return association if the_klass == association.owner_class
166+
return association if association.polymorphic? || association.klass == owner_class
167167
end
168168
raise "could not find inverse of polymorphic belongs_to: #{model.inspect} #{self.inspect}" if options[:polymorphic]
169169
# instead of raising an error go ahead and create the inverse relationship if it does not exist.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def alias_attribute(new_name, old_name)
172172
:full_table_name_prefix, :full_table_name_suffix, :reset_sequence_name, :sequence_name=, :next_sequence_value, :column_defaults, :content_columns,
173173
:readonly_attributes, :attr_readonly, :create, :create!, :instantiate, :find, :type_caster, :arel_table, :find_by, :find_by!, :initialize_find_by_cache,
174174
:generated_association_methods, :arel_engine, :arel_attribute, :predicate_builder, :collection_cache_key, :relation_delegate_class,
175-
:initialize_relation_delegate_cache, :enum, :collecting_queries_for_explain, :exec_explain, :i18n_scope, :lookup_ancestors, :human_attribute_name,
175+
:initialize_relation_delegate_cache, :enum, :collecting_queries_for_explain, :exec_explain, :i18n_scope, :lookup_ancestors,
176176
:references, :uniq, :maximum, :none, :exists?, :second, :limit, :order, :eager_load, :update, :delete_all, :destroy, :ids, :many?, :pluck, :third,
177177
:delete, :fourth, :fifth, :forty_two, :second_to_last, :third_to_last, :preload, :sum, :take!, :first!, :last!, :second!, :offset, :select, :fourth!,
178178
:third!, :third_to_last!, :fifth!, :where, :first_or_create, :second_to_last!, :forty_two!, :first, :having, :any?, :one?, :none?, :find_or_create_by,
@@ -336,6 +336,10 @@ def server_method(name, default: nil)
336336
end
337337
end
338338

339+
def human_attribute_name(attribute, opts = {})
340+
opts[:default] || attribute
341+
end
342+
339343
def define_attribute_methods
340344
columns_hash.each do |name, column_hash|
341345
next if name == primary_key

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def changes
207207
end
208208

209209
def errors
210-
@errors ||= ActiveModel::Errors.new(self)
210+
@errors ||= ActiveModel::Errors.new(ar_instance)
211211
end
212212

213213
# called when we have a newly created record, to initialize

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,9 @@ def update_child(item)
435435
inverse_of = @association.inverse_of
436436
current_association_value = item.attributes[inverse_of]
437437
backing_record.virgin = false unless backing_record.data_loading?
438-
#backing_record.update_belongs_to(inverse_of, @owner)
439-
backing_record.set_belongs_to_via_has_many(@association, @owner)
438+
# next line was commented out and following line was active.
439+
backing_record.update_belongs_to(inverse_of, @owner)
440+
#backing_record.set_belongs_to_via_has_many(@association, @owner)
440441
# following is handled by update_belongs_to and is redundant
441442
# unless current_association_value.nil? # might be a dummy value which responds to nil
442443
# current_association = @association.inverse.inverse(current_association_value)
@@ -542,8 +543,8 @@ def internal_replace(new_array)
542543
r.id = new_array[i].id if array[i] and array[i].id and !r.new? and r.backing_record.vector.last =~ /^\*[0-9]+$/
543544
end
544545
end
545-
546-
@collection.dup.each { |item| delete(item) } if @collection # this line is a big nop I think
546+
# the following makes sure that the existing elements are properly removed from the collection
547+
@collection.dup.each { |item| delete(item) } if @collection
547548
@collection = []
548549
if new_array.is_a? Collection
549550
@dummy_collection = new_array.dummy_collection

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def getter_common(attribute, reload)
108108
else
109109
virtual_fetch_on_server_warning(attribute) if on_opal_server? && changed?
110110
yield false, attribute
111-
end.tap { |value| Hyperstack::Internal::State::Variable.get(self, attribute) unless data_loading? }
111+
end.tap { Hyperstack::Internal::State::Variable.get(self, attribute) unless data_loading? }
112112
end
113113

114114
def find_association(association, id, klass)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
end.to eq("last_name"=>["no swear words allowed"])
4343
end
4444

45+
it "can validate and use the full_messages method" do
46+
expect_promise do
47+
User.new(last_name: 'f**k').validate.then do |new_user|
48+
new_user.errors.full_messages
49+
end
50+
end.to eq(["Last name no swear words allowed"])
51+
end
52+
4553
it "the valid? method will return true if the model has no errors" do
4654
mount "Validator" do
4755
class Validator < HyperComponent
@@ -62,8 +70,9 @@ class << self
6270
it "the valid? method will return false if the model has errors" do
6371
expect_promise do
6472
user = User.new(last_name: 'f**k')
65-
user.save.then { user.valid? }
73+
user.save(validate: true).then { |result| user.valid?}
6674
end.to be_falsy
75+
6776
end
6877

6978
it "the valid? method reacts to the model being saved" do

0 commit comments

Comments
 (0)