File: table_select.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,254 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen-string-literal: true

module Sequel
  module Plugins
    # The table_select plugin changes the default selection for a
    # model dataset from <tt>*</tt> to <tt>table.*</tt>.
    # This makes it so that if you join the model's dataset to
    # other tables, columns in the other tables do not appear
    # in the result sets (and possibly overwrite columns in the
    # current model with the same name).
    #
    # Usage:
    #
    #   # Make all model subclasses select table.*
    #   Sequel::Model.plugin :table_select
    #
    #   # Make the Album class select albums.*
    #   Album.plugin :table_select
    module TableSelect
      # Modify the current model's dataset selection, if the model
      # has a dataset.
      def self.configure(model)
        model.instance_exec do
          self.dataset = dataset if @dataset
        end
      end

      module ClassMethods
        private

        # If the underlying dataset selects from a single table and
        # has no explicit selection, select table.* from that table.
        def convert_input_dataset(ds)
          ds = super
          unless ds.opts[:select]
            ds = ds.select_all(ds.first_source)
          end
          ds
        end
      end
    end
  end
end