<commit_before># == Schema Information
#
# Table name: mobile_apps
#
#  id                   :integer          not null, primary key
#  name                 :text(16777215)
#  short_description    :text(16777215)
#  long_description     :text(16777215)
#  icon_url             :text(16777215)
#  language             :string(255)
#  agency_id            :integer
#  status               :integer          default("under_review")
#  mongo_id             :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  validated_at         :datetime
#  primary_contact_id   :integer
#  secondary_contact_id :integer
#  primary_agency_id    :integer
#  secondary_agency_id  :integer
#  notes                :text(65535)
#

class MobileApp < ActiveRecord::Base
  #handles logging of activity
  include PublicActivity::Model
  include Notifications
  include Elasticsearch::Model

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :id, type: :integer
      indexes :name, analyzer: 'english'
      indexes :platform, analyzer: 'english'
      indexes :agencies, analyzer: 'english'
      indexes :contacts, analyzer: 'english'
      indexes :status, analyzer: 'english'
      indexes :updated_at, type: :date
    end
  end

  def as_indexed_json(options={})
    contact_list = []
    if self.try(:primary_contact).try(:email)
      contact_list << self.try(:primary_contact).try(:email)
    end
    if self.try(:secondary_contact).try(:email)
      contact_list << self.try(:secondary_contact).try(:email)
    end
    if self.users.count > 0
      contact_list << self.users.map(&:email)
    end

    agency_list = []
    if self.try(:primary_agency).try(:name)
      agency_list << self.try(:primary_agency).try(:name)
    end
    if self.try(:secondary_agency).try(:name)
      agency_list << self.try(:secondary_agency).try(:name)
    end
    if self.users.count > 0
      agency_list << self.agencies.map(&:name)
    end

    result = {
      id: self.id,
      name: name,
      agencies: agency_list.flatten.join(", "),
      contacts: contact_list.flatten.join(", "),
      platform: self.mobile_app_versions.map(&:platform).join(", "),
      status: self.status.humanize,
      updated_at: self.updated_at
    }
    result
  end

  tracked owner: Proc.new{ |controller, model| controller.current_user }

  scope :by_agency, lambda {|id| joins(:agencies).where("agencies.id" => id) }
  scope :api, -> { where(status: 1) }

  enum status: { under_review: 0, published: 1, archived: 2,
    publish_requested: 3, archive_requested: 4 }


  #attr_accessible :name, :shortname, :info_url, :agency_contact_ids
  acts_as_taggable

  has_many :gallery_items, as: :item, dependent: :destroy
  has_many :galleries, through: :gallery_items, source: "MobileApp"

  has_many :mobile_app_agencies, dependent: :destroy
  has_many :agencies, :through => :mobile_app_agencies

  has_many :mobile_app_users, dependent: :destroy
  has_many :users, :through => :mobile_app_users

  has_many :mobile_app_versions, :dependent => :destroy

  has_many :mobile_app_official_tags, dependent: :destroy
  has_many :official_tags, :through => :mobile_app_official_tags

  belongs_to :primary_contact, class_name: "User", foreign_key: "primary_contact_id"
  belongs_to :secondary_contact, class_name: "User", foreign_key: "secondary_contact_id"

  # validates :primary_contact, :presence => true
  # validates :secondary_contact, :presence => true


  belongs_to :primary_agency, class_name: "Agency", foreign_key: "primary_agency_id"
  belongs_to :secondary_agency, class_name: "Agency", foreign_key: "secondary_agency_id"

  # validates :primary_agency, :presence => true

  accepts_nested_attributes_for :mobile_app_versions, reject_if: :all_blank, allow_destroy: true

  #validates :name, :presence => true
  validates_presence_of :name, message: 'Name can’t be blank' 
  #validates :short_description, :presence => true
  validates_presence_of :short_description, message: 'Short Description can’t be blank'
  #validates :long_description, :presence => true

  validates :agencies, :length => { :minimum => 1, :message => "Sponsoring Agencies*-select at least one sponsoring agency. Type to search Agencies list." }
  validates :users, :length => { :minimum => 1, :message => "Contacts*-add at least one contact. Type to search Contacts list." }
  validates :mobile_app_versions, :length => { :minimum => 1, :message => "Mobile app versions have at least one version of the product listed." }



  def self.platform_counts
    joins(:mobile_app_versions).group(:platform).distinct("mobile_app_id, platform").count
  end

  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << (column_names + ["agencies" ,"contacts" ,"tags", "store_url", "platform", "version_number", "publish_date", "screenshot", "device", "average_rating", "number_of_ratings"])

      self.all.includes(:agencies,:users,:official_tags, :mobile_app_versions).each do |ma|
        versions = ma.mobile_app_versions
        if versions.count > 0
          versions.each do |version|
            csv << (ma.attributes.values_at(*column_names) + [
              ma.agencies.map(&:name).join("|") ,
              ma.users.map(&:email).join("|"),
              ma.official_tags.map(&:tag_text).join("|"),
              version.store_url,
              version.platform,
              version.version_number,
              version.publish_date ? version.publish_date.strftime("%B %e, %Y %H:%M") : "",
              version.screenshot,
              version.device,
              version.average_rating,
              version.number_of_ratings])
          end
        else<commit_msg>update error msg<commit_after># == Schema Information
#
# Table name: mobile_apps
#
#  id                   :integer          not null, primary key
#  name                 :text(16777215)
#  short_description    :text(16777215)
#  long_description     :text(16777215)
#  icon_url             :text(16777215)
#  language             :string(255)
#  agency_id            :integer
#  status               :integer          default("under_review")
#  mongo_id             :string(255)
#  created_at           :datetime
#  updated_at           :datetime
#  validated_at         :datetime
#  primary_contact_id   :integer
#  secondary_contact_id :integer
#  primary_agency_id    :integer
#  secondary_agency_id  :integer
#  notes                :text(65535)
#

class MobileApp < ActiveRecord::Base
  #handles logging of activity
  include PublicActivity::Model
  include Notifications
  include Elasticsearch::Model

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :id, type: :integer
      indexes :name, analyzer: 'english'
      indexes :platform, analyzer: 'english'
      indexes :agencies, analyzer: 'english'
      indexes :contacts, analyzer: 'english'
      indexes :status, analyzer: 'english'
      indexes :updated_at, type: :date
    end
  end

  def as_indexed_json(options={})
    contact_list = []
    if self.try(:primary_contact).try(:email)
      contact_list << self.try(:primary_contact).try(:email)
    end
    if self.try(:secondary_contact).try(:email)
      contact_list << self.try(:secondary_contact).try(:email)
    end
    if self.users.count > 0
      contact_list << self.users.map(&:email)
    end

    agency_list = []
    if self.try(:primary_agency).try(:name)
      agency_list << self.try(:primary_agency).try(:name)
    end
    if self.try(:secondary_agency).try(:name)
      agency_list << self.try(:secondary_agency).try(:name)
    end
    if self.users.count > 0
      agency_list << self.agencies.map(&:name)
    end

    result = {
      id: self.id,
      name: name,
      agencies: agency_list.flatten.join(", "),
      contacts: contact_list.flatten.join(", "),
      platform: self.mobile_app_versions.map(&:platform).join(", "),
      status: self.status.humanize,
      updated_at: self.updated_at
    }
    result
  end

  tracked owner: Proc.new{ |controller, model| controller.current_user }

  scope :by_agency, lambda {|id| joins(:agencies).where("agencies.id" => id) }
  scope :api, -> { where(status: 1) }

  enum status: { under_review: 0, published: 1, archived: 2,
    publish_requested: 3, archive_requested: 4 }


  #attr_accessible :name, :shortname, :info_url, :agency_contact_ids
  acts_as_taggable

  has_many :gallery_items, as: :item, dependent: :destroy
  has_many :galleries, through: :gallery_items, source: "MobileApp"

  has_many :mobile_app_agencies, dependent: :destroy
  has_many :agencies, :through => :mobile_app_agencies

  has_many :mobile_app_users, dependent: :destroy
  has_many :users, :through => :mobile_app_users

  has_many :mobile_app_versions, :dependent => :destroy

  has_many :mobile_app_official_tags, dependent: :destroy
  has_many :official_tags, :through => :mobile_app_official_tags

  belongs_to :primary_contact, class_name: "User", foreign_key: "primary_contact_id"
  belongs_to :secondary_contact, class_name: "User", foreign_key: "secondary_contact_id"

  # validates :primary_contact, :presence => true
  # validates :secondary_contact, :presence => true


  belongs_to :primary_agency, class_name: "Agency", foreign_key: "primary_agency_id"
  belongs_to :secondary_agency, class_name: "Agency", foreign_key: "secondary_agency_id"

  # validates :primary_agency, :presence => true

  accepts_nested_attributes_for :mobile_app_versions, reject_if: :all_blank, allow_destroy: true

  #validates :name, :presence => true
  validates_presence_of :name, message: 'Name can’t be blank' 
  #validates :short_description, :presence => true
  validates_presence_of :short_description, message: 'Short Description can’t be blank'
  #validates :long_description, :presence => true

  validates :agencies, :length => { :minimum => 1, :message => "Sponsoring Agencies*- Select at least one sponsoring agency. Type to search Agencies list." }
  validates :users, :length => { :minimum => 1, :message => "Contacts*- Add at least one contact. Type to search Contacts list." }
  validates :mobile_app_versions, :length => { :minimum => 1, :message => "Mobile app versions have at least one version of the product listed." }



  def self.platform_counts
    joins(:mobile_app_versions).group(:platform).distinct("mobile_app_id, platform").count
  end

  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << (column_names + ["agencies" ,"contacts" ,"tags", "store_url", "platform", "version_number", "publish_date", "screenshot", "device", "average_rating", "number_of_ratings"])

      self.all.includes(:agencies,:users,:official_tags, :mobile_app_versions).each do |ma|
        versions = ma.mobile_app_versions
        if versions.count > 0
          versions.each do |version|
            csv << (ma.attributes.values_at(*column_names) + [
              ma.agencies.map(&:name).join("|") ,
              ma.users.map(&:email).join("|"),
              ma.official_tags.map(&:tag_text).join("|"),
              version.store_url,
              version.platform,
              version.version_number,
              version.publish_date ? version.publish_date.strftime("%B %e, %Y %H:%M") : "",
              version.screenshot,
              version.device,
              version.average_rating,
              version.number_of_ratings])
          end
        else
{'fraction_non_alphanumeric': 0.11975838221132802, 'fraction_numerical': 0.009804779830167207, 'mean_word_length': 3.3887821744141373, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625017', 'n_tokens_mistral': 3723, 'n_tokens_neox': 3540, 'n_words': 801}