Skip to content
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
33 changes: 33 additions & 0 deletions app/models/custom_attributes/custom_attribute_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ module CustomAttributeDefinition
scope :by_sort_order, -> { order('sort_order ASC') }
validates :attr_name, presence: true, format: { with: /^[a-zA-Z\_\s]+$/, multiline: true, message: 'cannot contain special characters.' }
validates :default_value, format: { with: /^[\+\-]?\d*\.?\d*$/, multiline: true, message: 'should be a number.' }, if: :number_type?
validate :can_be_marked_as_unique

def number_type?
[CustomAttributes::CustomAttribute::TYPE_NUMBER, CustomAttributes::CustomAttribute::TYPE_DECIMAL].include?(attr_type)
end

def text_type?
attr_type == CustomAttributes::CustomAttribute::TYPE_TEXT
end

def boolean_type?
attr_type == CustomAttributes::CustomAttribute::TYPE_BOOLEAN
end
Expand All @@ -30,6 +35,7 @@ def save_custom_attribute(selected_option_label)
begin
transaction do
self.custom_attribute_options = [] unless has_options?
default_value = "" if unique?
save!
if has_options?
update! default_value: custom_attribute_options.find_by(label: selected_option_label).try(:id)
Expand All @@ -44,6 +50,7 @@ def save_custom_attribute(selected_option_label)
def update_custom_attribute(custom_attribute_params, selected_option_label)
begin
transaction do
custom_attribute_params[:default_value] = "" if unique?
update!(custom_attribute_params)
if has_options?
update! default_value: custom_attribute_options.find_by(label: selected_option_label).try(:id)
Expand All @@ -58,6 +65,32 @@ def update_custom_attribute(custom_attribute_params, selected_option_label)
def default_option
custom_attribute_options.find_by(id: default_value)
end

def can_be_marked_as_unique
if unique?
if !can_be_unique?
errors.add :base, 'Can not be made as unique!'
elsif has_duplicate_values?
errors.add :base, 'Remove duplicate values first to make is unique.'
end
end
errors.blank?
end

def can_be_unique?
text_type? || number_type?
end

def has_duplicate_values?
column = {
CustomAttributes::CustomAttribute::TYPE_TEXT => 'string_value',
CustomAttributes::CustomAttribute::TYPE_NUMBER => 'integer_value',
CustomAttributes::CustomAttribute::TYPE_DECIMAL => 'double_value',
}[attr_type]
return false if column.blank?
duplicate_cavs = custom_attribute_values.where("#{column} IS NOT NULL").select("#{column}, count(*) as values_count").group(column).having("count(*) > 1")
duplicate_cavs.present?
end
end
end
end
24 changes: 24 additions & 0 deletions app/models/custom_attributes/custom_attributes_utilities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module CustomAttributes
module CustomAttributesUtilities
extend ActiveSupport::Concern

included do
after_save :check_duplicate_custom_attributes
end

def check_duplicate_custom_attributes
return unless self.custom_attribute_defn.unique?
column_maping = {
"#{self.owner.class.name}CustomAttributeStringValue" => 'string_value',
"#{self.owner.class.name}CustomAttributeIntegerValue" => 'integer_value',
"#{self.owner.class.name}CustomAttributeDoubleValue" => 'double_value',
}

return if column_maping[self.class.name].blank?
if self.value.present?
duplicate_count = self.class.where("#{column_maping[self.class.name]} = ?", self.value).count
raise StandardError.new("Duplicate value for '#{self.custom_attribute_defn.attr_name}'") if duplicate_count > 1
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@

<% if new_record %>
<%= f.hidden_field :sort_order %>
<% end %>
<% end %>

<div class="clearfix"></div>
<h3>Preferences</h3>
<div class="clearfix"></div>

<div class='custom_attribute_required margin-bottom-55'>
<%= f.check_box :required, class: "ca_required ca_required_field", disabled: f.object.try(:boolean_type?) %> Make this mandatory <br/>
</div>

<div class='custom_attribute_unique margin-bottom-55'>
<% unique_restricted = !f.object.try(:can_be_unique?) || f.object.try(:has_duplicate_values?) %>
<%= f.check_box :unique, class: "ca_unique ca_unique_field", id: 'make_unique_checkbox', disabled: unique_restricted %> Make this unique <br/>
</div>
2 changes: 1 addition & 1 deletion lib/custom_attributes/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CustomAttributes
VERSION = "0.1.18"
VERSION = "0.1.19"
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class <%= name %>CustomAttributeDefinition < ActiveRecord::Base
include CustomAttributes::CustomAttributeDefinition
has_many :custom_attribute_values, dependent: :destroy, class_name: "<%= name %>CustomAttributeValue", foreign_key: "<%= singular_name %>_custom_attribute_definition_id"
has_many :custom_attribute_options, dependent: :destroy, class_name: "<%= name %>CustomAttributeOption", foreign_key: "<%= singular_name %>_custom_attribute_definition_id"
<% if options.tenant %>
validates :attr_name, uniqueness: { scope: :<%= options.tenant %>_id, message: " has already been taken." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def custom_attribute_params
:attr_name,
:attr_type,
:sort_order,
:required,
:unique,
custom_attribute_options_attributes: [
:id,
:label,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class <%= name %>CustomAttributeValue < ActiveRecord::Base
include CustomAttributes::CustomAttributeValue
include CustomAttributes::CustomAttributeValue, CustomAttributes::CustomAttributesUtilities
belongs_to :custom_attribute_defn, class_name: "<%= name %>CustomAttributeDefinition", foreign_key: "<%= singular_name %>_custom_attribute_definition_id"
belongs_to :owner, class_name: "<%= name %>", foreign_key: "<%= singular_name %>_id"
has_many :custom_attribute_options, through: :custom_attribute_defn
Expand Down
1 change: 1 addition & 0 deletions lib/generators/custom_attributes/templates/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def change
t.string :placeholder
t.integer :sort_order
t.boolean :required, default: false
t.boolean :unique, default: false
end
<% if options.tenant %>
t.integer :<%= options.tenant.underscore %>_id, foreign_key: true
Expand Down