Skip to content

Commit

Permalink
Allowing an attribute and its validations to be set at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
ajn committed Aug 4, 2011
1 parent 0cc4f54 commit bcc37a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/modest_model.rb
Expand Up @@ -7,5 +7,6 @@
module ModestModel module ModestModel
autoload :Base, File.expand_path('../modest_model/base', __FILE__) autoload :Base, File.expand_path('../modest_model/base', __FILE__)
autoload :Validators, File.expand_path('../modest_model/validators', __FILE__) autoload :Validators, File.expand_path('../modest_model/validators', __FILE__)
autoload :CombinedAttr, File.expand_path('../modest_model/combined_attr', __FILE__)
autoload :Resource, File.expand_path('../modest_model/resource', __FILE__) autoload :Resource, File.expand_path('../modest_model/resource', __FILE__)
end end
1 change: 1 addition & 0 deletions lib/modest_model/base.rb
Expand Up @@ -8,6 +8,7 @@ class Base
include ActiveModel::AttributeMethods include ActiveModel::AttributeMethods


include ModestModel::Validators include ModestModel::Validators
extend ModestModel::CombinedAttr


def initialize(attributes = {}, options={}) def initialize(attributes = {}, options={})
self.assign_attributes(attributes, options) self.assign_attributes(attributes, options)
Expand Down
10 changes: 10 additions & 0 deletions lib/modest_model/combined_attr.rb
@@ -0,0 +1,10 @@
module ModestModel
module CombinedAttr
def attribute *attrs
options = attrs.extract_options!
validations = options.slice!(*_validates_default_keys)
attributes *attrs
validates *(attrs +[validations]) if validations.any?
end
end
end
3 changes: 1 addition & 2 deletions test/fixtures/sample_model.rb
@@ -1,5 +1,4 @@
class SampleModel < ModestModel::Base class SampleModel < ModestModel::Base
attributes :name, :email attributes :name, :email
attributes :nickname attribute :nickname, :absence => true
validates :nickname, :absence => true
end end
10 changes: 6 additions & 4 deletions test/fixtures/sample_resource.rb
@@ -1,9 +1,11 @@
class SampleResource < ModestModel::Resource class SampleResource < ModestModel::Resource
attributes :id, :name, :email, :number attributes :id, :name, :email
attributes :nickname, :saved_at, :destroyed_at attributes :saved_at, :destroyed_at
attributes :find_callback, :create_callback, :save_callback, :update_callback, :destroy_callback attributes :find_callback, :create_callback, :save_callback, :update_callback, :destroy_callback
validates :nickname, :absence => true
validates :number, :numericality => {:allow_blank => true} # Attributes with validations
attribute :nickname, :absence => true
attribute :number, :numericality => {:allow_blank => true}


after_find :set_find_callback after_find :set_find_callback
def set_find_callback def set_find_callback
Expand Down

0 comments on commit bcc37a1

Please sign in to comment.