Module: HotGlue::ControllerHelper
- Defined in:
- app/helpers/hot_glue/controller_helper.rb
Instance Method Summary collapse
- #association_constructor(field, search) ⇒ Object
- #boolean_modified_datetime_constructor(field, search) ⇒ Object
- #boolean_query_constructor(field, search) ⇒ Object
- #current_timezone ⇒ Object
- #date_field_localized(form_object, field_name, value, **args) ⇒ Object
- #date_query_constructor(field, match, search_start, search_end) ⇒ Object
- #date_to_current_timezone(date, timezone = nil) ⇒ Object
- #datetime_field_localized(form_object, field_name, value, **args) ⇒ Object
- #enum_constructor(field_name, value, **args) ⇒ Object
- #format_timezone_offset(hour, minute) ⇒ Object
- #formatted_time_display(object, method, current_user) ⇒ Object
- #formatted_time_field(object, method, current_user) ⇒ Object
- #hawk_params(hawk_schema, modified_params) ⇒ Object
- #integer_query_constructor(match, search) ⇒ Object
- #is_dst_now? ⇒ Boolean
- #modify_date_inputs_on_params(modified_params, current_user_object = nil, field_list = {}) ⇒ Object
- #string_query_constructor(match, search) ⇒ Object
- #time_field_localized(form_object, field_name, value, **args) ⇒ Object
- #time_query_constructor(field, match, search_start, search_end) ⇒ Object
- #timezonize(tz) ⇒ Object
Instance Method Details
#association_constructor(field, search) ⇒ Object
298 299 300 301 302 303 304 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 298 def association_constructor(field, search) unless search.blank? ["#{field} = ?", search] else nil end end |
#boolean_modified_datetime_constructor(field, search) ⇒ Object
314 315 316 317 318 319 320 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 314 def boolean_modified_datetime_constructor(field,search) unless search == '-1' ["#{field} #{search == '0' ? 'IS NULL' : 'IS NOT NULL'}"] else nil end end |
#boolean_query_constructor(field, search) ⇒ Object
306 307 308 309 310 311 312 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 306 def boolean_query_constructor(field, search) unless search == "-1" ["#{field} IS #{search ? 'TRUE' : 'FALSE'}"] else nil end end |
#current_timezone ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 44 def current_timezone # returns a TimeZone (https://apidock.com/rails/TimeZone) object if defined?(current_user) if current_user.try(:timezone) ActiveSupport::TimeZone[current_user.timezone] else Rails.application.config.time_zone # Time.zone.name end else Rails.application.config.time_zone # Time.zone.name end end |
#date_field_localized(form_object, field_name, value, **args) ⇒ Object
23 24 25 26 27 28 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 23 def date_field_localized(form_object, field_name, value, **args) form_object.text_field(field_name, args.merge({class: 'form-control', type: 'date', value: value })) end |
#date_query_constructor(field, match, search_start, search_end) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 249 def date_query_constructor(field, match, search_start, search_end) if match.blank? nil elsif ['is_on', 'not_on'].include?(match) && search_start.blank? nil elsif ['is_on_or_after','is_between'].include?(match) && (search_start.blank? ) nil elsif ['is_before_or_on'].include?(match) && (search_end.blank? ) nil elsif ['is_between'].include?(match) && (search_start.blank? || search_end.blank? ) nil else case match when 'is_on' ["#{field} = ?", search_start] when 'is_on_or_after' ["#{field} = ? OR #{field} > ?", search_start, search_start] when "is_before_or_on" ["#{field} = ? OR #{field} < ?", search_end, search_end] when "is_between" ["#{field} BETWEEN ? AND ?", search_start, search_end] when "not_on" ["#{field} != ?", search_start] end end end |
#date_to_current_timezone(date, timezone = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 102 def date_to_current_timezone(date, timezone = nil) # used for displaying when in EDIT mode # (this format is how the browser expectes to receive the value='' of the input field) if date.nil? return nil else return date.in_time_zone(timezone).strftime("%Y-%m-%dT%H:%M") end end |
#datetime_field_localized(form_object, field_name, value, **args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 8 def datetime_field_localized(form_object, field_name, value, **args ) current_timezone args = args.merge({class: 'form-control', type: 'datetime-local' }) if !value.nil? args[:value] = date_to_current_timezone(value, current_timezone) + timezonize(current_timezone) end form_object.text_field(field_name, args) end |
#enum_constructor(field_name, value, **args) ⇒ Object
243 244 245 246 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 243 def enum_constructor(field_name, value, **args) return nil if value.blank? ["#{field_name} = ?", value] end |
#format_timezone_offset(hour, minute) ⇒ Object
116 117 118 119 120 121 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 116 def format_timezone_offset(hour, minute) sign = hour < 0 ? "-" : "+" hour_abs = hour.abs.to_s.rjust(2, '0') minute_str = minute.to_s.rjust(2, '0') "#{sign}#{hour_abs}#{minute_str}" end |
#formatted_time_display(object, method, current_user) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 59 def formatted_time_display(object, method, current_user) tz = ActiveSupport::TimeZone[current_user.timezone] t = object.public_send(method) # Build UTC datetime for today + stored time utc_datetime = Time.utc( Time.now.year, Time.now.month, Time.now.day, t.hour, t.min, t.sec ) # Convert to user's timezone (DST-aware) local_time = utc_datetime.in_time_zone(tz) local_time.strftime('%-l:%M %p %Z') end |
#formatted_time_field(object, method, current_user) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 80 def formatted_time_field(object, method, current_user) tz = ActiveSupport::TimeZone[current_user.timezone] t = object.public_send(method) # Build UTC datetime from the stored time utc_datetime = Time.utc( Time.now.year, Time.now.month, Time.now.day, t.hour, t.min, t.sec ) # Convert to user's timezone (DST-aware) local_time = utc_datetime.in_time_zone(tz) # Format for HTML5 <input type="time"> (24h clock, HH:MM) local_time.strftime('%H:%M') end |
#hawk_params(hawk_schema, modified_params) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 169 def hawk_params(hawk_schema, modified_params) @hawk_alarm = +"" hawk_schema.each do |hawk_key, hawk_definition| if hawk_definition[0].to_s.start_with?("[") # the hawk is polymorphic # hawk_definition[0] is like "[account.companies,account.vc_firms]" scopes = hawk_definition[0].to_s.gsub(/^\[|\]$/, "").split(",").map(&:strip) unless modified_params[hawk_key.to_s].blank? passed = scopes.any? do |scope_str| relation = scope_str.split(".").inject(self) { |obj, method_name| obj.send(method_name) } relation.where(id: modified_params[hawk_key.to_s]).exists? end unless passed @hawk_alarm << "You aren't allowed to set #{hawk_key.to_s} to #{modified_params[hawk_key.to_s]}. " modified_params.tap { |hs| hs.delete(hawk_key.to_s) } end end else hawk_root = hawk_definition[0] unless modified_params[hawk_key.to_s].blank? begin hawk_definition.where(modified_params[hawk_key.to_s]) rescue ActiveRecord::RecordNotFound => e @hawk_alarm << "You aren't allowed to set #{hawk_key.to_s} to #{modified_params[hawk_key.to_s]}. " modified_params.tap { |hs| hs.delete(hawk_key.to_s) } end end end end modified_params end |
#integer_query_constructor(match, search) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 222 def integer_query_constructor(match, search) if match.blank? || search.blank? nil else case match when '=' "= #{search.to_i}" when '≥' ">= #{search.to_i}" when '>' "> #{search.to_i}" when '≤' "<= #{search.to_i}" when '<' "< #{search.to_i}" else nil end end end |
#is_dst_now? ⇒ Boolean
112 113 114 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 112 def is_dst_now? ActiveSupport::TimeZone['Eastern Time (US & Canada)'].now.dst? end |
#modify_date_inputs_on_params(modified_params, current_user_object = nil, field_list = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 123 def modify_date_inputs_on_params(modified_params, current_user_object = nil, field_list = {}) use_timezone = if current_user_object.try(:timezone) ActiveSupport::TimeZone[current_user_object.timezone] else Time.zone end modified_params.tap do |params| params.keys.each do |k| include_me = if field_list.is_a?(Hash) field_list[k.to_sym].present? elsif field_list.is_a?(Array) field_list.include?(k.to_sym) end next unless include_me && params[k].present? input_value = params[k].gsub("T", " ") # "13:00" or "2025-09-24 13:00" case field_list[k.to_sym] when :datetime # Interpret input as in user's local time zone local_time = use_timezone.strptime(input_value, "%Y-%m-%d %H:%M") # Convert to UTC for storage parsed_time = local_time.utc.change(sec: 0) when :time # Parse as HH:MM (local wall clock time) t = Time.strptime(input_value, "%H:%M") # Interpret in user's timezone, with today's date local_time = use_timezone.local(Time.now.year, Time.now.month, Time.now.day, t.hour, t.min, 0) # Convert to UTC for storage parsed_time = local_time.utc else next end Rails.logger.info "input_value: #{input_value}" Rails.logger.info "parsed_time: #{parsed_time} (#{parsed_time.zone})" params[k] = parsed_time end end end |
#string_query_constructor(match, search) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 203 def string_query_constructor(match, search) if match.blank? || search.blank? nil else case match when 'contains' "%#{search}%" when 'is_exactly' "#{search}" when 'starts_with' "#{search}%" when 'ends_with' "%#{search}" else nil end end end |
#time_field_localized(form_object, field_name, value, **args) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 30 def time_field_localized(form_object, field_name, value, **args ) current_timezone form_object.text_field(field_name, args.merge({class: 'form-control', type: 'time', value: value })) end |
#time_query_constructor(field, match, search_start, search_end) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 276 def time_query_constructor(field, match, search_start, search_end) if match.blank? nil elsif ['is_at'].include?(match) && search_start.blank? nil elsif ['is_ar_or_after', 'is_before_or_at', 'is_between'].include?(match) && (search_start.blank? || search_end.blank?) nil else case match when 'is_at_exactly' ["EXTRACT(HOUR FROM #{field}) = ? AND EXTRACT(MINUTE FROM #{field}) = ? ", search_start.split(":")[0], search_start.split(":")[1]] # when 'is_at_or_after' # ["#{field} = ? OR #{field} > ?", search_start, search_start] # when "is_before_or_at" # ["#{field} = ? OR #{field} < ?", search_end, search_end] # when "is_between" # ["#{field} BETWEEN ? AND ?", search_start, search_end] end end end |
#timezonize(tz) ⇒ Object
3 4 5 6 |
# File 'app/helpers/hot_glue/controller_helper.rb', line 3 def timezonize(tz) tz = tz.to_i (tz >= 0 ? "+" : "-") + sprintf('%02d',tz.abs) + ":00" end |