module ApiQueries::ClassMethods

class method

Public Instance Methods

api_q(opts={}) click to toggle source
# File lib/api_queries.rb, line 10
def api_q(opts={})
  # add default value
  opts[:column_date] = 'updated_at' unless opts[:column_date].present?

  # condition hash
  conditions = {}

  # check if specified column exists
  if column_names.include?(opts[:column_date])
    # last updated at q
    if opts[:q] == 'last_updated_at'
      return { last_updated_at: (begin
                                  order(opts[:column_date] => :desc).limit(1).first.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
                                rescue StandardError
                                  nil
                                end) }
    end

    # AFTER: updated_at > given_date
    if opts[:after].present?
      conditions = ["#{table_name}.#{opts[:column_date]} > ?", fdate(opts[:after])]
    # BEFORE: updated_at < given_date
    elsif opts[:before].present?
      conditions = ["#{table_name}.#{opts[:column_date]} < ?", fdate(opts[:before])]
    # FROM & TO: between "from date" to "to date"
    elsif opts[:from].present? && opts[:to].present?
      # conditions[opts[:column_date].to_sym] = (fdate(opts[:from])..fdate(opts[:to]))
      # conditions = ["#{self.table_name}.#{opts[:column_date]} >= ?"]
      conditions = { table_name.to_sym => { opts[:column_date].to_sym => fdate(opts[:from])..fdate(opts[:to]) } }

    # FROM: updated_at >= given_date
    elsif opts[:from].present?
      conditions = ["#{table_name}.#{opts[:column_date]} >= ?", fdate(opts[:from])]
    # TO: updated_at <= given_date
    elsif opts[:to].present?
      conditions = ["#{table_name}.#{opts[:column_date]} <= ?", fdate(opts[:to])]
    end
  else
    raise Errors::UnknownColumn, 'Invalid value for column_date.' unless opts[:column_date] == 'updated_at'
  end

  # get by status
  conditions[:status] = 'active' if opts[:active_only].to_s == '1' && conditions.is_a?(Hash)
  # return hash
  records = where(conditions)
  # get by status
  records = records.where(status: 'active') if opts[:active_only].to_s == '1' && conditions.is_a?(Array)

  if opts[:q] == 'count'
    { count: records.count }
  else
    records.order(id: :asc).paginate(page: opts[:page], per_page: 50)
  end
end

Private Instance Methods

fdate(date_string) click to toggle source
# File lib/api_queries.rb, line 67
def fdate(date_string)
  # Date.strptime(date_string, '%Y-%m-%dT%H:%M:%SZ') # YYYY-MM-DDTHH:MM:SSZ
  date_string.to_datetime
end