<commit_before># == Schema Information
#
# Table name: pages
#
#  id                       :integer          not null, primary key
#  site_id                  :integer
#  name                     :string
#  description              :string
#  uri                      :string
#  url                      :string
#  created_at               :datetime         not null
#  updated_at               :datetime         not null
#  content_type             :integer
#  type                     :text
#  enabled                  :boolean          default(FALSE)
#  parent_id                :integer
#  position                 :integer
#  content                  :json
#  show_on_menu             :boolean          default(TRUE)
#  page_version             :integer          default(1)
#  thumbnail_file_name      :string
#  thumbnail_content_type   :string
#  thumbnail_file_size      :integer
#  thumbnail_updated_at     :datetime
#  cover_image_file_name    :string
#  cover_image_content_type :string
#  cover_image_file_size    :integer
#  cover_image_updated_at   :datetime
#

class SitePage < Page
  include PgSearch
  pg_search_scope :search,
                  associated_against: {
                    tags: :value
                  },
                  against: { name: 'A', description: 'B', content: 'C' },
                  order_within_rank: 'pages.updated_at DESC'

  pg_search_scope :search_tags,
                  associated_against: {
                    tags: :value
                  },
                  using: {
                    tsearch: { any_word: true }
                  },
                  order_within_rank: 'pages.updated_at DESC'

  scope :for_site, ->(site_id) { where(site_id: site_id)}
  scope :enabled, -> { where(enabled: true) }
  scope :not_tag_page, -> { where.not(content_type: ContentType::TAG_SEARCHING)}
  scope :not_link_page, -> { where.not(content_type: ContentType::LINK)}
  scope :not_group_page, -> { where.not(content_type: ContentType::GROUP)}

  belongs_to :site
  has_many :routes, through: :site
  has_one :site_template, through: :site
  has_many :users, through: :site
  has_one :dataset_setting, dependent: :destroy,
          inverse_of: :site_page, autosave: true
  has_one :dashboard_setting,foreign_key: 'page_id', dependent: :destroy,
          inverse_of: :site_page, autosave: true
  has_many :tags, foreign_key: :page_id, dependent: :destroy
  has_many :content_images, dependent: :destroy, foreign_key: :page_id

  before_create :set_defaults
  before_save :construct_url, if: 'content_type.eql? ContentType::LINK'
  before_save :update_temporary_cover_and_thumb
  before_save :convert_booleans_in_map_pages

  validates :url, uniqueness: {scope: :site}, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::LINK)'
  validates :uri, uniqueness: {scope: :site}, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::LINK)'
  validates_presence_of :site_id
  before_create :cheat_with_position_on_create
  before_update :cheat_with_position_on_update
  after_create :update_routes
  after_update :update_routes, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::HOMEPAGE)'
  after_save :update_temporary_content_images
  after_save :destroy_temporary_cover_and_thumb

  validate :step_validation

  accepts_nested_attributes_for :tags, allow_destroy: true

  has_attached_file :thumbnail, styles: { original: '800x>' }

  attr_accessor :form_step
  attr_accessor :thumbnail_url
  attr_accessor :temp_cover_image
  attr_accessor :temp_thumbnail
  attr_accessor :delete_thumbnail
  attr_accessor :delete_cover_image

  MAX_RELATED_PAGES_SIZE = 3

  def form_steps
    steps = {pages: %w[position title type],
             names: %w[Position Title Type]}

    case self.content_type
    when ContentType::OPEN_CONTENT_V2
      steps = { pages: %w[position title type open_content_v2 open_content_v2_preview],
                names: %w[Position Details Type Content Preview] }
    when ContentType::DASHBOARD_V2
      steps = { pages: %w[position title type dashboard_dataset dashboard_widget columns_selection preview],
                names: %w[Position Details Type Dataset Widget Columns Preview] }
    when ContentType::LINK
      steps = { pages: %w[position title type link],
                names: %w[Position Details Type Link] }
    when ContentType::STATIC_CONTENT
      steps = { pages: %w[title type open_content open_content_preview],
                names: %w[Details Type Content Preview] }
    when ContentType::MAP
      steps = { pages: %w[position title type map],
                names: %w[Position Details Type Map] }
    when ContentType::HOMEPAGE
      steps = { pages: %w[title open_content open_content_preview],
                names: ['Title', 'Open Content', 'Open Content Preview'] }
    when ContentType::TAG_SEARCHING
      steps = { pages: %w[position title type tag_searching],
                names: ['Position', 'Details', 'Type', 'Tag Searching'] }
    when ContentType::GROUP
      steps = { pages: %w[position title type confirmation],
                names: %w[Position Details Type Confirmation] }
    end
    steps
  end

  def uri=(value)
    if value
      value.gsub!(/[^a-zA-Z0-9\-?=&]/, '')
      write_attribute(:uri, value)
      regenerate_url<commit_msg>Fix error when try to access to open content tabs on home and static content pages<commit_after># == Schema Information
#
# Table name: pages
#
#  id                       :integer          not null, primary key
#  site_id                  :integer
#  name                     :string
#  description              :string
#  uri                      :string
#  url                      :string
#  created_at               :datetime         not null
#  updated_at               :datetime         not null
#  content_type             :integer
#  type                     :text
#  enabled                  :boolean          default(FALSE)
#  parent_id                :integer
#  position                 :integer
#  content                  :json
#  show_on_menu             :boolean          default(TRUE)
#  page_version             :integer          default(1)
#  thumbnail_file_name      :string
#  thumbnail_content_type   :string
#  thumbnail_file_size      :integer
#  thumbnail_updated_at     :datetime
#  cover_image_file_name    :string
#  cover_image_content_type :string
#  cover_image_file_size    :integer
#  cover_image_updated_at   :datetime
#

class SitePage < Page
  include PgSearch
  pg_search_scope :search,
                  associated_against: {
                    tags: :value
                  },
                  against: { name: 'A', description: 'B', content: 'C' },
                  order_within_rank: 'pages.updated_at DESC'

  pg_search_scope :search_tags,
                  associated_against: {
                    tags: :value
                  },
                  using: {
                    tsearch: { any_word: true }
                  },
                  order_within_rank: 'pages.updated_at DESC'

  scope :for_site, ->(site_id) { where(site_id: site_id)}
  scope :enabled, -> { where(enabled: true) }
  scope :not_tag_page, -> { where.not(content_type: ContentType::TAG_SEARCHING)}
  scope :not_link_page, -> { where.not(content_type: ContentType::LINK)}
  scope :not_group_page, -> { where.not(content_type: ContentType::GROUP)}

  belongs_to :site
  has_many :routes, through: :site
  has_one :site_template, through: :site
  has_many :users, through: :site
  has_one :dataset_setting, dependent: :destroy,
          inverse_of: :site_page, autosave: true
  has_one :dashboard_setting,foreign_key: 'page_id', dependent: :destroy,
          inverse_of: :site_page, autosave: true
  has_many :tags, foreign_key: :page_id, dependent: :destroy
  has_many :content_images, dependent: :destroy, foreign_key: :page_id

  before_create :set_defaults
  before_save :construct_url, if: 'content_type.eql? ContentType::LINK'
  before_save :update_temporary_cover_and_thumb
  before_save :convert_booleans_in_map_pages

  validates :url, uniqueness: {scope: :site}, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::LINK)'
  validates :uri, uniqueness: {scope: :site}, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::LINK)'
  validates_presence_of :site_id
  before_create :cheat_with_position_on_create
  before_update :cheat_with_position_on_update
  after_create :update_routes
  after_update :update_routes, unless: 'content_type.eql?(nil) || content_type.eql?(ContentType::HOMEPAGE)'
  after_save :update_temporary_content_images
  after_save :destroy_temporary_cover_and_thumb

  validate :step_validation

  accepts_nested_attributes_for :tags, allow_destroy: true

  has_attached_file :thumbnail, styles: { original: '800x>' }

  attr_accessor :form_step
  attr_accessor :thumbnail_url
  attr_accessor :temp_cover_image
  attr_accessor :temp_thumbnail
  attr_accessor :delete_thumbnail
  attr_accessor :delete_cover_image

  MAX_RELATED_PAGES_SIZE = 3

  def form_steps
    steps = {pages: %w[position title type],
             names: %w[Position Title Type]}

    case self.content_type
    when ContentType::OPEN_CONTENT_V2
      steps = { pages: %w[position title type open_content_v2 open_content_v2_preview],
                names: %w[Position Details Type Content Preview] }
    when ContentType::DASHBOARD_V2
      steps = { pages: %w[position title type dashboard_dataset dashboard_widget columns_selection preview],
                names: %w[Position Details Type Dataset Widget Columns Preview] }
    when ContentType::LINK
      steps = { pages: %w[position title type link],
                names: %w[Position Details Type Link] }
    when ContentType::STATIC_CONTENT
      steps = { pages: %w[title type open_content_v2 open_content_v2_preview],
                names: %w[Details Type Content Preview] }
    when ContentType::MAP
      steps = { pages: %w[position title type map],
                names: %w[Position Details Type Map] }
    when ContentType::HOMEPAGE
      steps = { pages: %w[title open_content_v2 open_content_v2_preview],
                names: ['Title', 'Open Content', 'Open Content Preview'] }
    when ContentType::TAG_SEARCHING
      steps = { pages: %w[position title type tag_searching],
                names: ['Position', 'Details', 'Type', 'Tag Searching'] }
    when ContentType::GROUP
      steps = { pages: %w[position title type confirmation],
                names: %w[Position Details Type Confirmation] }
    end
    steps
  end

  def uri=(value)
    if value
      value.gsub!(/[^a-zA-Z0-9\-?=&]/, '')
      write_attribute(:uri, value)
      regenerate_url
{'fraction_non_alphanumeric': 0.09153018249883013, 'fraction_numerical': 0.0024333177351427236, 'mean_word_length': 2.6975778546712803, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1514589', 'n_tokens_mistral': 3243, 'n_tokens_neox': 3153, 'n_words': 750}