Class: Groupdate::Magic::Relation
- Inherits:
-
Groupdate::Magic
- Object
- Groupdate::Magic
- Groupdate::Magic::Relation
- Defined in:
- lib/groupdate/magic.rb
Constant Summary
Constants inherited from Groupdate::Magic
Instance Attribute Summary
Attributes inherited from Groupdate::Magic
#group_index, #n_seconds, #options, #period
Class Method Summary collapse
- .generate_relation(relation, field:, **options) ⇒ Object
-
.process_result(relation, result, **options) ⇒ Object
allow any options to keep flexible for future.
-
.resolve_column(relation, column) ⇒ Object
resolves eagerly need to convert both where_clause (easy) and group_clause (not easy) if want to avoid this.
-
.validate_column(column) ⇒ Object
basic version of Active Record disallow_raw_sql! symbol = column (safe), Arel node = SQL (safe), other = untrusted matches table.column and column.
Instance Method Summary collapse
- #cast_method ⇒ Object
- #cast_result(result, multiple_groups) ⇒ Object
- #check_nils(result, multiple_groups, relation) ⇒ Object
-
#initialize(**options) ⇒ Relation
constructor
A new instance of Relation.
- #perform(relation, result, default_value:) ⇒ Object
- #time_zone_support?(relation) ⇒ Boolean
Methods inherited from Groupdate::Magic
#day_start, #range, #series_builder, #time_range, #time_zone, #validate_arguments, #validate_keywords, validate_period, #week_start
Constructor Details
#initialize(**options) ⇒ Relation
Returns a new instance of Relation.
122 123 124 125 |
# File 'lib/groupdate/magic.rb', line 122 def initialize(**) super(**.reject { |k, _| [:default_value, :carry_forward, :last, :current].include?(k) }) @options = end |
Class Method Details
.generate_relation(relation, field:, **options) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/groupdate/magic.rb', line 198 def self.generate_relation(relation, field:, **) magic = Groupdate::Magic::Relation.new(**) adapter_name = relation.connection.adapter_name adapter = Groupdate.adapters[adapter_name] raise Groupdate::Error, "Connection adapter not supported: #{adapter_name}" unless adapter # very important column = validate_column(field) column = resolve_column(relation, column) # generate ActiveRecord relation relation = adapter.new( relation, column: column, period: magic.period, time_zone: magic.time_zone, time_range: magic.time_range, week_start: magic.week_start, day_start: magic.day_start, n_seconds: magic.n_seconds ).generate # add Groupdate info magic.group_index = relation.group_values.size - 1 (relation.groupdate_values ||= []) << magic relation end |
.process_result(relation, result, **options) ⇒ Object
allow any options to keep flexible for future
254 255 256 257 258 259 |
# File 'lib/groupdate/magic.rb', line 254 def self.process_result(relation, result, **) relation.groupdate_values.reverse.each do |gv| result = gv.perform(relation, result, default_value: [:default_value]) end result end |
.resolve_column(relation, column) ⇒ Object
resolves eagerly need to convert both where_clause (easy) and group_clause (not easy) if want to avoid this
246 247 248 249 250 |
# File 'lib/groupdate/magic.rb', line 246 def resolve_column(relation, column) node = relation.send(:relation).send(:arel_columns, [column]).first node = Arel::Nodes::SqlLiteral.new(node) if node.is_a?(String) relation.connection.visitor.accept(node, Arel::Collectors::SQLString.new).value end |
.validate_column(column) ⇒ Object
basic version of Active Record disallow_raw_sql! symbol = column (safe), Arel node = SQL (safe), other = untrusted matches table.column and column
233 234 235 236 237 238 239 240 241 |
# File 'lib/groupdate/magic.rb', line 233 def validate_column(column) unless column.is_a?(Symbol) || column.is_a?(Arel::Nodes::SqlLiteral) column = column.to_s unless /\A\w+(\.\w+)?\z/i.match(column) raise ActiveRecord::UnknownAttributeReference, "Query method called with non-attribute argument(s): #{column.inspect}. Use Arel.sql() for known-safe values." end end column end |
Instance Method Details
#cast_method ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/groupdate/magic.rb', line 141 def cast_method @cast_method ||= begin case period when :minute_of_hour, :hour_of_day, :day_of_month, :day_of_year, :month_of_year lambda { |k| k.to_i } when :day_of_week lambda { |k| (k.to_i - 1 - week_start) % 7 } when :day, :week, :month, :quarter, :year if day_start != 0 day_start_hour = day_start / 3600 day_start_min = (day_start % 3600) / 60 day_start_sec = (day_start % 3600) % 60 lambda { |k| k.in_time_zone(time_zone).change(hour: day_start_hour, min: day_start_min, sec: day_start_sec) } else lambda { |k| k.in_time_zone(time_zone) } end else utc = ActiveSupport::TimeZone["UTC"] lambda { |k| (k.is_a?(String) || !k.respond_to?(:to_time) ? utc.parse(k.to_s) : k.to_time).in_time_zone(time_zone) } end end end |
#cast_result(result, multiple_groups) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/groupdate/magic.rb', line 164 def cast_result(result, multiple_groups) new_result = {} result.each do |k, v| if multiple_groups k[group_index] = cast_method.call(k[group_index]) else k = cast_method.call(k) end new_result[k] = v end new_result end |
#check_nils(result, multiple_groups, relation) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/groupdate/magic.rb', line 187 def check_nils(result, multiple_groups, relation) has_nils = multiple_groups ? (result.keys.first && result.keys.first[group_index].nil?) : result.key?(nil) if has_nils if time_zone_support?(relation) raise Groupdate::Error, "Invalid query - be sure to use a date or time column" else raise Groupdate::Error, "Database missing time zone support for #{time_zone.tzinfo.name} - see https://github.com/ankane/groupdate#for-mysql" end end end |
#perform(relation, result, default_value:) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/groupdate/magic.rb', line 127 def perform(relation, result, default_value:) multiple_groups = relation.group_values.size > 1 check_nils(result, multiple_groups, relation) result = cast_result(result, multiple_groups) series_builder.generate( result, default_value: .key?(:default_value) ? [:default_value] : default_value, multiple_groups: multiple_groups, group_index: group_index ) end |
#time_zone_support?(relation) ⇒ Boolean
177 178 179 180 181 182 183 184 185 |
# File 'lib/groupdate/magic.rb', line 177 def time_zone_support?(relation) if relation.connection.adapter_name =~ /mysql/i # need to call klass for Rails < 5.2 sql = relation.klass.send(:sanitize_sql_array, ["SELECT CONVERT_TZ(NOW(), '+00:00', ?)", time_zone.tzinfo.name]) !relation.connection.select_all(sql).first.values.first.nil? else true end end |