<commit_before>module RailsRole::Govern
  extend ActiveSupport::Concern

  included do
    attribute :identifier, :string
    attribute :namespace_identifier, :string, default: 'application'
    attribute :business_identifier, :string
    attribute :position, :integer

    belongs_to :name_space, foreign_key: :namespace_identifier, primary_key: :identifier, optional: true
    belongs_to :busyness, foreign_key: :business_identifier, primary_key: :identifier, optional: true
    has_many :rules, -> { order(position: :asc) }, dependent: :destroy
    has_many :role_rules, dependent: :destroy
    accepts_nested_attributes_for :rules, allow_destroy: true

    default_scope -> { order(position: :asc, id: :asc) }

    validates :identifier, uniqueness: true

    after_create_commit :sync_controller

    acts_as_list scope: [:namespace_identifier, :business_identifier]
  end

  def business_name
    t = I18n.t "#{business_identifier}.title", default: nil
    return t if t

    business_identifier
  end

  def namespace_name
    t = I18n.t "#{business_identifier}.#{namespace_identifier}.title", default: nil
    return t if t

    namespace_identifier
  end

  def name
    t = I18n.t "#{identifier.to_s.split('/').join('.')}.index.title", default: nil
    return t if t

    identifier
  end

  class_methods do

    def sync
      missing_controllers, invalid_controllers = analyze_controllers
      RailsCom::Routes.controllers.extract!(*missing_controllers).each do |controller, routes|
        govern = Govern.find_or_initialize_by(identifier: controller)
        route = routes[0]
        govern.business_identifier = route[:business]
        govern.namespace_identifier = route[:namespace]

        present_rules = govern.rules.pluck(:identifier)

        all_rules = routes.map(&->(i){ i[:action] })
        all_rules = ['admin', 'read'] + all_rules if all_rules.present?

        (all_rules - present_rules).each do |action|
          govern.rules.build(identifier: action)
        end

        (present_rules - all_rules).each do |action|
          r = govern.rules.find_by(identifier: action)
          r.mark_for_destruction
        end

        govern.save if govern.rules.length > 0
      end

      Govern.where(identifier: invalid_controllers).each do |govern|
        govern.destroy
      end
    end

    def analyze_controllers
      present_controllers = Govern.unscoped.select(:identifier).distinct.pluck(:identifier)
      all_controllers = RailsCom::Routes.controllers.except!(*RailsRole.config.ignore_controllers).keys

      missing_controllers = all_controllers - present_controllers
      invalid_controllers = present_controllers - all_controllers
      [missing_controllers, invalid_controllers]
    end

  end

end<commit_msg>simple sync method<commit_after>module RailsRole::Govern
  extend ActiveSupport::Concern

  included do
    attribute :identifier, :string
    attribute :namespace_identifier, :string, default: 'application'
    attribute :business_identifier, :string
    attribute :position, :integer

    belongs_to :name_space, foreign_key: :namespace_identifier, primary_key: :identifier, optional: true
    belongs_to :busyness, foreign_key: :business_identifier, primary_key: :identifier, optional: true

    has_many :rules, -> { order(position: :asc) }, foreign_key: :controller_identifier, primary_key: :identifier, dependent: :destroy
    has_many :role_rules, dependent: :destroy

    accepts_nested_attributes_for :rules, allow_destroy: true

    default_scope -> { order(position: :asc, id: :asc) }

    validates :identifier, uniqueness: true

    after_create_commit :sync_controller

    acts_as_list scope: [:namespace_identifier, :business_identifier]
  end

  def business_name
    t = I18n.t "#{business_identifier}.title", default: nil
    return t if t

    business_identifier
  end

  def namespace_name
    t = I18n.t "#{business_identifier}.#{namespace_identifier}.title", default: nil
    return t if t

    namespace_identifier
  end

  def name
    t = I18n.t "#{identifier.to_s.split('/').join('.')}.index.title", default: nil
    return t if t

    identifier
  end

  class_methods do

    def sync
      present_controllers = Govern.unscoped.select(:identifier).distinct.pluck(:identifier)
      all_controllers = RailsCom::Routes.controllers.except!(*RailsRole.config.ignore_controllers).keys
      missing_controllers = all_controllers - present_controllers
      invalid_controllers = present_controllers - all_controllers

      RailsCom::Routes.controllers.extract!(*missing_controllers).each do |controller, routes|
        govern = Govern.find_or_initialize_by(identifier: controller)
        route = routes[0]
        govern.business_identifier = route[:business] if route[:business]
        govern.namespace_identifier = route[:namespace] if route[:namespace]

        present_rules = govern.rules.pluck(:identifier)
        all_rules = routes.map(&->(i){ i[:action] })
        (all_rules - present_rules).each do |action|
          govern.rules.build(identifier: action, controller_identifier: controller)
        end
        (present_rules - all_rules).each do |action|
          r = govern.rules.find_by(identifier: action)
          r.mark_for_destruction
        end

        govern.save if govern.rules.length > 0
      end

      Govern.where(identifier: invalid_controllers).each do |govern|
        govern.destroy
      end
    end

  end

end
{'fraction_non_alphanumeric': 0.09485294117647058, 'fraction_numerical': 0.0029411764705882353, 'mean_word_length': 3.6306382978723404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6589245', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1526, 'n_words': 359}