remote_record 0.7.0 → 0.7.1
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/remote_record/reference.rb +35 -11
 - data/lib/remote_record/version.rb +1 -1
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 95e6b11dd488bf26b81544c37471498737a26598429ba8290e4e9c8e0f6d79a0
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: cb125226d35be488de6d72651bb1249539d4d2dec487f75e242fc949593b008c
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: '08872bedccc7008cbf05129c2e9e791ee73c88bdc222dbaab1ff973bf32b0e24a284690072394c4b6e9a5126f7c0c851e6a2ffbf7ef855909397fbc77d870cbd'
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: d86cd65b6db39f4191d39b0be6e954b56b118daa02534eb0b8c20e10fcf988d9e07ada0fef2fc418e787b1a112dfc300b31df2f7afde199df88bb3fab68d04ca
         
     | 
| 
         @@ -40,23 +40,49 @@ module RemoteRecord 
     | 
|
| 
       40 
40 
     | 
    
         
             
                  end
         
     | 
| 
       41 
41 
     | 
    
         | 
| 
       42 
42 
     | 
    
         
             
                  def remote_all(&authz_proc)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    find_or_initialize_all(remote_record_class.all(&authz_proc))
         
     | 
| 
      
 44 
     | 
    
         
            +
                  end
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                  def remote_where(params, &authz_proc)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    find_or_initialize_all(remote_record_class.where(params, &authz_proc))
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                  private
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                  def find_or_initialize_all(remote_resources)
         
     | 
| 
       43 
53 
     | 
    
         
             
                    no_fetching do
         
     | 
| 
       44 
     | 
    
         
            -
                       
     | 
| 
       45 
     | 
    
         
            -
                         
     | 
| 
       46 
     | 
    
         
            -
                          record.attrs =  
     | 
| 
      
 54 
     | 
    
         
            +
                      pair_remote_resources_with_records(remote_resources) do |unsaved_resources, relation|
         
     | 
| 
      
 55 
     | 
    
         
            +
                        new_resources = unsaved_resources.map do |resource|
         
     | 
| 
      
 56 
     | 
    
         
            +
                          new(remote_resource_id: resource['id']).tap { |record| record.attrs = resource }
         
     | 
| 
       47 
57 
     | 
    
         
             
                        end
         
     | 
| 
      
 58 
     | 
    
         
            +
                        relation.to_a + new_resources
         
     | 
| 
       48 
59 
     | 
    
         
             
                      end
         
     | 
| 
       49 
60 
     | 
    
         
             
                    end
         
     | 
| 
       50 
61 
     | 
    
         
             
                  end
         
     | 
| 
       51 
62 
     | 
    
         | 
| 
       52 
     | 
    
         
            -
                  def  
     | 
| 
       53 
     | 
    
         
            -
                     
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
             
     | 
| 
       56 
     | 
    
         
            -
             
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
      
 63 
     | 
    
         
            +
                  def pair_remote_resources_with_records(remote_resources)
         
     | 
| 
      
 64 
     | 
    
         
            +
                    # get resource ids
         
     | 
| 
      
 65 
     | 
    
         
            +
                    ids = remote_resource_ids(remote_resources)
         
     | 
| 
      
 66 
     | 
    
         
            +
                    # get what exists in the database
         
     | 
| 
      
 67 
     | 
    
         
            +
                    relation = where(remote_resource_id: ids)
         
     | 
| 
      
 68 
     | 
    
         
            +
                    # for each record, set its attrs
         
     | 
| 
      
 69 
     | 
    
         
            +
                    relation.map do |record|
         
     | 
| 
      
 70 
     | 
    
         
            +
                      record.attrs = remote_resources.find do |r|
         
     | 
| 
      
 71 
     | 
    
         
            +
                        r['id'].to_s == record.remote_resource_id.to_s
         
     | 
| 
       58 
72 
     | 
    
         
             
                      end
         
     | 
| 
       59 
73 
     | 
    
         
             
                    end
         
     | 
| 
      
 74 
     | 
    
         
            +
                    unsaved_resources = resources_without_persisted_references(remote_resources, relation)
         
     | 
| 
      
 75 
     | 
    
         
            +
                    yield(unsaved_resources, relation)
         
     | 
| 
      
 76 
     | 
    
         
            +
                  end
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                  def remote_resource_ids(remote_resources)
         
     | 
| 
      
 79 
     | 
    
         
            +
                    remote_resources.map { |remote_resource| remote_resource['id'] }
         
     | 
| 
      
 80 
     | 
    
         
            +
                  end
         
     | 
| 
      
 81 
     | 
    
         
            +
             
     | 
| 
      
 82 
     | 
    
         
            +
                  def resources_without_persisted_references(remote_resources, relation)
         
     | 
| 
      
 83 
     | 
    
         
            +
                    remote_resources.reject do |resource|
         
     | 
| 
      
 84 
     | 
    
         
            +
                      relation.pluck(:remote_resource_id).include? resource['id']
         
     | 
| 
      
 85 
     | 
    
         
            +
                    end
         
     | 
| 
       60 
86 
     | 
    
         
             
                  end
         
     | 
| 
       61 
87 
     | 
    
         
             
                end
         
     | 
| 
       62 
88 
     | 
    
         | 
| 
         @@ -102,8 +128,6 @@ module RemoteRecord 
     | 
|
| 
       102 
128 
     | 
    
         
             
                    self
         
     | 
| 
       103 
129 
     | 
    
         
             
                  end
         
     | 
| 
       104 
130 
     | 
    
         | 
| 
       105 
     | 
    
         
            -
                  private
         
     | 
| 
       106 
     | 
    
         
            -
             
     | 
| 
       107 
131 
     | 
    
         
             
                  delegate :attrs=, to: :@instance
         
     | 
| 
       108 
132 
     | 
    
         | 
| 
       109 
133 
     | 
    
         
             
                  def instance
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: remote_record
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.7. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.7.1
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Simon Fish
         
     | 
| 
         @@ -9,7 +9,7 @@ authors: 
     | 
|
| 
       9 
9 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       11 
11 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       12 
     | 
    
         
            -
            date: 2021-02- 
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2021-02-11 00:00:00.000000000 Z
         
     | 
| 
       13 
13 
     | 
    
         
             
            dependencies:
         
     | 
| 
       14 
14 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       15 
15 
     | 
    
         
             
              name: activerecord
         
     |