Skip to content

Commit 5f0e76c

Browse files
committed
Merge branch 'alpha1.5' into edge
2 parents 2e7c266 + aad5bed commit 5f0e76c

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,18 +339,34 @@ def server_method(name, default: nil)
339339
end
340340
end
341341

342+
# def define_attribute_methods
343+
# columns_hash.each do |name, column_hash|
344+
# next if name == primary_key
345+
# column_hash[:serialized?] = true if ReactiveRecord::Base.serialized?[self][name]
346+
#
347+
# define_method(name) { @backing_record.get_attr_value(name, nil) } unless method_defined?(name)
348+
# define_method("#{name}!") { @backing_record.get_attr_value(name, true) } unless method_defined?("#{name}!")
349+
# define_method("#{name}=") { |val| @backing_record.set_attr_value(name, val) } unless method_defined?("#{name}=")
350+
# define_method("#{name}_changed?") { @backing_record.changed?(name) } unless method_defined?("#{name}_changed?")
351+
# define_method("#{name}?") { @backing_record.get_attr_value(name, nil).present? } unless method_defined?("#{name}?")
352+
# end
353+
# self.inheritance_column = nil if inheritance_column && !columns_hash.key?(inheritance_column)
354+
# end
355+
356+
342357
def define_attribute_methods
343358
columns_hash.each do |name, column_hash|
344359
next if name == primary_key
345360
# only add serialized key if its serialized. This just makes testing a bit
346361
# easier by keeping the columns_hash the same if there are no seralized strings
347362
# see rspec ./spec/batch1/column_types/column_type_spec.rb:100
348363
column_hash[:serialized?] = true if ReactiveRecord::Base.serialized?[self][name]
349-
define_method(name) { @backing_record.get_attr_value(name, nil) }
350-
define_method("#{name}!") { @backing_record.get_attr_value(name, true) }
351-
define_method("#{name}=") { |val| @backing_record.set_attr_value(name, val) }
352-
define_method("#{name}_changed?") { @backing_record.changed?(name) }
353-
define_method("#{name}?") { @backing_record.get_attr_value(name, nil).present? }
364+
365+
define_method(name) { @backing_record.get_attr_value(name, nil) } unless method_defined?(name)
366+
define_method("#{name}!") { @backing_record.get_attr_value(name, true) } unless method_defined?("#{name}!")
367+
define_method("#{name}=") { |val| @backing_record.set_attr_value(name, val) } unless method_defined?("#{name}=")
368+
define_method("#{name}_changed?") { @backing_record.changed?(name) } unless method_defined?("#{name}_changed?")
369+
define_method("#{name}?") { @backing_record.get_attr_value(name, nil).present? } unless method_defined?("#{name}?")
354370
end
355371
self.inheritance_column = nil if inheritance_column && !columns_hash.key?(inheritance_column)
356372
end

ruby/hyper-model/spec/batch1/column_types/column_type_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -342,19 +342,4 @@ class TypeTest < ActiveRecord::Base
342342
end.to eq(r.text)
343343
end
344344

345-
it 'allows for attribute methods to be overwritten and use super' do
346-
isomorphic do
347-
class TypeTest < ActiveRecord::Base
348-
def string
349-
super.reverse
350-
end
351-
end
352-
end
353-
r = TypeTest.create(string: 'reverse')
354-
expect_promise do
355-
ReactiveRecord.load { TypeTest.find(1).string }
356-
end.to eq(r.string)
357-
expect(r.string).to eq(r[:string].reverse)
358-
end
359-
360345
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require 'spec_helper'
2+
require 'test_components'
3+
require 'rspec-steps'
4+
5+
RSpec::Steps.steps "overriding attribute methods", js: true do
6+
7+
it 'can override the getter' do
8+
expect_evaluate_ruby do
9+
TypeTest.class_eval do
10+
def string
11+
super.reverse
12+
end
13+
end
14+
TypeTest.new(string: 'hello').string
15+
end.to eq('hello'.reverse)
16+
end
17+
18+
it 'can override the forced getter' do
19+
expect_evaluate_ruby do
20+
TypeTest.class_eval do
21+
def string!
22+
!super
23+
end
24+
end
25+
TypeTest.new(string: 'hello').string!
26+
end.to eq(true)
27+
end
28+
29+
it 'can override the setter' do
30+
expect_evaluate_ruby do
31+
TypeTest.class_eval do
32+
def integer=(x)
33+
super(-x)
34+
end
35+
end
36+
test = TypeTest.new
37+
test.integer = 12
38+
test.integer
39+
end.to eq(-12)
40+
end
41+
42+
it 'can override the _changed method' do
43+
expect_evaluate_ruby do
44+
TypeTest.class_eval do
45+
def integer_changed?
46+
integer.even? ? super : false
47+
end
48+
end
49+
[TypeTest.new(integer: 6).integer_changed?, TypeTest.new(integer: 7).integer_changed?]
50+
end.to eq([true, false])
51+
end
52+
53+
it 'can override the boolean ? method' do
54+
expect_evaluate_ruby do
55+
TypeTest.class_eval do
56+
def boolean?
57+
!super
58+
end
59+
end
60+
TypeTest.new(boolean: true).boolean?
61+
end.to eq(false)
62+
end
63+
64+
end

ruby/hyper-model/spec/support/component_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def isomorphic(&block)
306306
def evaluate_ruby(str="", opts={}, &block)
307307
insure_mount
308308
str = "#{str}\n#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.last}" if block
309-
js = Opal.compile(str).gsub("\n","").gsub("(Opal);","(Opal)")
309+
js = Opal.compile(str).gsub("// Prepare super implicit arguments\n", "").gsub("\n","").gsub("(Opal);","(Opal)")
310310
JSON.parse(evaluate_script("[#{js}].$to_json()"), opts).first
311311
end
312312

ruby/hyper-spec/lib/hyper-spec/component_test_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ def evaluate_ruby(str = '', opts = {}, &block)
111111
if block
112112
str = "#{str}\n#{Unparser.unparse Parser::CurrentRuby.parse(block.source).children.last}"
113113
end
114-
js = Opal.compile(str).delete("\n").gsub('(Opal);', '(Opal)')
114+
js = Opal.compile(str).gsub("// Prepare super implicit arguments\n", "")
115+
.delete("\n").gsub('(Opal);', '(Opal)')
115116
# workaround for firefox 58 and geckodriver 0.19.1, because firefox is unable to find .$to_json:
116117
# JSON.parse(evaluate_script("(function(){var a=Opal.Array.$new(); a[0]=#{js}; return a.$to_json();})();"), opts).first
117118
JSON.parse(evaluate_script("[#{js}].$to_json()"), opts).first

0 commit comments

Comments
 (0)