Advertisement
Guest User

modelExample

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.73 KB | None | 0 0
  1. class Project < ActiveRecord::Base
  2.   include TagValueNamesHash
  3.   before_save :set_default_gradation
  4.   auto_strip_attributes :name, squish: true
  5.   validates :name, presence: true
  6.   serialize :mailing, Serializers::JSON
  7.  
  8.   has_one :manager,
  9.           class_name: 'User',
  10.           primary_key: :manager_id,
  11.           foreign_key: :id
  12.   has_many :locations, -> { order name: :asc },
  13.            class_name: 'Projects::Location'
  14.   has_many :templates, -> { where(parent_id: nil).order(updated_at: :desc) },
  15.            class_name: 'Projects::Template'
  16.   has_many :templates_children, -> {
  17.     where.not(parent_id: nil).order(updated_at: :desc)
  18.   }, class_name: 'Projects::Template'
  19.   has_many :waves, -> { order(date_end: :desc) },
  20.            class_name: 'Projects::Wave'
  21.   has_many :visits, class_name: 'Projects::Visit'
  22.   has_and_belongs_to_many :tag_values,
  23.                           class_name: 'Tags::Value',
  24.                           join_table: 'projects_tags_values'
  25.   has_many :locations_tags,
  26.            inverse_of: :project,
  27.            class_name: 'Projects::Locations::Tag',
  28.            foreign_key: :owner_id
  29.   has_many :visits_tags,
  30.            inverse_of: :project,
  31.            class_name: 'Projects::Visits::Tag',
  32.            foreign_key: :owner_id
  33.  
  34.   has_one :logo, class_name: 'Projects::Logo'
  35.   belongs_to :gradation, class_name: 'Projects::Gradation'
  36.  
  37.   scope :includes_base_suite, -> {
  38.     includes(:waves, :tag_values, :logo,
  39.              locations: [:tag_values],
  40.              locations_tags: [:values],
  41.              templates: [:children, tags: [:values]],
  42.              visits_tags: [:values])
  43.   }
  44.   scope :search, ->(text, projects) {
  45.     if text.present?
  46.       where('id = ? OR name ILIKE ?',
  47.             text.to_i, "%#{text}%")
  48.     else
  49.       projects
  50.     end
  51.   }
  52.  
  53.   scope :accessible_relations, ->(accessible_info) {
  54.     includes(
  55.       :logo, :waves, :locations,
  56.       locations_tags: [:values],
  57.       templates: [:children, tags: [:values]],
  58.       visits_tags: [:values]
  59.     ).where(
  60.       projects_waves: {id: accessible_info[:ids][:wave_ids]},
  61.       projects_locations: {id: accessible_info[:ids][:location_ids]},
  62.       tags_values: {id: accessible_info[:ids][:location_tag_value_ids]}
  63.     ).references(
  64.       :projects_waves, :projects_locations,
  65.       :tags_values,
  66.       :projects_templates_answers
  67.     )
  68.   }
  69.  
  70.   def customers(priority = nil)
  71.     @customers = User.full.customer.where_accessible_projects(id)
  72.     if priority
  73.       customers = []
  74.       @customers.each do |c|
  75.         customers << c if Ability.new(c).can?(:"reject_by_customer_#{priority}",
  76.                                               Projects::Visit)
  77.       end
  78.       customers
  79.     else
  80.       @customers
  81.     end
  82.   end
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement