diff options
Diffstat (limited to 'activemodel/test/models')
-rw-r--r-- | activemodel/test/models/account.rb | 7 | ||||
-rw-r--r-- | activemodel/test/models/blog_post.rb | 11 | ||||
-rw-r--r-- | activemodel/test/models/contact.rb | 42 | ||||
-rw-r--r-- | activemodel/test/models/custom_reader.rb | 17 | ||||
-rw-r--r-- | activemodel/test/models/helicopter.rb | 9 | ||||
-rw-r--r-- | activemodel/test/models/person.rb | 19 | ||||
-rw-r--r-- | activemodel/test/models/person_with_validator.rb | 26 | ||||
-rw-r--r-- | activemodel/test/models/reply.rb | 34 | ||||
-rw-r--r-- | activemodel/test/models/sheep.rb | 5 | ||||
-rw-r--r-- | activemodel/test/models/topic.rb | 41 | ||||
-rw-r--r-- | activemodel/test/models/track_back.rb | 13 | ||||
-rw-r--r-- | activemodel/test/models/user.rb | 12 | ||||
-rw-r--r-- | activemodel/test/models/visitor.rb | 12 |
13 files changed, 248 insertions, 0 deletions
diff --git a/activemodel/test/models/account.rb b/activemodel/test/models/account.rb new file mode 100644 index 0000000000..40408e5708 --- /dev/null +++ b/activemodel/test/models/account.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +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..d4b02eeaa7 --- /dev/null +++ b/activemodel/test/models/blog_post.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +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..c40a6d6f0e --- /dev/null +++ b/activemodel/test/models/contact.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +class Contact + extend ActiveModel::Naming + include ActiveModel::Conversion + include ActiveModel::Validations + + include ActiveModel::Serializers::JSON + + attr_accessor :id, :name, :age, :created_at, :awesome, :preferences + attr_accessor :address, :friends, :contact + + 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 + + def attributes=(hash) + hash.each do |k, v| + instance_variable_set("@#{k}", v) + end + end + + def attributes + instance_values.except("address", "friends", "contact") + end +end diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb new file mode 100644 index 0000000000..df605e93d9 --- /dev/null +++ b/activemodel/test/models/custom_reader.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +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 diff --git a/activemodel/test/models/helicopter.rb b/activemodel/test/models/helicopter.rb new file mode 100644 index 0000000000..fe82c463d3 --- /dev/null +++ b/activemodel/test/models/helicopter.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +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..b61fdf76b1 --- /dev/null +++ b/activemodel/test/models/person.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +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..44e78cbc29 --- /dev/null +++ b/activemodel/test/models/person_with_validator.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +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/reply.rb b/activemodel/test/models/reply.rb new file mode 100644 index 0000000000..6bb18f95fe --- /dev/null +++ b/activemodel/test/models/reply.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +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..30dd9ce192 --- /dev/null +++ b/activemodel/test/models/sheep.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +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..2f4e92c3b2 --- /dev/null +++ b/activemodel/test/models/topic.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +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..728a022db5 --- /dev/null +++ b/activemodel/test/models/track_back.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class Post + class TrackBack + def to_model + NamedTrackBack.new + end + end + + class NamedTrackBack + extend ActiveModel::Naming + end +end diff --git a/activemodel/test/models/user.rb b/activemodel/test/models/user.rb new file mode 100644 index 0000000000..e98fd8a0a1 --- /dev/null +++ b/activemodel/test/models/user.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +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..9da004ffcc --- /dev/null +++ b/activemodel/test/models/visitor.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +class Visitor + extend ActiveModel::Callbacks + include ActiveModel::SecurePassword + + define_model_callbacks :create + + has_secure_password(validations: false) + + attr_accessor :password_digest, :password_confirmation +end |