2019-08-28 18:46:09 +02:00
|
|
|
defmodule Philomena.Commissions.Item do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2019-11-17 00:42:41 +01:00
|
|
|
alias Philomena.Commissions.Commission
|
|
|
|
alias Philomena.Images.Image
|
|
|
|
|
2019-08-28 18:46:09 +02:00
|
|
|
schema "commission_items" do
|
2019-11-17 00:42:41 +01:00
|
|
|
belongs_to :commission, Commission
|
|
|
|
belongs_to :example_image, Image
|
2019-08-28 18:46:09 +02:00
|
|
|
|
|
|
|
field :item_type, :string
|
|
|
|
field :description, :string
|
|
|
|
field :base_price, :decimal
|
|
|
|
field :add_ons, :string
|
|
|
|
|
|
|
|
timestamps(inserted_at: :created_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(item, attrs) do
|
|
|
|
item
|
2019-12-04 18:14:41 +01:00
|
|
|
|> cast(attrs, [:item_type, :description, :base_price, :add_ons, :example_image_id])
|
|
|
|
|> validate_required([:commission_id, :item_type, :description])
|
|
|
|
|> validate_length(:description, max: 300, count: :bytes)
|
|
|
|
|> validate_length(:add_ons, max: 500, count: :bytes)
|
|
|
|
|> validate_number(:base_price, greater_than_or_equal_to: 0, less_than_or_equal_to: 500)
|
|
|
|
|> validate_inclusion(:item_type, Commission.types())
|
|
|
|
|> foreign_key_constraint(:example_image_id, name: :fk_rails_56d368749a)
|
2019-08-28 18:46:09 +02:00
|
|
|
end
|
|
|
|
end
|