diff options
Diffstat (limited to 'activemodel/test/models')
-rw-r--r-- | activemodel/test/models/account.rb | 5 | ||||
-rw-r--r-- | activemodel/test/models/blog_post.rb | 9 | ||||
-rw-r--r-- | activemodel/test/models/contact.rb | 26 | ||||
-rw-r--r-- | activemodel/test/models/custom_reader.rb | 15 | ||||
-rw-r--r-- | activemodel/test/models/helicopter.rb | 7 | ||||
-rw-r--r-- | activemodel/test/models/person.rb | 17 | ||||
-rw-r--r-- | activemodel/test/models/person_with_validator.rb | 24 | ||||
-rw-r--r-- | activemodel/test/models/project.rb | 3 | ||||
-rw-r--r-- | activemodel/test/models/reply.rb | 32 | ||||
-rw-r--r-- | activemodel/test/models/sheep.rb | 3 | ||||
-rw-r--r-- | activemodel/test/models/topic.rb | 40 | ||||
-rw-r--r-- | activemodel/test/models/track_back.rb | 11 | ||||
-rw-r--r-- | activemodel/test/models/user.rb | 10 | ||||
-rw-r--r-- | activemodel/test/models/visitor.rb | 10 |
14 files changed, 212 insertions, 0 deletions
diff --git a/activemodel/test/models/account.rb b/activemodel/test/models/account.rb new file mode 100644 index 0000000000..eed668d38f --- /dev/null +++ b/activemodel/test/models/account.rb @@ -0,0 +1,5 @@ +class Account + include ActiveModel::ForbiddenAttributesProtection + + public :sanitize_for_mass_assignment +end diff --git a/activemodel/test/models/blog_post.rb b/activemodel/test/models/blog_post.rb new file mode 100644 index 0000000000..46eba857df --- /dev/null +++ b/activemodel/test/models/blog_post.rb @@ -0,0 +1,9 @@ +module Blog + def self.use_relative_model_naming? + true + end + + class Post + extend ActiveModel::Naming + end +end diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb new file mode 100644 index 0000000000..c25be28e1d --- /dev/null +++ b/activemodel/test/models/contact.rb @@ -0,0 +1,26 @@ +class Contact + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_accessor :id, :name, :age, :created_at, :awesome, :preferences + + def social + %w(twitter github) + end + + def network + { git: :github } + end + + def initialize(options = {}) + options.each { |name, value| send("#{name}=", value) } + end + + def pseudonyms + nil + end + + def persisted? + id + end +end diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb new file mode 100644 index 0000000000..2fbcf79c3d --- /dev/null +++ b/activemodel/test/models/custom_reader.rb @@ -0,0 +1,15 @@ +class CustomReader + include ActiveModel::Validations + + def initialize(data = {}) + @data = data + end + + def []=(key, value) + @data[key] = value + end + + def read_attribute_for_validation(key) + @data[key] + end +end
\ No newline at end of file diff --git a/activemodel/test/models/helicopter.rb b/activemodel/test/models/helicopter.rb new file mode 100644 index 0000000000..933f3c463a --- /dev/null +++ b/activemodel/test/models/helicopter.rb @@ -0,0 +1,7 @@ +class Helicopter + include ActiveModel::Conversion +end + +class Helicopter::Comanche + include ActiveModel::Conversion +end diff --git a/activemodel/test/models/person.rb b/activemodel/test/models/person.rb new file mode 100644 index 0000000000..e896e90f98 --- /dev/null +++ b/activemodel/test/models/person.rb @@ -0,0 +1,17 @@ +class Person + include ActiveModel::Validations + extend ActiveModel::Translation + + attr_accessor :title, :karma, :salary, :gender + + def condition_is_true + true + end +end + +class Person::Gender + extend ActiveModel::Translation +end + +class Child < Person +end diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb new file mode 100644 index 0000000000..505ed880c1 --- /dev/null +++ b/activemodel/test/models/person_with_validator.rb @@ -0,0 +1,24 @@ +class PersonWithValidator + include ActiveModel::Validations + + class PresenceValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank? + end + end + + class LikeValidator < ActiveModel::EachValidator + def initialize(options) + @with = options[:with] + super + end + + def validate_each(record, attribute, value) + unless value[@with] + record.errors.add attribute, "does not appear to be like #{@with}" + end + end + end + + attr_accessor :title, :karma +end diff --git a/activemodel/test/models/project.rb b/activemodel/test/models/project.rb new file mode 100644 index 0000000000..581b6dc0b3 --- /dev/null +++ b/activemodel/test/models/project.rb @@ -0,0 +1,3 @@ +class Project + include ActiveModel::DeprecatedMassAssignmentSecurity +end diff --git a/activemodel/test/models/reply.rb b/activemodel/test/models/reply.rb new file mode 100644 index 0000000000..b77910e671 --- /dev/null +++ b/activemodel/test/models/reply.rb @@ -0,0 +1,32 @@ +require 'models/topic' + +class Reply < Topic + validate :errors_on_empty_content + validate :title_is_wrong_create, on: :create + + validate :check_empty_title + validate :check_content_mismatch, on: :create + validate :check_wrong_update, on: :update + + def check_empty_title + errors[:title] << "is Empty" unless title && title.size > 0 + end + + def errors_on_empty_content + errors[:content] << "is Empty" unless content && content.size > 0 + end + + def check_content_mismatch + if title && content && content == "Mismatch" + errors[:title] << "is Content Mismatch" + end + end + + def title_is_wrong_create + errors[:title] << "is Wrong Create" if title && title == "Wrong Create" + end + + def check_wrong_update + errors[:title] << "is Wrong Update" if title && title == "Wrong Update" + end +end diff --git a/activemodel/test/models/sheep.rb b/activemodel/test/models/sheep.rb new file mode 100644 index 0000000000..7aba055c4f --- /dev/null +++ b/activemodel/test/models/sheep.rb @@ -0,0 +1,3 @@ +class Sheep + extend ActiveModel::Naming +end diff --git a/activemodel/test/models/topic.rb b/activemodel/test/models/topic.rb new file mode 100644 index 0000000000..1411a093e9 --- /dev/null +++ b/activemodel/test/models/topic.rb @@ -0,0 +1,40 @@ +class Topic + include ActiveModel::Validations + include ActiveModel::Validations::Callbacks + + def self._validates_default_keys + super | [ :message ] + end + + attr_accessor :title, :author_name, :content, :approved, :created_at + attr_accessor :after_validation_performed + + after_validation :perform_after_validation + + def initialize(attributes = {}) + attributes.each do |key, value| + send "#{key}=", value + end + end + + def condition_is_true + true + end + + def condition_is_true_but_its_not + false + end + + def perform_after_validation + self.after_validation_performed = true + end + + def my_validation + errors.add :title, "is missing" unless title + end + + def my_validation_with_arg(attr) + errors.add attr, "is missing" unless send(attr) + end + +end diff --git a/activemodel/test/models/track_back.rb b/activemodel/test/models/track_back.rb new file mode 100644 index 0000000000..768c96ecf0 --- /dev/null +++ b/activemodel/test/models/track_back.rb @@ -0,0 +1,11 @@ +class Post + class TrackBack + def to_model + NamedTrackBack.new + end + end + + class NamedTrackBack + extend ActiveModel::Naming + end +end
\ No newline at end of file diff --git a/activemodel/test/models/user.rb b/activemodel/test/models/user.rb new file mode 100644 index 0000000000..1ec6001c48 --- /dev/null +++ b/activemodel/test/models/user.rb @@ -0,0 +1,10 @@ +class User + extend ActiveModel::Callbacks + include ActiveModel::SecurePassword + + define_model_callbacks :create + + has_secure_password + + attr_accessor :password_digest +end diff --git a/activemodel/test/models/visitor.rb b/activemodel/test/models/visitor.rb new file mode 100644 index 0000000000..22ad1a3c3d --- /dev/null +++ b/activemodel/test/models/visitor.rb @@ -0,0 +1,10 @@ +class Visitor + extend ActiveModel::Callbacks + include ActiveModel::SecurePassword + + define_model_callbacks :create + + has_secure_password(validations: false) + + attr_accessor :password_digest, :password_confirmation +end |