pg_search 1.0.1 → 1.0.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/CHANGELOG.md +5 -1
- data/lib/pg_search/multisearch/rebuilder.rb +10 -1
- data/lib/pg_search/version.rb +1 -1
- data/spec/lib/pg_search/multisearch/rebuilder_spec.rb +123 -84
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: cfa2efbbbc811da8b5c62690f0084c9ddd60615d
         | 
| 4 | 
            +
              data.tar.gz: 90e7078224516ca17fdc503b418f309fd72560ae
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9c177eb231f004b44affdd5006ab5de2d75b13b4b4da0fbc0c4cb85b9d4394a1d96e26a27f28d23c4200638fc523e1dda1f414067a350c417f160ab0d2a96a2c
         | 
| 7 | 
            +
              data.tar.gz: 7e98e612c9d089f5bab6c19365485c6190ad0b6338f662bfeb8864516592618f7883508c019bb031c7472ff3b5793b51f35ec27aaafcee748b9b85a5006fffe0
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,8 +1,12 @@ | |
| 1 1 | 
             
            # pg_search changelog
         | 
| 2 2 |  | 
| 3 | 
            +
            ## 1.0.2
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            *   Don’t use SQL to rebuild search documents when models are multisearchable against dynamic methods and not just columns. Iterate over each record with `find_each` instead.
         | 
| 6 | 
            +
             | 
| 3 7 | 
             
            ## 1.0.1
         | 
| 4 8 |  | 
| 5 | 
            -
            *   Call `.unscoped` on relation used to build subquery, to eliminate unnecessary JOINs. (Markus Doits) | 
| 9 | 
            +
            *   Call `.unscoped` on relation used to build subquery, to eliminate unnecessary JOINs. (Markus Doits)
         | 
| 6 10 |  | 
| 7 11 | 
             
            ## 1.0.0
         | 
| 8 12 |  | 
| @@ -13,7 +13,7 @@ module PgSearch | |
| 13 13 | 
             
                  def rebuild
         | 
| 14 14 | 
             
                    if model.respond_to?(:rebuild_pg_search_documents)
         | 
| 15 15 | 
             
                      model.rebuild_pg_search_documents
         | 
| 16 | 
            -
                    elsif  | 
| 16 | 
            +
                    elsif conditional? || dynamic?
         | 
| 17 17 | 
             
                      model.find_each { |record| record.update_pg_search_document }
         | 
| 18 18 | 
             
                    else
         | 
| 19 19 | 
             
                      model.connection.execute(rebuild_sql)
         | 
| @@ -24,6 +24,15 @@ module PgSearch | |
| 24 24 |  | 
| 25 25 | 
             
                  attr_reader :model
         | 
| 26 26 |  | 
| 27 | 
            +
                  def conditional?
         | 
| 28 | 
            +
                    model.pg_search_multisearchable_options.key?(:if) || model.pg_search_multisearchable_options.key?(:unless)
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  def dynamic?
         | 
| 32 | 
            +
                    column_names = model.columns.map(&:name)
         | 
| 33 | 
            +
                    columns.any? { |column| !column_names.include?(column.to_s) }
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 27 36 | 
             
                  def connection
         | 
| 28 37 | 
             
                    model.connection
         | 
| 29 38 | 
             
                  end
         | 
    
        data/lib/pg_search/version.rb
    CHANGED
    
    
| @@ -23,7 +23,7 @@ describe PgSearch::Multisearch::Rebuilder do | |
| 23 23 | 
             
                    end
         | 
| 24 24 | 
             
                  end
         | 
| 25 25 |  | 
| 26 | 
            -
                  context " | 
| 26 | 
            +
                  context "and multisearchable is conditional" do
         | 
| 27 27 | 
             
                    [:if, :unless].each do |conditional_key|
         | 
| 28 28 | 
             
                      context "via :#{conditional_key}" do
         | 
| 29 29 | 
             
                        with_model :Model do
         | 
| @@ -51,55 +51,56 @@ describe PgSearch::Multisearch::Rebuilder do | |
| 51 51 | 
             
                end
         | 
| 52 52 |  | 
| 53 53 | 
             
                context "when the model does not define .rebuild_pg_search_documents" do
         | 
| 54 | 
            -
                  context " | 
| 55 | 
            -
                     | 
| 56 | 
            -
                       | 
| 57 | 
            -
                        t | 
| 58 | 
            -
             | 
| 54 | 
            +
                  context "and multisearchable is not conditional" do
         | 
| 55 | 
            +
                    context "when :against only includes columns" do
         | 
| 56 | 
            +
                      with_model :Model do
         | 
| 57 | 
            +
                        table do |t|
         | 
| 58 | 
            +
                          t.string :name
         | 
| 59 | 
            +
                        end
         | 
| 59 60 |  | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 61 | 
            +
                        model do
         | 
| 62 | 
            +
                          include PgSearch
         | 
| 63 | 
            +
                          multisearchable :against => :name
         | 
| 64 | 
            +
                        end
         | 
| 63 65 | 
             
                      end
         | 
| 64 | 
            -
                    end
         | 
| 65 66 |  | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 67 | 
            +
                      it "should not call :rebuild_pg_search_documents" do
         | 
| 68 | 
            +
                        rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
         | 
| 68 69 |  | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 70 | 
            +
                        # stub respond_to? to return false since should_not_receive defines the method
         | 
| 71 | 
            +
                        original_respond_to = Model.method(:respond_to?)
         | 
| 72 | 
            +
                        allow(Model).to receive(:respond_to?) do |method_name, *args|
         | 
| 73 | 
            +
                          if method_name == :rebuild_pg_search_documents
         | 
| 74 | 
            +
                            false
         | 
| 75 | 
            +
                          else
         | 
| 76 | 
            +
                            original_respond_to.call(method_name, *args)
         | 
| 77 | 
            +
                          end
         | 
| 76 78 | 
             
                        end
         | 
| 77 | 
            -
                      end
         | 
| 78 79 |  | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 80 | 
            +
                        expect(Model).not_to receive(:rebuild_pg_search_documents)
         | 
| 81 | 
            +
                        rebuilder.rebuild
         | 
| 82 | 
            +
                      end
         | 
| 82 83 |  | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 84 | 
            +
                      it "should execute the default SQL" do
         | 
| 85 | 
            +
                        time = DateTime.parse("2001-01-01")
         | 
| 86 | 
            +
                        rebuilder = PgSearch::Multisearch::Rebuilder.new(Model, lambda { time } )
         | 
| 86 87 |  | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 88 | 
            +
                        # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
         | 
| 89 | 
            +
                        # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
         | 
| 90 | 
            +
                        version_4_0_1_or_newer = (
         | 
| 90 91 | 
             
                        (ActiveRecord::VERSION::MAJOR > 4) ||
         | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 92 | 
            +
                          (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
         | 
| 93 | 
            +
                          (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
         | 
| 94 | 
            +
                        )
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                        expected_timestamp =
         | 
| 97 | 
            +
                          if version_4_0_1_or_newer
         | 
| 98 | 
            +
                            "2001-01-01 00:00:00.000000"
         | 
| 99 | 
            +
                          else
         | 
| 100 | 
            +
                            "2001-01-01 00:00:00"
         | 
| 101 | 
            +
                          end
         | 
| 101 102 |  | 
| 102 | 
            -
             | 
| 103 | 
            +
                        expected_sql = <<-SQL.strip_heredoc
         | 
| 103 104 | 
             
                        INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
         | 
| 104 105 | 
             
                          SELECT 'Model' AS searchable_type,
         | 
| 105 106 | 
             
                                 #{Model.quoted_table_name}.#{Model.primary_key} AS searchable_id,
         | 
| @@ -109,53 +110,53 @@ describe PgSearch::Multisearch::Rebuilder do | |
| 109 110 | 
             
                                 '#{expected_timestamp}' AS created_at,
         | 
| 110 111 | 
             
                                 '#{expected_timestamp}' AS updated_at
         | 
| 111 112 | 
             
                          FROM #{Model.quoted_table_name}
         | 
| 112 | 
            -
             | 
| 113 | 
            +
                        SQL
         | 
| 113 114 |  | 
| 114 | 
            -
             | 
| 115 | 
            +
                        executed_sql = []
         | 
| 115 116 |  | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 117 | 
            +
                        notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
         | 
| 118 | 
            +
                          executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
         | 
| 119 | 
            +
                        end
         | 
| 119 120 |  | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 121 | 
            +
                        rebuilder.rebuild
         | 
| 122 | 
            +
                        ActiveSupport::Notifications.unsubscribe(notifier)
         | 
| 122 123 |  | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 124 | 
            +
                        expect(executed_sql.length).to eq(1)
         | 
| 125 | 
            +
                        expect(executed_sql.first.strip).to eq(expected_sql.strip)
         | 
| 126 | 
            +
                      end
         | 
| 126 127 |  | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 128 | 
            +
                      context "for a model with a non-standard primary key" do
         | 
| 129 | 
            +
                        with_model :ModelWithNonStandardPrimaryKey do
         | 
| 130 | 
            +
                          table primary_key: :non_standard_primary_key do |t|
         | 
| 131 | 
            +
                            t.string :name
         | 
| 132 | 
            +
                          end
         | 
| 132 133 |  | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 134 | 
            +
                          model do
         | 
| 135 | 
            +
                            include PgSearch
         | 
| 136 | 
            +
                            multisearchable :against => :name
         | 
| 137 | 
            +
                          end
         | 
| 136 138 | 
             
                        end
         | 
| 137 | 
            -
                      end
         | 
| 138 139 |  | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 140 | 
            +
                        it "generates SQL with the correct primary key" do
         | 
| 141 | 
            +
                          time = DateTime.parse("2001-01-01")
         | 
| 142 | 
            +
                          rebuilder = PgSearch::Multisearch::Rebuilder.new(ModelWithNonStandardPrimaryKey, lambda { time } )
         | 
| 142 143 |  | 
| 143 | 
            -
             | 
| 144 | 
            -
             | 
| 145 | 
            -
             | 
| 144 | 
            +
                          # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
         | 
| 145 | 
            +
                          # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
         | 
| 146 | 
            +
                          version_4_0_1_or_newer = (
         | 
| 146 147 | 
             
                          (ActiveRecord::VERSION::MAJOR > 4) ||
         | 
| 147 | 
            -
             | 
| 148 | 
            -
             | 
| 149 | 
            -
             | 
| 150 | 
            -
             | 
| 151 | 
            -
             | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
             | 
| 155 | 
            -
             | 
| 156 | 
            -
             | 
| 148 | 
            +
                            (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
         | 
| 149 | 
            +
                            (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
         | 
| 150 | 
            +
                          )
         | 
| 151 | 
            +
             | 
| 152 | 
            +
                          expected_timestamp =
         | 
| 153 | 
            +
                            if version_4_0_1_or_newer
         | 
| 154 | 
            +
                              "2001-01-01 00:00:00.000000"
         | 
| 155 | 
            +
                            else
         | 
| 156 | 
            +
                              "2001-01-01 00:00:00"
         | 
| 157 | 
            +
                            end
         | 
| 157 158 |  | 
| 158 | 
            -
             | 
| 159 | 
            +
                          expected_sql = <<-SQL.strip_heredoc
         | 
| 159 160 | 
             
                          INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
         | 
| 160 161 | 
             
                            SELECT 'ModelWithNonStandardPrimaryKey' AS searchable_type,
         | 
| 161 162 | 
             
                                   #{ModelWithNonStandardPrimaryKey.quoted_table_name}.non_standard_primary_key AS searchable_id,
         | 
| @@ -165,24 +166,62 @@ describe PgSearch::Multisearch::Rebuilder do | |
| 165 166 | 
             
                                   '#{expected_timestamp}' AS created_at,
         | 
| 166 167 | 
             
                                   '#{expected_timestamp}' AS updated_at
         | 
| 167 168 | 
             
                            FROM #{ModelWithNonStandardPrimaryKey.quoted_table_name}
         | 
| 168 | 
            -
             | 
| 169 | 
            +
                          SQL
         | 
| 169 170 |  | 
| 170 | 
            -
             | 
| 171 | 
            +
                          executed_sql = []
         | 
| 171 172 |  | 
| 172 | 
            -
             | 
| 173 | 
            -
             | 
| 173 | 
            +
                          notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
         | 
| 174 | 
            +
                            executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
         | 
| 175 | 
            +
                          end
         | 
| 176 | 
            +
             | 
| 177 | 
            +
                          rebuilder.rebuild
         | 
| 178 | 
            +
                          ActiveSupport::Notifications.unsubscribe(notifier)
         | 
| 179 | 
            +
             | 
| 180 | 
            +
                          expect(executed_sql.length).to eq(1)
         | 
| 181 | 
            +
                          expect(executed_sql.first.strip).to eq(expected_sql.strip)
         | 
| 174 182 | 
             
                        end
         | 
| 183 | 
            +
                      end
         | 
| 184 | 
            +
                    end
         | 
| 185 | 
            +
             | 
| 186 | 
            +
                    context "when :against includes non-column dynamic methods" do
         | 
| 187 | 
            +
                      with_model :Model do
         | 
| 188 | 
            +
                        table do
         | 
| 189 | 
            +
                        end
         | 
| 190 | 
            +
             | 
| 191 | 
            +
                        model do
         | 
| 192 | 
            +
                          include PgSearch
         | 
| 193 | 
            +
                          multisearchable against: [:foo]
         | 
| 194 | 
            +
             | 
| 195 | 
            +
                          def foo
         | 
| 196 | 
            +
                            "bar"
         | 
| 197 | 
            +
                          end
         | 
| 198 | 
            +
                        end
         | 
| 199 | 
            +
                      end
         | 
| 200 | 
            +
             | 
| 201 | 
            +
                      it "calls update_pg_search_document on each record" do
         | 
| 202 | 
            +
                        record = Model.create!
         | 
| 203 | 
            +
             | 
| 204 | 
            +
                        rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
         | 
| 205 | 
            +
             | 
| 206 | 
            +
                        # stub respond_to? to return false since should_not_receive defines the method
         | 
| 207 | 
            +
                        original_respond_to = Model.method(:respond_to?)
         | 
| 208 | 
            +
                        allow(Model).to receive(:respond_to?) do |method_name, *args|
         | 
| 209 | 
            +
                          if method_name == :rebuild_pg_search_documents
         | 
| 210 | 
            +
                            false
         | 
| 211 | 
            +
                          else
         | 
| 212 | 
            +
                            original_respond_to.call(method_name, *args)
         | 
| 213 | 
            +
                          end
         | 
| 214 | 
            +
                        end
         | 
| 215 | 
            +
                        expect(Model).not_to receive(:rebuild_pg_search_documents)
         | 
| 175 216 |  | 
| 176 217 | 
             
                        rebuilder.rebuild
         | 
| 177 | 
            -
                        ActiveSupport::Notifications.unsubscribe(notifier)
         | 
| 178 218 |  | 
| 179 | 
            -
                        expect( | 
| 180 | 
            -
                        expect(executed_sql.first.strip).to eq(expected_sql.strip)
         | 
| 219 | 
            +
                        expect(record.pg_search_document).to be_present
         | 
| 181 220 | 
             
                      end
         | 
| 182 221 | 
             
                    end
         | 
| 183 222 | 
             
                  end
         | 
| 184 223 |  | 
| 185 | 
            -
                  context " | 
| 224 | 
            +
                  context "and multisearchable is conditional" do
         | 
| 186 225 | 
             
                    context "via :if" do
         | 
| 187 226 | 
             
                      with_model :Model do
         | 
| 188 227 | 
             
                        table do |t|
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: pg_search
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0. | 
| 4 | 
            +
              version: 1.0.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Grant Hutchins
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2015-05- | 
| 12 | 
            +
            date: 2015-05-04 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: activerecord
         |