Initial commit
This commit is contained in:
66
app/api/v2/queries/activity_filter.rb
Normal file
66
app/api/v2/queries/activity_filter.rb
Normal file
@@ -0,0 +1,66 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# queries helping module
|
||||
module API::V2::Queries
|
||||
class ActivityFilter
|
||||
attr_accessor :initial_scope
|
||||
|
||||
# initialize query
|
||||
def initialize(initial_scope)
|
||||
@initial_scope = initial_scope
|
||||
end
|
||||
|
||||
# returns query with with all applied filters
|
||||
def call(params)
|
||||
params[:with_user] ? @initial_scope = @initial_scope.joins(:user) : @initial_scope
|
||||
|
||||
scoped = filter_by_date(@initial_scope, params[:from], params[:to])
|
||||
scoped = filter_by_topic(scoped, params[:topic])
|
||||
scoped = filter_by_action(scoped, params[:action])
|
||||
scoped = filter_by_result(scoped, params[:result])
|
||||
scoped = filter_by_uid(scoped, params[:uid])
|
||||
scoped = filter_by_email(scoped, params[:email])
|
||||
scoped = filter_by_target(scoped, params[:target_uid])
|
||||
scoped = scoped.order('activities.id' => 'DESC') if params[:ordered]
|
||||
scoped
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# adds where(activities.created_at > from and activities.created_at < to) to query
|
||||
def filter_by_date(scoped, from = nil, to = nil)
|
||||
updated_scope = from ? scoped.where('activities.created_at >= ?', Time.at(from.to_i)) : scoped
|
||||
to ? updated_scope.where('activities.created_at <= ?', Time.at(to.to_i)) : updated_scope
|
||||
end
|
||||
|
||||
# adds where(activities.topic = topic) to query
|
||||
def filter_by_topic(scoped, topic = nil)
|
||||
topic ? scoped.where(activities: { topic: topic }) : scoped
|
||||
end
|
||||
|
||||
# adds where(activities.action = action) to query
|
||||
def filter_by_action(scoped, action = nil)
|
||||
action ? scoped.where(activities: { action: action }) : scoped
|
||||
end
|
||||
|
||||
# adds where(activities.result = result) to query
|
||||
def filter_by_result(scoped, result = nil)
|
||||
result ? scoped.where(activities: { result: result }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.uid = uid) to query
|
||||
def filter_by_uid(scoped, uid = nil)
|
||||
uid ? scoped.where(users: { uid: uid }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.email = email) to query
|
||||
def filter_by_email(scoped, email = nil)
|
||||
email ? scoped.where(users: { email: email }) : scoped
|
||||
end
|
||||
|
||||
# adds where(activities.target_uid = target_uid) to query
|
||||
def filter_by_target(scoped, target_uid = nil)
|
||||
target_uid ? scoped.where(activities: { target_uid: target_uid }) : scoped
|
||||
end
|
||||
end
|
||||
end
|
||||
64
app/api/v2/queries/user_filter.rb
Normal file
64
app/api/v2/queries/user_filter.rb
Normal file
@@ -0,0 +1,64 @@
|
||||
# queries helping module
|
||||
module API::V2::Queries
|
||||
class UserFilter
|
||||
attr_accessor :initial_scope
|
||||
|
||||
# initialize query to get User.all
|
||||
def initialize(initial_scope)
|
||||
@initial_scope = initial_scope.left_outer_joins(:profiles)
|
||||
end
|
||||
|
||||
# returns query with with all applied filters
|
||||
def call(params)
|
||||
scoped = filter_by_date(initial_scope, params[:range], params[:from], params[:to])
|
||||
scoped = filter_by_uid(scoped, params[:uid])
|
||||
scoped = filter_by_email(scoped, params[:email])
|
||||
scoped = filter_by_role(scoped, params[:role])
|
||||
scoped = filter_by_country(scoped, params[:country])
|
||||
scoped = filter_by_level(scoped, params[:level])
|
||||
scoped = filter_by_state(scoped, params[:state])
|
||||
scoped
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# adds where(users.[created, updated]_at > from and users.[created, updated]_at < to) to query
|
||||
def filter_by_date(scoped, range = 'created', from = nil, to = nil)
|
||||
newer_than_sql = "users.#{range}_at >= ?"
|
||||
older_than_sql = "users.#{range}_at <= ?"
|
||||
|
||||
updated_scope = from ? scoped.where(newer_than_sql, Time.at(from.to_i)) : scoped
|
||||
to ? updated_scope.where(older_than_sql, Time.at(to.to_i)) : updated_scope
|
||||
end
|
||||
|
||||
# adds where(users.uid = uid) to query
|
||||
def filter_by_uid(scoped, uid = nil)
|
||||
uid ? scoped.where(users: { uid: uid }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.email = email) to query
|
||||
def filter_by_email(scoped, email = nil)
|
||||
email ? scoped.where(users: { email: email }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.role = role) to query
|
||||
def filter_by_role(scoped, role = nil)
|
||||
role ? scoped.where(users: { role: role }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.level = level) to query
|
||||
def filter_by_level(scoped, level = nil)
|
||||
level ? scoped.where(users: { level: level }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.state = state) to query
|
||||
def filter_by_state(scoped, state = nil)
|
||||
state ? scoped.where(users: { state: state }) : scoped
|
||||
end
|
||||
|
||||
# adds where(users.country = country) to query
|
||||
def filter_by_country(scoped, country = nil)
|
||||
country ? scoped.where(profiles: { country: country }) : scoped
|
||||
end
|
||||
end
|
||||
end
|
||||
47
app/api/v2/queries/user_with_label_filter.rb
Normal file
47
app/api/v2/queries/user_with_label_filter.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
# queries helping module
|
||||
module API::V2::Queries
|
||||
class UserWithLabelFilter
|
||||
attr_accessor :initial_scope
|
||||
|
||||
# initialize query to get User.all
|
||||
def initialize(initial_scope)
|
||||
@initial_scope = initial_scope.left_outer_joins(:labels)
|
||||
end
|
||||
|
||||
# returns query with with all applied filters
|
||||
def call(params)
|
||||
scoped = filter_by_date(initial_scope, params[:range], params[:from], params[:to])
|
||||
scoped = filter_by_key(scoped, params[:key])
|
||||
scoped = filter_by_value(scoped, params[:value])
|
||||
scoped = filter_by_scope(scoped, params[:scope])
|
||||
|
||||
scoped
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# adds where(labels.[created, updated]_at > from and labels.[created, updated]_at < to) to query
|
||||
def filter_by_date(scoped, range = 'created', from = nil, to = nil)
|
||||
newer_than_sql = "labels.#{range}_at >= ?"
|
||||
older_than_sql = "labels.#{range}_at <= ?"
|
||||
|
||||
updated_scope = from ? scoped.where(newer_than_sql, Time.at(from.to_i)) : scoped
|
||||
to ? updated_scope.where(older_than_sql, Time.at(to.to_i)) : updated_scope
|
||||
end
|
||||
|
||||
# adds where(labels.key = key) to query
|
||||
def filter_by_key(scoped, key = nil)
|
||||
key ? scoped.where(labels: { key: key }) : scoped
|
||||
end
|
||||
|
||||
# adds where(labels.value = value) to query
|
||||
def filter_by_value(scoped, value = nil)
|
||||
value ? scoped.where(labels: { value: value }) : scoped
|
||||
end
|
||||
|
||||
# adds where(labels.scope = scope) to query
|
||||
def filter_by_scope(scoped, scope = nil)
|
||||
scope ? scoped.where(labels: { scope: scope }) : scoped
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user