spec/shared/relations.rb in rom-repository-0.2.0 vs spec/shared/relations.rb in rom-repository-0.3.0
- old
+ new
@@ -1,29 +1,105 @@
RSpec.shared_context 'relations' do
let(:users) { rom.relation(:users) }
let(:tasks) { rom.relation(:tasks) }
let(:tags) { rom.relation(:tags) }
+ let(:posts) { rom.relation(:posts) }
+ let(:books) { rom.relation(:books) }
before do
+ configuration.relation(:books) do
+ schema(:books) do
+ attribute :id, ROM::SQL::Types::Serial
+ attribute :title, ROM::SQL::Types::String
+ attribute :created_at, ROM::SQL::Types::Time
+ attribute :updated_at, ROM::SQL::Types::Time
+ end
+ end
+
configuration.relation(:users) do
+ schema(infer: true) do
+ associations do
+ has_many :posts
+ has_many :labels, through: :posts
+ end
+ end
+
+ def by_name(name)
+ where(name: name)
+ end
+
def all
select(:id, :name).order(:name, :id)
end
def find(criteria)
where(criteria)
end
end
configuration.relation(:tasks) do
+ schema(infer: true) do
+ associations do
+ belongs_to :user
+ end
+ end
+
def find(criteria)
where(criteria)
end
def for_users(users)
where(user_id: users.map { |u| u[:id] })
end
end
configuration.relation(:tags)
+
+ configuration.relation(:labels) do
+ schema(infer: true) do
+ associations do
+ has_many :posts_labels
+ has_many :posts, through: :posts_labels
+ end
+ end
+ end
+
+ configuration.relation(:posts) do
+ schema(:posts, infer: true) do
+ associations do
+ has_many :labels, through: :posts_labels
+ belongs_to :user, as: :author
+ end
+ end
+ end
+
+ configuration.relation(:posts_labels) do
+ schema(infer: true) do
+ associations do
+ belongs_to :post
+ belongs_to :label
+ end
+ end
+ end
+
+ configuration.relation(:comments) do
+ register_as :comments
+
+ schema(:messages, infer: true) do
+ associations do
+ has_many :reactions, relation: :likes
+ has_many :reactions, relation: :likes, as: :emotions
+ end
+ end
+ end
+
+ configuration.relation(:likes) do
+ register_as :likes
+
+ schema(:reactions, infer: true) do
+ associations do
+ belongs_to :message, relation: :comments
+ end
+ end
+ end
end
end