Этот коммит содержится в:
Mattia Roccoberton
2020-09-01 11:12:40 +02:00
родитель 80b5b3d3f3
Коммит b89fc0f939
82 изменённых файлов: 1707 добавлений и 4 удалений

Просмотреть файл

@@ -0,0 +1,7 @@
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
scope :published, -> {}
end

25
spec/dummy/app/models/author.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,25 @@
# frozen_string_literal: true
class Author < ApplicationRecord
has_many :posts
has_many :published_posts, -> { published }, class_name: 'Post'
has_many :recent_posts, -> { recents }, class_name: 'Post'
has_many :tags, through: :posts
has_one :profile, inverse_of: :author, dependent: :destroy
has_one_attached :avatar
accepts_nested_attributes_for :profile, allow_destroy: true
validates :email, format: { with: /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\z/i, message: 'Invalid email' }
validate -> {
errors.add( :base, 'Invalid age' ) if !age || age.to_i % 3 == 1
}
def to_s
"#{name} (#{age})"
end
end

0
spec/dummy/app/models/concerns/.keep Обычный файл
Просмотреть файл

25
spec/dummy/app/models/post.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,25 @@
# frozen_string_literal: true
class Post < ApplicationRecord
enum state: %i[available unavailable arriving]
belongs_to :author, inverse_of: :posts, autosave: true
has_one :author_profile, through: :author, source: :profile
has_many :post_tags, inverse_of: :post, dependent: :destroy
has_many :tags, through: :post_tags
validates :title, allow_blank: false, presence: true
scope :published, -> { where(published: true) }
scope :recents, -> { where('created_at > ?', Date.today - 8.month) }
def short_title
title.truncate 10
end
def upper_title
title.upcase
end
end

9
spec/dummy/app/models/post_tag.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,9 @@
# frozen_string_literal: true
class PostTag < ApplicationRecord
belongs_to :post, inverse_of: :post_tags
belongs_to :tag, inverse_of: :post_tags
validates :post, presence: true
validates :tag, presence: true
end

9
spec/dummy/app/models/profile.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,9 @@
# frozen_string_literal: true
class Profile < ApplicationRecord
belongs_to :author, inverse_of: :profile, touch: true
def to_s
description
end
end

6
spec/dummy/app/models/tag.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,6 @@
# frozen_string_literal: true
class Tag < ApplicationRecord
has_many :post_tags, inverse_of: :tag, dependent: :destroy
has_many :posts, through: :post_tags
end