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

Added name validation to query & dashboard #297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions app/models/blazer/dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module Blazer
class Dashboard < Record

include ::Blazer::NameValidator

belongs_to :creator, optional: true, class_name: Blazer.user_class.to_s if Blazer.user_class
has_many :dashboard_queries, dependent: :destroy
has_many :queries, through: :dashboard_queries

validates :name, presence: true

def variables
queries.flat_map { |q| q.variables }.uniq
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/blazer/query.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Blazer
class Query < Record

include ::Blazer::NameValidator

belongs_to :creator, optional: true, class_name: Blazer.user_class.to_s if Blazer.user_class
has_many :checks, dependent: :destroy
has_many :dashboard_queries, dependent: :destroy
Expand Down
21 changes: 21 additions & 0 deletions app/models/concerns/blazer/name_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Blazer
module NameValidator
extend ActiveSupport::Concern

included do
before_validation :strip_unwanted_whitespaces

validates :name, presence: true,
uniqueness: {
scope: :creator_id,
case_sensitive: false,
message: "already taken for this user."
}
end

private
def strip_unwanted_whitespaces
self.name = "#{self.name}".strip
end
end
end