Sha256: 6b10726444981fe97868e842f4cb484bd6b45a1537a7c775dffd0e9d448712e3

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

class ChosenDeck < ApplicationRecord
  belongs_to :deck, inverse_of: :chosen_decks
  belongs_to :commission, inverse_of: :chosen_decks

  has_many :chosen_rooms, inverse_of: :chosen_deck, dependent: :destroy
  has_many :rooms, through: :chosen_rooms, inverse_of: :chosen_decks
  accepts_nested_attributes_for :chosen_rooms, allow_destroy: true

  validates :deck, presence: true
  validates :commission, presence: true

  before_create :increment_code
  # Items: I need a method to obtain all the items in the
  # current selected Commission
  def records_for_print id
    # TODO: move away from this pseudo code as soon as Bancolini tells me
    # what Mobilart wants
    # chosen_item.name
    # chosen_item.barcode
    # chosen_item.timetables.last.status
    # chosen_item.timetables.last.job_type
    ChosenItem.joins(chosen_furniture: {chosen_room: :chosen_deck}).where(chosen_decks: {id: id}).distinct
  end

  def list_associated_items
    true
  end

  default_scope {joins(:commission)}

  amoeba do
    enable
    include_association :chosen_rooms
  end

  def display_name
    "Ponte #{deck.name} della commessa #{commission.name} contenente #{chosen_rooms.count} stanze"
  end

  rails_admin do
    navigation_label I18n.t(:operative_section)
    navigation_icon 'fa fa-tasks'
    parent Commission
    weight 5

    # list do
    field :commission do
      read_only true
    end
    field :deck do
      read_only true
    end
    field :chosen_rooms
  end

  def readable_barcode
    "#{commission.code}|#{deck.code}|#{code}"
  end

  def barcode
    "#{commission.code}#{deck.code}#{code}"
  end

  def name
    # "#{deck.name rescue nil} (#{readable_barcode rescue nil})"
    deck.name rescue "Undefined"
  end

  private

  def increment_code
    max_code = ChosenDeck.order("chosen_decks.code DESC").pluck(:code).compact.map(&:to_i).max.to_i
    # Rails.logger.debug "MAX CODE IS:#{max_code} Adding 1: #{(max_code + 1)}"
    self.code = (max_code + 1)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mobilart_models-1.2.14 app/models/chosen_deck.rb