Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scopes for MiqRequest #16843

Merged
merged 2 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion app/models/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class MiqRequest < ApplicationRecord

include MiqRequestMixin

scope :created_recently, ->(days_ago) { where("created_on > ?", days_ago.days.ago) }
scope :with_approval_state, ->(state) { where(:approval_state => state) }
scope :with_type, ->(type) { where(:type => type) }
scope :with_request_type, ->(type) { where(:request_type => type) }
scope :with_requester, ->(id) { where(:requester_id => User.with_same_userid(id).collect(&:id)) }

MODEL_REQUEST_TYPES = {
:Service => {
:MiqProvisionRequest => {
Expand Down Expand Up @@ -91,7 +97,7 @@ class MiqRequest < ApplicationRecord
:automation => N_("Automation")
}
}
}
}.freeze

REQUEST_TYPES_BACKEND_ONLY = {:MiqProvisionRequestTemplate => {:template => "VM Provision Template"}}
REQUEST_TYPES = MODEL_REQUEST_TYPES.values.each_with_object(REQUEST_TYPES_BACKEND_ONLY) { |i, h| i.each { |k, v| h[k] = v } }
Expand All @@ -105,6 +111,12 @@ def self.class_from_request_data(data)
model_symbol.to_s.constantize
end

def self.with_reason_like(reason)
# Reason string uses * wildcard, scope is required to convert it into % wildcard for LIKE statement
reason = reason.match(/\A(?<start>\*?)(?<content>.*?)(?<end>\*?)\z/)
where("reason LIKE (?)", "#{reason[:start] ? '%' : ''}#{sanitize_sql_like(reason[:content])}#{reason[:end] ? '%' : ''}")
end

# Supports old-style requests where specific request was a seperate table connected as a resource
def resource
self
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class User < ApplicationRecord
serialize :settings, Hash # Implement settings column as a hash
default_value_for(:settings) { Hash.new }

scope :with_same_userid, ->(id) { where(:userid => User.find(id).userid) }

def self.with_allowed_roles_for(user_or_group)
includes(:miq_groups => :miq_user_role).where.not(:miq_user_roles => {:name => user_or_group.disallowed_roles})
end
Expand Down