Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Project < ActiveRecord::Base
- include TagValueNamesHash
- before_save :set_default_gradation
- auto_strip_attributes :name, squish: true
- validates :name, presence: true
- serialize :mailing, Serializers::JSON
- has_one :manager,
- class_name: 'User',
- primary_key: :manager_id,
- foreign_key: :id
- has_many :locations, -> { order name: :asc },
- class_name: 'Projects::Location'
- has_many :templates, -> { where(parent_id: nil).order(updated_at: :desc) },
- class_name: 'Projects::Template'
- has_many :templates_children, -> {
- where.not(parent_id: nil).order(updated_at: :desc)
- }, class_name: 'Projects::Template'
- has_many :waves, -> { order(date_end: :desc) },
- class_name: 'Projects::Wave'
- has_many :visits, class_name: 'Projects::Visit'
- has_and_belongs_to_many :tag_values,
- class_name: 'Tags::Value',
- join_table: 'projects_tags_values'
- has_many :locations_tags,
- inverse_of: :project,
- class_name: 'Projects::Locations::Tag',
- foreign_key: :owner_id
- has_many :visits_tags,
- inverse_of: :project,
- class_name: 'Projects::Visits::Tag',
- foreign_key: :owner_id
- has_one :logo, class_name: 'Projects::Logo'
- belongs_to :gradation, class_name: 'Projects::Gradation'
- scope :includes_base_suite, -> {
- includes(:waves, :tag_values, :logo,
- locations: [:tag_values],
- locations_tags: [:values],
- templates: [:children, tags: [:values]],
- visits_tags: [:values])
- }
- scope :search, ->(text, projects) {
- if text.present?
- where('id = ? OR name ILIKE ?',
- text.to_i, "%#{text}%")
- else
- projects
- end
- }
- scope :accessible_relations, ->(accessible_info) {
- includes(
- :logo, :waves, :locations,
- locations_tags: [:values],
- templates: [:children, tags: [:values]],
- visits_tags: [:values]
- ).where(
- projects_waves: {id: accessible_info[:ids][:wave_ids]},
- projects_locations: {id: accessible_info[:ids][:location_ids]},
- tags_values: {id: accessible_info[:ids][:location_tag_value_ids]}
- ).references(
- :projects_waves, :projects_locations,
- :tags_values,
- :projects_templates_answers
- )
- }
- def customers(priority = nil)
- @customers = User.full.customer.where_accessible_projects(id)
- if priority
- customers = []
- @customers.each do |c|
- customers << c if Ability.new(c).can?(:"reject_by_customer_#{priority}",
- Projects::Visit)
- end
- customers
- else
- @customers
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement