Class: ArelQuery::ArelHelper

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/arel_query.rb

Constant Summary collapse

FUNCTION_METHOD_PATTERN =
/\A[a-z]+(_[a-z]+)*\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ArelHelper

Returns a new instance of ArelHelper.



49
50
51
52
# File 'app/models/concerns/arel_query.rb', line 49

def initialize(model)
  @model = model
  @table = model.arel_table if model.respond_to?(:arel_table)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/models/concerns/arel_query.rb', line 113

def method_missing(method_name, *args, &block)
  node_class = "Arel::Nodes::#{method_name.to_s.camelize}".safe_constantize

  if node_class
    self.class.define_method method_name do |*method_args|
      node_class.new(*method_args)
    end
  elsif @table&.respond_to?(method_name)
    return @table.send(method_name, *args)
  elsif method_name.to_s =~ FUNCTION_METHOD_PATTERN
    self.class.define_method method_name do |*method_args|
      Arel::Nodes::NamedFunction.new(method_name.to_s, method_args)
    end
  else
    return super
  end

  send(method_name, *args)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



43
44
45
# File 'app/models/concerns/arel_query.rb', line 43

def model
  @model
end

Instance Method Details

#[](it) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'app/models/concerns/arel_query.rb', line 140

def [](it)
  case it
  when Symbol
    @table[it]
  when String
    string(it)
  else
    literal(it)
  end
end

#as(what, alias_name) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
89
# File 'app/models/concerns/arel_query.rb', line 85

def as(what, alias_name)
  raise ArgumentError, 'Alias name must be a Symbol or String' unless alias_name.is_a?(Symbol) || alias_name.is_a?(String)

  Arel::Nodes::As.new(what, literal(alias_name))
end

#column(name) ⇒ Object Also known as: col, c



133
134
135
# File 'app/models/concerns/arel_query.rb', line 133

def column(name)
  @table[name]
end

#connectionObject



103
104
105
# File 'app/models/concerns/arel_query.rb', line 103

def connection
  ActiveRecord::Base.connection
end

#for_with(name, relation) ⇒ Object



91
92
93
# File 'app/models/concerns/arel_query.rb', line 91

def for_with(name, relation)
  WithHelper.new(name, relation)
end

#literal(value) ⇒ Object Also known as: l, lit



66
67
68
# File 'app/models/concerns/arel_query.rb', line 66

def literal(value)
  Arel::Nodes::SqlLiteral.new(value.to_s)
end

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/concerns/arel_query.rb', line 107

def respond_to_missing?(_method_name, _include_private = false)
  true
end

#results(query) ⇒ Object



95
96
97
# File 'app/models/concerns/arel_query.rb', line 95

def results(query)
  connection.select_all(query)
end

#rows(query) ⇒ Object



99
100
101
# File 'app/models/concerns/arel_query.rb', line 99

def rows(query)
  results(query).to_a
end

#selectArel::SelectManager

Returns:

  • (Arel::SelectManager)


81
82
83
# File 'app/models/concerns/arel_query.rb', line 81

def select(...)
  Arel::SelectManager.new(...)
end

#string(value) ⇒ Object Also known as: s, str



73
74
75
# File 'app/models/concerns/arel_query.rb', line 73

def string(value)
  literal "'#{value}'"
end

#table(name = nil) ⇒ Object Also known as: t



54
55
56
57
58
59
60
61
62
# File 'app/models/concerns/arel_query.rb', line 54

def table(name = nil)
  if name
    Arel::Table.new(name)
  elsif @table
    @table
  else
    raise ArgumentError, 'No table available. Provide a table name.'
  end
end