<commit_before># == Schema Information
#
# Table name: sites
#
#  id                              :integer         not null, primary key
#  name                            :string(255)     
#  short_description               :text            
#  long_description                :text            
#  contact_email                   :string(255)     
#  contact_person                  :string(255)     
#  url                             :string(255)     
#  permalink                       :string(255)     
#  google_analytics_id             :string(255)     
#  logo_file_name                  :string(255)     
#  logo_content_type               :string(255)     
#  logo_file_size                  :integer         
#  logo_updated_at                 :datetime        
#  theme_id                        :integer         
#  blog_url                        :string(255)     
#  word_for_clusters               :string(255)     
#  word_for_regions                :string(255)     
#  show_global_donations_raises    :boolean         
#  project_classification          :integer         default(0)
#  geographic_context_country_id   :integer         
#  geographic_context_region_id    :integer         
#  project_context_cluster_id      :integer         
#  project_context_sector_id       :integer         
#  project_context_organization_id :integer         
#  project_context_tags            :string(255)     
#  created_at                      :datetime        
#  updated_at                      :datetime        
#  project_context_tags_ids        :string(255)     
#  status                          :boolean         
#  visits                          :float           default(0.0)
#  visits_last_week                :float           default(0.0)
#  geographic_context_geometry     :geometry        
#

class Site < ActiveRecord::Base

  #acts_as_geom :the_geom => :polygon

  has_many :resources, :conditions => 'resources.element_type = #{Iom::ActsAsResource::SITE_TYPE}', :foreign_key => :element_id, :dependent => :destroy
  has_many :media_resources, :conditions => 'media_resources.element_type = #{Iom::ActsAsResource::SITE_TYPE}', :foreign_key => :element_id, :dependent => :destroy, :order => 'position ASC'
  belongs_to  :theme
  has_many :partners, :dependent => :destroy
  has_many :pages, :dependent => :destroy

  has_attached_file :logo, :styles => { :small => "60x60#" }

  scope :published, where(:status => true)
  scope :draft,     where(:status => false)

  validates_presence_of :name, :url
  validates_uniqueness_of :url

  before_validation :clean_html

  attr_accessor :geographic_context, :project_context, :show_blog

  before_save :set_geographic_context, :set_project_context, :set_project_context_tags_ids
  after_create :create_pages

  def show_blog
    !blog_url.blank?
  end
  alias :show_blog? :show_blog

  def word_for_clusters
    w = read_attribute(:word_for_clusters)
    w.blank? ? 'clusters' : w
  end

  def word_for_sectors
    w = read_attribute(:word_for_sectors)
    w.blank? ? 'sectors' : w
  end

  def cluster
    Cluster.find(self.project_context_cluster_id)
  rescue ActiveRecord::RecordNotFound
    nil
  end
  
  def published?
    status == true
  end


  def sector
    Sector.find(self.project_context_sector_id)
  rescue ActiveRecord::RecordNotFound
    nil
  end

  def navigate_by_cluster?
    project_classification == 0
  end

  def navigate_by_sector?
    project_classification == 1
  end

  # Filter projects from site configuration
  #
  # Use cases:
  #
  #  - cluster filtering (1)
  #    query: select projects.* from projects, clusters_projects where clusters_projects.project_id = projects.id and clusters_projects.cluster_id = #{cluster_id}
  #
  #  - sector filtering (2)
  #    query: select projects.* from projects, projects_sectors where projects_sectors.project_id = projects.id and projects_sectors.sector_id = #{sector_id}
  #
  #  - organizacion filtering (3)
  #    query: select projects.* from projects where projects.primary_organization_id = #{organization_id}
  #
  #  - tags filtering (4)
  #    query: select projects.* from projects, projects_tags where projects_tags.project_id = projects.id and projects_tags.id IN (#{tags_ids})
  #
  #  - country filtering (5)
  #    query: select projects.* from projects, countries_projects where countries_projects.project_id = projects.id and countries_projects.country_id = #{country_id}
  #
  #  - region filtering (6)
  #    query: select projects.* from projects, projects_regions where projects_regions.project_id = projects.id and projects_regions.region_id = #{region_id}
  #
  #  - bbox filtering (7)
  #    query : select projects.* from projects where ST_Contains(projects.the_geom, #{geographic_context_geometry})
  #
  def projects(options = {})
    default_options = { :limit => 10, :offset => 0 }
    options = default_options.merge(options)

    select = "projects.*"
    from = ["projects"]
    where = []

    # (1)
    if project_context_cluster_id?
      from << "clusters_projects"
      where << "(clusters_projects.project_id = projects.id AND clusters_projects.cluster_id = #{project_context_cluster_id})"
    end

    # (2)
    if project_context_sector_id?
      from << "projects_sectors"
      where << "(projects_sectors.project_id = projects.id AND projects_sectors.sector_id = #{project_context_sector_id})"
    end

    # (3)
    if project_context_organization_id?
      where << "projects.primary_organization_id = #{project_context_organization_id}"
    end

    # (4)
    if project_context_tags_ids?
      from << "projects_tags"
      where << "(projects_tags.project_id = projects.id AND projects_tags.tag_id IN (#{project_context_tags_ids}))"
    end

    # (5)
    if geographic_context_country_id?
      from << "countries_projects"
      where << "(countries_projects.project_id = projects.id AND countries_projects.country_id = #{geographic_context_country_id})"
    end

    # (6)
    if geographic_context_region_id?
      from << "projects_regions"
      where << "(projects_regions.project_id = projects.id AND projects_regions.region_id = #{geographic_context_region_id})"
    end

    # (7)
    if geographic_context_geometry?
      where << "ST_Contains(projects.the_geom, #{geographic_context_geometry})"
    end

    result = Project.select(select).from(from.join(',')).where(where.join(' AND '))
    if options[:limit]
      result = result.limit(options[:limit])
      if options[:offset]<commit_msg>solve issue with geography context on the admin dashboard<commit_after># == Schema Information
#
# Table name: sites
#
#  id                              :integer         not null, primary key
#  name                            :string(255)     
#  short_description               :text            
#  long_description                :text            
#  contact_email                   :string(255)     
#  contact_person                  :string(255)     
#  url                             :string(255)     
#  permalink                       :string(255)     
#  google_analytics_id             :string(255)     
#  logo_file_name                  :string(255)     
#  logo_content_type               :string(255)     
#  logo_file_size                  :integer         
#  logo_updated_at                 :datetime        
#  theme_id                        :integer         
#  blog_url                        :string(255)     
#  word_for_clusters               :string(255)     
#  word_for_regions                :string(255)     
#  show_global_donations_raises    :boolean         
#  project_classification          :integer         default(0)
#  geographic_context_country_id   :integer         
#  geographic_context_region_id    :integer         
#  project_context_cluster_id      :integer         
#  project_context_sector_id       :integer         
#  project_context_organization_id :integer         
#  project_context_tags            :string(255)     
#  created_at                      :datetime        
#  updated_at                      :datetime        
#  project_context_tags_ids        :string(255)     
#  status                          :boolean         
#  visits                          :float           default(0.0)
#  visits_last_week                :float           default(0.0)
#  geographic_context_geometry     :geometry        
#

class Site < ActiveRecord::Base

  #acts_as_geom :the_geom => :polygon

  has_many :resources, :conditions => 'resources.element_type = #{Iom::ActsAsResource::SITE_TYPE}', :foreign_key => :element_id, :dependent => :destroy
  has_many :media_resources, :conditions => 'media_resources.element_type = #{Iom::ActsAsResource::SITE_TYPE}', :foreign_key => :element_id, :dependent => :destroy, :order => 'position ASC'
  belongs_to  :theme
  has_many :partners, :dependent => :destroy
  has_many :pages, :dependent => :destroy

  has_attached_file :logo, :styles => { :small => "60x60#" }

  scope :published, where(:status => true)
  scope :draft,     where(:status => false)

  validates_presence_of :name, :url
  validates_uniqueness_of :url

  before_validation :clean_html

  attr_accessor :geographic_context, :project_context, :show_blog

  before_save :set_geographic_context, :set_project_context, :set_project_context_tags_ids
  after_create :create_pages

  def show_blog
    !blog_url.blank?
  end
  alias :show_blog? :show_blog

  def word_for_clusters
    w = read_attribute(:word_for_clusters)
    w.blank? ? 'clusters' : w
  end

  def word_for_sectors
    w = read_attribute(:word_for_sectors)
    w.blank? ? 'sectors' : w
  end

  def cluster
    Cluster.find(self.project_context_cluster_id)
  rescue ActiveRecord::RecordNotFound
    nil
  end
  
  def published?
    status == true
  end


  def sector
    Sector.find(self.project_context_sector_id)
  rescue ActiveRecord::RecordNotFound
    nil
  end

  def navigate_by_cluster?
    project_classification == 0
  end

  def navigate_by_sector?
    project_classification == 1
  end

  # Filter projects from site configuration
  #
  # Use cases:
  #
  #  - cluster filtering (1)
  #    query: select projects.* from projects, clusters_projects where clusters_projects.project_id = projects.id and clusters_projects.cluster_id = #{cluster_id}
  #
  #  - sector filtering (2)
  #    query: select projects.* from projects, projects_sectors where projects_sectors.project_id = projects.id and projects_sectors.sector_id = #{sector_id}
  #
  #  - organizacion filtering (3)
  #    query: select projects.* from projects where projects.primary_organization_id = #{organization_id}
  #
  #  - tags filtering (4)
  #    query: select projects.* from projects, projects_tags where projects_tags.project_id = projects.id and projects_tags.id IN (#{tags_ids})
  #
  #  - country filtering (5)
  #    query: select projects.* from projects, countries_projects where countries_projects.project_id = projects.id and countries_projects.country_id = #{country_id}
  #
  #  - region filtering (6)
  #    query: select projects.* from projects, projects_regions where projects_regions.project_id = projects.id and projects_regions.region_id = #{region_id}
  #
  #  - bbox filtering (7)
  #    query : select projects.* from projects where ST_Contains(projects.the_geom, #{geographic_context_geometry})
  #
  def projects(options = {})
    default_options = { :limit => 10, :offset => 0 }
    options = default_options.merge(options)

    select = "projects.*"
    from = ["projects"]
    where = []

    # (1)
    if project_context_cluster_id?
      from << "clusters_projects"
      where << "(clusters_projects.project_id = projects.id AND clusters_projects.cluster_id = #{project_context_cluster_id})"
    end

    # (2)
    if project_context_sector_id?
      from << "projects_sectors"
      where << "(projects_sectors.project_id = projects.id AND projects_sectors.sector_id = #{project_context_sector_id})"
    end

    # (3)
    if project_context_organization_id?
      where << "projects.primary_organization_id = #{project_context_organization_id}"
    end

    # (4)
    if project_context_tags_ids?
      from << "projects_tags"
      where << "(projects_tags.project_id = projects.id AND projects_tags.tag_id IN (#{project_context_tags_ids}))"
    end

    # (5)
    if geographic_context_country_id?
      from << "countries_projects"
      where << "(countries_projects.project_id = projects.id AND countries_projects.country_id = #{geographic_context_country_id})"
    end

    # (6)
    if geographic_context_region_id?
      from << "projects_regions"
      where << "(projects_regions.project_id = projects.id AND projects_regions.region_id = #{geographic_context_region_id})"
    end

    # (7)
    if geographic_context_geometry?
      where << "ST_Contains(ST_GeomFromText('POLYGON(#{geographic_context_geometry.text_representation()})',4326),projects.the_geom)"
    end

    result = Project.select(select).from(from.join(',')).where(where.join(' AND '))
    if options[:limit]
      result = result.limit(options[:limit])
      if options[:offset]
{'fraction_non_alphanumeric': 0.09393615396364748, 'fraction_numerical': 0.01053917824957996, 'mean_word_length': 2.644586696354022, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4288378', 'n_tokens_mistral': 4050, 'n_tokens_neox': 3844, 'n_words': 804}