Commit fc94859e1d8cb11155b4931fe8cd2c0825daf592
1 parent
ccb58e65
Exists in
colab
and in
4 other branches
Refatored code that will be used in every entity into a module: KalibroRecord
Showing
5 changed files
with
27 additions
and
22 deletions
Show diff stats
app/models/.keep
app/models/concerns/.keep
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
1 | +module KalibroRecord | ||
2 | + extend ActiveSupport::Concern | ||
3 | + | ||
4 | + include ActiveModel::Validations | ||
5 | + include ActiveModel::Conversion | ||
6 | + extend ActiveModel::Naming | ||
7 | + delegate :url_helpers, to: 'Rails.application.routes' | ||
8 | + | ||
9 | + def persisted? | ||
10 | + Project.exists?(self.id) unless self.id.nil? | ||
11 | + end | ||
12 | + | ||
13 | + def update(attributes = {}) | ||
14 | + attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } | ||
15 | + self.save | ||
16 | + end | ||
17 | + | ||
18 | + def save | ||
19 | + if self.valid? and self.kalibro_errors.empty? | ||
20 | + super | ||
21 | + else | ||
22 | + false | ||
23 | + end | ||
24 | + end | ||
25 | +end | ||
0 | \ No newline at end of file | 26 | \ No newline at end of file |
app/models/project.rb
1 | require "validators/kalibro_uniqueness_validator.rb" | 1 | require "validators/kalibro_uniqueness_validator.rb" |
2 | 2 | ||
3 | class Project < KalibroEntities::Entities::Project | 3 | class Project < KalibroEntities::Entities::Project |
4 | - include ActiveModel::Validations | ||
5 | - include ActiveModel::Conversion | ||
6 | - extend ActiveModel::Naming | ||
7 | - delegate :url_helpers, to: 'Rails.application.routes' | 4 | + include KalibroRecord |
8 | 5 | ||
9 | attr_accessor :name | 6 | attr_accessor :name |
10 | validates :name, presence: true, kalibro_uniqueness: true | 7 | validates :name, presence: true, kalibro_uniqueness: true |
11 | 8 | ||
12 | - def persisted? | ||
13 | - Project.exists?(self.id) unless self.id.nil? | ||
14 | - end | ||
15 | - | ||
16 | - def update(attributes = {}) | ||
17 | - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } | ||
18 | - self.save | ||
19 | - end | ||
20 | - | ||
21 | def self.latest(count = 1) | 9 | def self.latest(count = 1) |
22 | all.sort { |a,b| b.id <=> a.id }.first(count) | 10 | all.sort { |a,b| b.id <=> a.id }.first(count) |
23 | end | 11 | end |
24 | - | ||
25 | - def save | ||
26 | - if self.valid? and self.kalibro_errors.empty? | ||
27 | - super | ||
28 | - else | ||
29 | - false | ||
30 | - end | ||
31 | - end | ||
32 | end | 12 | end |
app/models/validators/kalibro_uniqueness_validator.rb
@@ -2,7 +2,7 @@ class KalibroUniquenessValidator < ActiveModel::EachValidator | @@ -2,7 +2,7 @@ class KalibroUniquenessValidator < ActiveModel::EachValidator | ||
2 | def validate_each(record, attribute, value) | 2 | def validate_each(record, attribute, value) |
3 | record.class.all.each do |entity| | 3 | record.class.all.each do |entity| |
4 | if entity.send(attribute) == value | 4 | if entity.send(attribute) == value |
5 | - record.errors[:attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." | 5 | + record.errors[attribute] << "There's already a #{record.class} with #{attribute} #{value}! Please, choose another one." |
6 | break | 6 | break |
7 | end | 7 | end |
8 | end | 8 | end |