<commit_before># frozen_string_literal: true

module GobiertoDashboards
  class DashboardForm < BaseForm
    include ::GobiertoCore::TranslationsHelpers
    include ActiveModel::Serialization

    attr_accessor(
      :site_id,
      :admin_id,
      :widgets_configuration,
      :context
    )

    attr_writer(
      :id,
      :title_translations,
      :title,
      :visibility_level
    )

    validate :context_presence
    validates :title, :visibility_level, :site, presence: true
    validates :title_translations, translated_attribute_presence: true
    validates :visibility_level, inclusion: { in: Dashboard.visibility_levels.keys }
    validate :widgets_configuration_json_format

    delegate :persisted?, to: :resource

    def resource
      @resource ||= resource_class.find_by(id: @id) || build_resource
    end
    alias dashboard resource

    def site
      @site ||= Site.find_by(id: site_id)
    end

    def id
      @id ||= resource.id
    end

    def title_translations
      @title_translations ||= (resource.title_translations || available_locales_blank_translations).tap do |translations|
        translations[I18n.locale] = @title if @title.present?
      end
    end

    def title
      title_translations.with_indifferent_access[I18n.locale]
    end

    def visibility_level
      @visibility_level ||= resource.visibility_level || :draft
    end

    def context_service
      @context_service ||= GobiertoCommon::ContextService.new(context)
    end

    def save
      save_resource if valid?
    end

    private

    def widgets_configuration_json_format
      return if widgets_configuration.blank? || widgets_configuration.is_a?(Enumerable)

      @widgets_configuration = JSON.parse(widgets_configuration)
    rescue JSON::ParserError
      errors.add :widgets_configuration, I18n.t("errors.messages.invalid")
    end

    def transformed_widgets_configuration<commit_msg>Fix dashboard form to avoid errors when some attributes are blank<commit_after># frozen_string_literal: true

module GobiertoDashboards
  class DashboardForm < BaseForm
    include ::GobiertoCore::TranslationsHelpers
    include ActiveModel::Serialization

    attr_accessor(
      :site_id,
      :admin_id,
      :widgets_configuration,
      :context
    )

    attr_writer(
      :id,
      :title_translations,
      :title,
      :visibility_level
    )

    validate :context_presence
    validates :title, :visibility_level, :site, presence: true
    validates :title_translations, translated_attribute_presence: true
    validates :visibility_level, inclusion: { in: Dashboard.visibility_levels.keys }
    validate :widgets_configuration_json_format

    delegate :persisted?, to: :resource

    def resource
      @resource ||= resource_class.find_by(id: @id) || build_resource
    end
    alias dashboard resource

    def site
      @site ||= Site.find_by(id: site_id)
    end

    def id
      @id ||= resource.id
    end

    def title_translations
      @title_translations ||= (resource.title_translations || available_locales_blank_translations).tap do |translations|
        translations[I18n.locale] = @title if @title.present?
      end
    end

    def title
      title_translations.with_indifferent_access[I18n.locale].presence || title_translations.values.find(&:present?)
    end

    def visibility_level
      @visibility_level ||= resource.visibility_level || :draft
    end

    def context_service
      @context_service ||= GobiertoCommon::ContextService.new(context)
    end

    def save
      save_resource if valid?
    end

    private

    def widgets_configuration_json_format
      return if widgets_configuration.blank? || widgets_configuration.is_a?(Enumerable)

      @widgets_configuration = JSON.parse(widgets_configuration)
    rescue JSON::ParserError
      errors.add :widgets_configuration, I18n.t("errors.messages.invalid")
    end

    def transformed_widgets_configuration
      return if widgets_configuration.blank?

{'fraction_non_alphanumeric': 0.07628607277289837, 'fraction_numerical': 0.00301129234629862, 'mean_word_length': 3.4338153503893216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '926529', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1127, 'n_words': 247}