diff --git a/app/models/.keep b/app/models/.keep deleted file mode 100644 index e69de29..0000000 --- a/app/models/.keep +++ /dev/null diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29..0000000 --- a/app/models/concerns/.keep +++ /dev/null diff --git a/app/models/concerns/kalibro_record.rb b/app/models/concerns/kalibro_record.rb new file mode 100644 index 0000000..cdf1479 --- /dev/null +++ b/app/models/concerns/kalibro_record.rb @@ -0,0 +1,25 @@ +module KalibroRecord + extend ActiveSupport::Concern + + include ActiveModel::Validations + include ActiveModel::Conversion + extend ActiveModel::Naming + delegate :url_helpers, to: 'Rails.application.routes' + + def persisted? + Project.exists?(self.id) unless self.id.nil? + end + + def update(attributes = {}) + attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } + self.save + end + + def save + if self.valid? and self.kalibro_errors.empty? + super + else + false + end + end +end \ No newline at end of file diff --git a/app/models/project.rb b/app/models/project.rb index 9a7f832..1772986 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1,32 +1,12 @@ require "validators/kalibro_uniqueness_validator.rb" class Project < KalibroEntities::Entities::Project - include ActiveModel::Validations - include ActiveModel::Conversion - extend ActiveModel::Naming - delegate :url_helpers, to: 'Rails.application.routes' + include KalibroRecord attr_accessor :name validates :name, presence: true, kalibro_uniqueness: true - def persisted? - Project.exists?(self.id) unless self.id.nil? - end - - def update(attributes = {}) - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } - self.save - end - def self.latest(count = 1) all.sort { |a,b| b.id <=> a.id }.first(count) end - - def save - if self.valid? and self.kalibro_errors.empty? - super - else - false - end - end end diff --git a/app/models/validators/kalibro_uniqueness_validator.rb b/app/models/validators/kalibro_uniqueness_validator.rb index cc5ba13..b1fc9e8 100644 --- a/app/models/validators/kalibro_uniqueness_validator.rb +++ b/app/models/validators/kalibro_uniqueness_validator.rb @@ -2,7 +2,7 @@ class KalibroUniquenessValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) record.class.all.each do |entity| if entity.send(attribute) == value - record.errors[:attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." + record.errors[attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." break end end -- libgit2 0.21.2