simple_record 1.1.52 → 1.1.53

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_record.rb CHANGED
@@ -530,13 +530,28 @@ module SimpleRecord
530
530
  def set(name, value)
531
531
 
532
532
  att_meta = defined_attributes_local[name.to_sym]
533
- return if att_meta.nil?
534
- if att_meta.type == :belongs_to
535
- attname = name.to_s + '_id'
536
- attvalue = value.nil? ? nil : value.id
533
+ if att_meta.nil?
534
+ # check if it ends with id and see if att_meta is there
535
+ ends_with = name.to_s[-3, 3]
536
+ if ends_with == "_id"
537
+ puts 'ends with id'
538
+ n2 = name.to_s[0, name.length-3]
539
+ puts 'n2=' + n2
540
+ att_meta = defined_attributes_local[n2.to_sym]
541
+ puts 'defined_attributes_local=' + defined_attributes_local.inspect
542
+ attname = name.to_s
543
+ attvalue = value
544
+ name = n2
545
+ end
546
+ return if att_meta.nil?
537
547
  else
538
- attname = name.to_s
539
- attvalue = value
548
+ if att_meta.type == :belongs_to
549
+ attname = name.to_s + '_id'
550
+ attvalue = value.nil? ? nil : value.id
551
+ else
552
+ attname = name.to_s
553
+ attvalue = value
554
+ end
540
555
  end
541
556
  attvalue = strip_array(attvalue)
542
557
  make_dirty(attname, attvalue)
@@ -132,7 +132,7 @@ module SimpleRecord
132
132
 
133
133
  # Define reader method
134
134
  send(:define_method, arg) do
135
- # puts 'GETTING ' + arg.to_s
135
+ puts 'GETTING ' + arg.to_s
136
136
  attribute = defined_attributes_local[arg]
137
137
  options2 = attribute.options # @@belongs_to_map[arg]
138
138
  class_name = options2[:class_name] || arg.to_s[0...1].capitalize + arg.to_s[1...arg.to_s.length]
@@ -144,7 +144,8 @@ module SimpleRecord
144
144
  # puts 'val=' + @attributes[arg_id][0].inspect unless @attributes[arg_id].nil?
145
145
  ret = nil
146
146
  arg_id = arg.to_s + '_id'
147
- if !@attributes[arg_id].nil? && @attributes[arg_id].size > 0 && @attributes[arg_id][0] != nil && @attributes[arg_id][0] != ''
147
+ arg_id_val = send("#{arg_id}")
148
+ if arg_id_val
148
149
  if !cache_store.nil?
149
150
  arg_id_val = @attributes[arg_id][0]
150
151
  cache_key = self.class.cache_key(class_name, arg_id_val)
@@ -153,7 +154,7 @@ module SimpleRecord
153
154
  # puts 'belongs_to incache=' + ret.inspect
154
155
  end
155
156
  if ret.nil?
156
- to_eval = "#{class_name}.find(@attributes['#{arg_id}'][0])"
157
+ to_eval = "#{class_name}.find('#{arg_id_val}')"
157
158
  # puts 'to eval=' + to_eval
158
159
  begin
159
160
  ret = eval(to_eval) # (defined? #{arg}_id)
data/test/test_base.rb CHANGED
@@ -14,7 +14,7 @@ class TestBase < Test::Unit::TestCase
14
14
  #puts 'inspecting config = ' + @config.inspect
15
15
 
16
16
  # Establish AWS connection directly
17
- @@sdb = Aws::SdbInterface.new(@config['amazon']['access_key'], @config['amazon']['secret_key'], {:connection_mode => :per_request, :protocol => "http", :port => 80})
17
+ @@sdb = Aws::SdbInterface.new(@config['amazon']['access_key'], @config['amazon']['secret_key'], {:connection_mode => :per_request})
18
18
 
19
19
  SimpleRecord.establish_connection(@config['amazon']['access_key'], @config['amazon']['secret_key'], :connection_mode=>:single)
20
20
  SimpleRecord::Base.set_domain_prefix("simplerecord_tests_")
@@ -458,6 +458,19 @@ class TestSimpleRecord < TestBase
458
458
  assert mm.name = mm2.name
459
459
  end
460
460
 
461
+ def test_constructor_using_belongs_to_ids
462
+ mm = MyModel.new({:name=>"myname"})
463
+ mm.save
464
+
465
+ mm2 = MyChildModel.new({"name"=>"myname2", :my_model_id=>mm.id})
466
+ puts 'mm2=' + mm2.inspect
467
+ assert mm.id == mm2.my_model_id, "#{mm.id} != #{mm2.my_model_id}"
468
+ mm3 = mm2.my_model
469
+ puts 'mm3=' + mm3.inspect
470
+ assert mm.name == mm3.name
471
+
472
+ end
473
+
461
474
  def test_update_attributes
462
475
  mm = MyModel.new({:name=>"myname"})
463
476
  mm.save
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.52
4
+ version: 1.1.53
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder