Skip to content

Instantly share code, notes, and snippets.

@adkron
Created March 24, 2016 19:13
Show Gist options
  • Save adkron/1db2d642392f01221224 to your computer and use it in GitHub Desktop.
Save adkron/1db2d642392f01221224 to your computer and use it in GitHub Desktop.
# == Schema Information
#
# Table name: cached_open_items
#
# id :integer not null, primary key
# company_id :integer not null
# project_id :integer
# login_information_id :integer not null
# open_item_id :integer not null
# open_item_type :string(255) not null
# details :text not null
# due_date :datetime
# status :text not null
# type :text not null
# created_at :datetime
# updated_at :datetime
# titleized_type :string
# titleized_type_key :string
#
# Indexes
#
# idx_cached_open_items_on_li_id_and_item_id_and_item_type (login_information_id,open_item_id,open_item_type) UNIQUE
# index_cached_open_items_on_company_id (company_id)
# index_cached_open_items_on_login_information_id (login_information_id)
# index_cached_open_items_on_open_item_id_and_open_item_type (open_item_id,open_item_type)
#
class CachedOpenItem < ActiveRecord::Base
self.inheritance_column = :not_using_single_table_inheritance
belongs_to :login_information
belongs_to :open_item, polymorphic: true
belongs_to :company
belongs_to :project
validates :login_information_id,
uniqueness: {
scope: [:open_item_type, :open_item_id]
},
presence: true
scope :for_company, ->(company) do
where(company_id: company.id)
end
scope :for_project, ->(project) do
where(project_id: project.id)
end
scope :active_projects_only, -> do
joins("LEFT OUTER JOIN projects ON projects.id = cached_open_items.project_id").
where('projects.id IS NULL OR projects.active')
end
def type
if open_item_type == 'GenericToolItem'
titleized_type
else
I18n.t(titleized_type_key)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment