calculated_attributes 0.1.2 → 0.1.3
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/gemfiles/rails3.gemfile.lock +2 -0
- data/gemfiles/rails4_1.gemfile.lock +2 -0
- data/gemfiles/rails4_2.gemfile.lock +2 -0
- data/lib/calculated_attributes/arel_patches.rb +2 -2
- data/lib/calculated_attributes/model_methods.rb +4 -4
- data/lib/calculated_attributes/rails_4_patches.rb +20 -9
- data/lib/calculated_attributes/version.rb +1 -1
- data/spec/lib/calculated_attributes_spec.rb +4 -1
- 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: 114648311fe5389527380b81d89677279f8e9019
         | 
| 4 | 
            +
              data.tar.gz: 42e5dc334c01643eaf56d97e4eb42aca0cb0113d
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 6635d62772fca4d1da02752555ed19fd55c8b04d9047e95086c233f89ee83822c7e277257ba21607e9bf7bf484b518e2547477333f5da84a1ccd0bcea223cc7d
         | 
| 7 | 
            +
              data.tar.gz: f5881cb7665e8132f22ee7dbfd932b7695d420cd5fec33fb7e2248a40cdd2975ca093a37fca39ff0375e493e1039fef630c06213935eefa771a13392ebe16772
         | 
| @@ -27,6 +27,7 @@ GEM | |
| 27 27 | 
             
                astrolabe (1.3.0)
         | 
| 28 28 | 
             
                  parser (>= 2.2.0.pre.3, < 3.0)
         | 
| 29 29 | 
             
                builder (3.0.4)
         | 
| 30 | 
            +
                byebug (6.0.2)
         | 
| 30 31 | 
             
                diff-lcs (1.2.5)
         | 
| 31 32 | 
             
                i18n (0.7.0)
         | 
| 32 33 | 
             
                multi_json (1.11.1)
         | 
| @@ -66,6 +67,7 @@ DEPENDENCIES | |
| 66 67 | 
             
              activerecord (= 3.2.21)
         | 
| 67 68 | 
             
              appraisal (~> 1.0.3)
         | 
| 68 69 | 
             
              bundler (~> 1.7)
         | 
| 70 | 
            +
              byebug
         | 
| 69 71 | 
             
              calculated_attributes!
         | 
| 70 72 | 
             
              rake (~> 10.0)
         | 
| 71 73 | 
             
              rspec (~> 3.1)
         | 
| @@ -29,6 +29,7 @@ GEM | |
| 29 29 | 
             
                astrolabe (1.3.0)
         | 
| 30 30 | 
             
                  parser (>= 2.2.0.pre.3, < 3.0)
         | 
| 31 31 | 
             
                builder (3.2.2)
         | 
| 32 | 
            +
                byebug (6.0.2)
         | 
| 32 33 | 
             
                diff-lcs (1.2.5)
         | 
| 33 34 | 
             
                i18n (0.7.0)
         | 
| 34 35 | 
             
                json (1.8.3)
         | 
| @@ -71,6 +72,7 @@ DEPENDENCIES | |
| 71 72 | 
             
              activerecord (= 4.1.11)
         | 
| 72 73 | 
             
              appraisal (~> 1.0.3)
         | 
| 73 74 | 
             
              bundler (~> 1.7)
         | 
| 75 | 
            +
              byebug
         | 
| 74 76 | 
             
              calculated_attributes!
         | 
| 75 77 | 
             
              rake (~> 10.0)
         | 
| 76 78 | 
             
              rspec (~> 3.1)
         | 
| @@ -29,6 +29,7 @@ GEM | |
| 29 29 | 
             
                astrolabe (1.3.0)
         | 
| 30 30 | 
             
                  parser (>= 2.2.0.pre.3, < 3.0)
         | 
| 31 31 | 
             
                builder (3.2.2)
         | 
| 32 | 
            +
                byebug (6.0.2)
         | 
| 32 33 | 
             
                diff-lcs (1.2.5)
         | 
| 33 34 | 
             
                i18n (0.7.0)
         | 
| 34 35 | 
             
                json (1.8.3)
         | 
| @@ -71,6 +72,7 @@ DEPENDENCIES | |
| 71 72 | 
             
              activerecord (= 4.2.2)
         | 
| 72 73 | 
             
              appraisal (~> 1.0.3)
         | 
| 73 74 | 
             
              bundler (~> 1.7)
         | 
| 75 | 
            +
              byebug
         | 
| 74 76 | 
             
              calculated_attributes!
         | 
| 75 77 | 
             
              rake (~> 10.0)
         | 
| 76 78 | 
             
              rspec (~> 3.1)
         | 
| @@ -17,7 +17,7 @@ module CalculatedAttributes | |
| 17 17 | 
             
            end
         | 
| 18 18 | 
             
            ActiveRecord::Base.extend CalculatedAttributes
         | 
| 19 19 |  | 
| 20 | 
            -
            ActiveRecord::Base | 
| 20 | 
            +
            class ActiveRecord::Base
         | 
| 21 21 | 
             
              def calculated(*args)
         | 
| 22 22 | 
             
                if self.class.respond_to? :scoped
         | 
| 23 23 | 
             
                  self.class.scoped.calculated(*args).find(id)
         | 
| @@ -62,9 +62,9 @@ ActiveRecord::Base.send(:include, Module.new do | |
| 62 62 | 
             
                  end
         | 
| 63 63 | 
             
                super || (no_sym_in_attr && (self.class.calculated.calculated[method] || self.class.base_class.calculated.calculated[method]))
         | 
| 64 64 | 
             
              end
         | 
| 65 | 
            -
            end | 
| 65 | 
            +
            end
         | 
| 66 66 |  | 
| 67 | 
            -
            ActiveRecord::Relation | 
| 67 | 
            +
            class ActiveRecord::Relation
         | 
| 68 68 | 
             
              def calculated(*args)
         | 
| 69 69 | 
             
                projections = arel.projections
         | 
| 70 70 | 
             
                args.each do |arg|
         | 
| @@ -83,4 +83,4 @@ ActiveRecord::Relation.send(:include, Module.new do | |
| 83 83 | 
             
                end
         | 
| 84 84 | 
             
                select(projections)
         | 
| 85 85 | 
             
              end
         | 
| 86 | 
            -
            end | 
| 86 | 
            +
            end
         | 
| @@ -44,19 +44,30 @@ module ActiveRecord | |
| 44 44 | 
             
                    parents = model_cache[join_root]
         | 
| 45 45 | 
             
                    column_aliases = aliases.column_aliases join_root
         | 
| 46 46 |  | 
| 47 | 
            -
                     | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
                      record_count: result_set.length,
         | 
| 51 | 
            -
                      class_name: join_root.base_klass.name
         | 
| 52 | 
            -
                    }
         | 
| 53 | 
            -
             | 
| 54 | 
            -
                    message_bus.instrument('instantiation.active_record', payload) do
         | 
| 47 | 
            +
                    if ActiveRecord::VERSION::MINOR == 1
         | 
| 48 | 
            +
                      type_caster = result_set.column_type primary_key
         | 
| 49 | 
            +
                      
         | 
| 55 50 | 
             
                      result_set.each do |row_hash|
         | 
| 56 | 
            -
                         | 
| 51 | 
            +
                        primary_id = type_caster.type_cast row_hash[primary_key]
         | 
| 52 | 
            +
                        parent = parents[primary_id] ||= join_root.instantiate(row_hash, column_aliases)
         | 
| 57 53 | 
             
                        @calculated_columns.each { |column| parent[column.right] = model[column.right] } if @calculated_columns
         | 
| 58 54 | 
             
                        construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
         | 
| 59 55 | 
             
                      end
         | 
| 56 | 
            +
                    else
         | 
| 57 | 
            +
                      message_bus = ActiveSupport::Notifications.instrumenter
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                      payload = {
         | 
| 60 | 
            +
                        record_count: result_set.length,
         | 
| 61 | 
            +
                        class_name: join_root.base_klass.name
         | 
| 62 | 
            +
                      }
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                      message_bus.instrument('instantiation.active_record', payload) do
         | 
| 65 | 
            +
                        result_set.each do |row_hash|
         | 
| 66 | 
            +
                          parent = parents[row_hash[primary_key]] ||= join_root.instantiate(row_hash, column_aliases)
         | 
| 67 | 
            +
                          @calculated_columns.each { |column| parent[column.right] = model[column.right] } if @calculated_columns
         | 
| 68 | 
            +
                          construct(parent, join_root, row_hash, result_set, seen, model_cache, aliases)
         | 
| 69 | 
            +
                        end
         | 
| 70 | 
            +
                      end
         | 
| 60 71 | 
             
                    end
         | 
| 61 72 |  | 
| 62 73 | 
             
                    parents.values
         | 
| @@ -104,7 +104,10 @@ describe 'calculated_attributes' do | |
| 104 104 |  | 
| 105 105 | 
             
              context 'when eager loading models' do
         | 
| 106 106 | 
             
                it 'includes calculated attributes' do
         | 
| 107 | 
            -
                  scope =  | 
| 107 | 
            +
                  scope = case ActiveRecord::VERSION::MAJOR
         | 
| 108 | 
            +
                  when 4 then model_scoped(Post).includes(:comments).references(:comments)
         | 
| 109 | 
            +
                  when 3 then model_scoped(Post).eager_load(:comments)
         | 
| 110 | 
            +
                  end
         | 
| 108 111 | 
             
                  expect(scope.first.comments_count).to eq(1)
         | 
| 109 112 | 
             
                end
         | 
| 110 113 | 
             
              end
         |