passive_record 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/passive_record/instance_methods.rb +9 -1
 - data/lib/passive_record/version.rb +1 -1
 - data/spec/passive_record_spec.rb +6 -0
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: cef72fdda85e2a5d1dabfd009842c092780ce77d
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: a6f81fbd8c7a0b88d318e46a8aa232ae69b0a549
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: bb22e938cd0081234dd6f02b02ae060d3fc37e794b78b479a313e6793ab0fe486f367dfa38c91abb98b1f2594297a33ae4b529968d8b3906c1addcaacea6c8a1
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: d0e3ee535809bb7817be14dece26cb727bb0db7621fb1a090f9baa2ad69b9f54366c29a73f4651e15f938e2edf802e424390398212510aa6cd5fcc7d50af4cf1
         
     | 
| 
         @@ -52,7 +52,15 @@ module PassiveRecord 
     | 
|
| 
       52 
52 
     | 
    
         
             
                  when target_name
         
     | 
| 
       53 
53 
     | 
    
         
             
                    matching_relation.lookup
         
     | 
| 
       54 
54 
     | 
    
         
             
                  when "#{target_name}="
         
     | 
| 
       55 
     | 
    
         
            -
                     
     | 
| 
      
 55 
     | 
    
         
            +
                    if args.first.is_a?(Array)
         
     | 
| 
      
 56 
     | 
    
         
            +
                      # need to loop through each arg and set id
         
     | 
| 
      
 57 
     | 
    
         
            +
                      args.first.each do |child|
         
     | 
| 
      
 58 
     | 
    
         
            +
                        child.send(matching_relation.parent_model_id_field + "=", id)
         
     | 
| 
      
 59 
     | 
    
         
            +
                      end
         
     | 
| 
      
 60 
     | 
    
         
            +
                    else
         
     | 
| 
      
 61 
     | 
    
         
            +
                      # assume simple assignment
         
     | 
| 
      
 62 
     | 
    
         
            +
                      matching_relation.parent_model_id = args.first.id
         
     | 
| 
      
 63 
     | 
    
         
            +
                    end
         
     | 
| 
       56 
64 
     | 
    
         
             
                  when "create_#{target_name}", "create_#{target_name.singularize}"
         
     | 
| 
       57 
65 
     | 
    
         
             
                    matching_relation.create(*args)
         
     | 
| 
       58 
66 
     | 
    
         
             
                  when "#{target_name}_id"
         
     | 
    
        data/spec/passive_record_spec.rb
    CHANGED
    
    | 
         @@ -182,12 +182,18 @@ describe "passive record models" do 
     | 
|
| 
       182 
182 
     | 
    
         | 
| 
       183 
183 
     | 
    
         
             
                context 'one-to-many relationships' do
         
     | 
| 
       184 
184 
     | 
    
         
             
                  let(:parent) { Parent.create }
         
     | 
| 
      
 185 
     | 
    
         
            +
                  let(:another_parent) { Parent.create(children: [another_child]) }
         
     | 
| 
      
 186 
     | 
    
         
            +
                  let(:another_child) { Child.create }
         
     | 
| 
       185 
187 
     | 
    
         | 
| 
       186 
188 
     | 
    
         
             
                  it 'should create children' do
         
     | 
| 
       187 
189 
     | 
    
         
             
                    expect { parent.create_child }.to change{ Child.count }.by(1)
         
     | 
| 
       188 
190 
     | 
    
         
             
                    expect(parent.children).to all(be_a(Child))
         
     | 
| 
       189 
191 
     | 
    
         
             
                  end
         
     | 
| 
       190 
192 
     | 
    
         | 
| 
      
 193 
     | 
    
         
            +
                  it 'should assign children on creation' do
         
     | 
| 
      
 194 
     | 
    
         
            +
                    expect(another_parent.children).to match_array([another_child])
         
     | 
| 
      
 195 
     | 
    
         
            +
                  end
         
     | 
| 
      
 196 
     | 
    
         
            +
             
     | 
| 
       191 
197 
     | 
    
         
             
                  it 'should create inverse relationships' do
         
     | 
| 
       192 
198 
     | 
    
         
             
                    child = parent.create_child
         
     | 
| 
       193 
199 
     | 
    
         
             
                    expect(child.parent).to eq(parent)
         
     |