<commit_before>module Typus

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    ##
    # Return model fields as an array
    #
    def model_fields
      columns.map { |u| [u.name, u.type.to_s] }
    end

    ##
    # Return model fields as a hash
    #
    def model_fields_hash
      hash = Hash.new
      columns.map { |u| hash[u.name.to_sym] = u.type.to_sym }
      return hash
    end

    ##
    #
    #
    def humanize
      name.titleize.capitalize
    end

    ##
    # Form and list fields
    #
    # Someday we could use something like:
    #   typus_list_fields :name, :created_at, :updated_at, :status
    #   typus_form_fields :name, :body, :excerpt, :created_at
    #
    def typus_fields_for(filter)

      fields_with_type = []
      fields = Typus::Configuration.config["#{self.name}"]["fields"][filter].split(", ")

      fields.each do |f|

        ##
        # Get the field_type for each field
        self.model_fields.each do |af|
          @field_type = af.last if af.first == f
        end

        ##
        # Some custom field_type depending on the attribute name
        case f
          when 'parent_id':       @field_type = 'tree'
          when /_id/:             @field_type = 'collection'
          when /file_name/:       @field_type = 'file'
          when /password/:        @field_type = 'password'
          when 'position':        @field_type = 'position'
          else @field_type = 'string' if @field_type == ""
        end

        ##
        # @field_type = (eval f.upcase) rescue @field_type
        @field_type = 'selector' if @field_type.class == Array
        fields_with_type << [ f, @field_type ]
        @field_type = ""

      end

      return fields_with_type

    rescue
      self.model_fields
    end

    ##
    # Typus sidebar filters.
    #
    # Someday we could use something like:
    #   typus_filters :created_at, :status
    #
    def typus_filters
      available_fields = self.model_fields
      return [] unless Typus::Configuration.config["#{self.name}"]["filters"]
      fields = Typus::Configuration.config["#{self.name}"]["filters"].split(", ")
      fields_with_type = []
      fields.each do |f|
        available_fields.each do |af|
          @field_type = af[1] if af[0] == f
        end
        fields_with_type << [ f, @field_type ]
      end
      return fields_with_type
    end

    ##
    #  Extended actions for this model on Typus.
    #
    # Someday we could use something like:
    #     typus_list_actions :action_one
    #     typus_form_actions :action_two, :action_three
    #
    def typus_actions_for(filter)
      Typus::Configuration.config["#{self.name}"]["actions"][filter].split(", ") rescue []
    end

    ##
    # Used for +order_by+, +search+ and more ...
    #
    # Someday we could use something like:
    #     typus_search :title, :details
    #     typus_related :tags, :categories
    #     typus_order_by :title, :created_at
    #
    # Default order is ASC, except for datetime items that is DESC.
    #
    def typus_defaults_for(filter)
      Typus::Configuration.config["#{self.name}"][filter].split(", ") rescue []
    end

    ##
    # Used for +relationships+
    #
    def typus_relationships_for(filter)
      Typus::Configuration.config["#{self.name}"]["relationships"][filter].split(", ") rescue []
    end

    def typus_order_by<commit_msg>Models can now define how Typus should behave ...<commit_after>module Typus

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods

    ##
    # FIXME: Currently not doing what we want to ...
    #
    def list_fields(*options)
      fields = []
      options.each { |o| fields << o if o.is_a? Symbol }
      return fields
    end

    ##
    # Return model fields as an array
    #
    def model_fields
      columns.map { |u| [u.name, u.type.to_s] }
    end

    ##
    # Return model fields as a hash
    #
    def model_fields_hash
      hash = Hash.new
      columns.map { |u| hash[u.name.to_sym] = u.type.to_sym }
      return hash
    end

    ##
    #
    #
    def humanize
      name.titleize.capitalize
    end

    ##
    # Form and list fields
    #
    #   class Post < ActiveRecord::Base
    #
    #     def self.list_fields
    #       [ :title, :category_id, :status ]
    #     end
    #
    #     def self.form_fields
    #       [ :title, :body, :excerpt, :category_id, :status ]
    #     end
    #
    #   end
    #
    def typus_fields_for(filter)

      fields_with_type = []

      begin
        fields = self.send("#{filter}_fields").map { |a| a.to_s }
      rescue
        fields = Typus::Configuration.config["#{self.name}"]["fields"][filter].split(", ")
      end

      fields.each do |f|

        ##
        # Get the field_type for each field
        self.model_fields.each do |af|
          @field_type = af.last if af.first == f
        end

        ##
        # Some custom field_type depending on the attribute name
        case f
          when 'parent_id':       @field_type = 'tree'
          when /_id/:             @field_type = 'collection'
          when /file_name/:       @field_type = 'file'
          when /password/:        @field_type = 'password'
          when 'position':        @field_type = 'position'
          else @field_type = 'string' if @field_type == ""
        end

        ##
        # @field_type = (eval f.upcase) rescue @field_type
        @field_type = 'selector' if @field_type.class == Array
        fields_with_type << [ f, @field_type ]
        @field_type = ""

      end

      return fields_with_type

    rescue
      self.model_fields
    end

    ##
    # Typus sidebar filters.
    #
    #   class Post < ActiveRecord::Base
    #
    #     def self.filters
    #       [ :created_at, :status ]
    #     end
    #
    #   end
    #
    def typus_filters
      available_fields = self.model_fields
      begin
        fields = self.filters.map { |a| a.to_s }
      rescue
        return [] unless Typus::Configuration.config["#{self.name}"]["filters"]
        fields = Typus::Configuration.config["#{self.name}"]["filters"].split(", ")
      end
      fields_with_type = []
      fields.each do |f|
        available_fields.each do |af|
          @field_type = af[1] if af[0] == f
        end
        fields_with_type << [ f, @field_type ]
      end
      return fields_with_type
    end

    ##
    #  Extended actions for this model on Typus.
    #
    #    class Post < ActiveRecord::Base
    #
    #      def self.list_actions
    #        [ :rebuild_all ]
    #      end
    #
    #      def self.form_actions
    #        [ :rebuild, :notify ]
    #      end
    #
    #    end
    #
    def typus_actions_for(filter)
      begin
        self.send("#{filter}_actions").map { |a| a.to_s }
      rescue
        Typus::Configuration.config["#{self.name}"]["actions"][filter].split(", ") rescue []
      end
    end

    ##
    # Used for +order_by+, +search+ and more ...
    #
    # Someday we could use something like:
    #
    #     typus_search :title, :details
    #     typus_related :tags, :categories
    #
    # Default order is ASC, except for datetime items that is DESC.
    #
    def typus_defaults_for(filter)
      Typus::Configuration.config["#{self.name}"][filter].split(", ") rescue []
    end

    ##
    # Used for +relationships+
    #
    def typus_relationships_for(filter)
      Typus::Configuration.config["#{self.name}"]["relationships"][filter].split(", ") rescue []
    end

    def typus_order_by
{'fraction_non_alphanumeric': 0.1177406523468576, 'fraction_numerical': 0.0005303632988597189, 'mean_word_length': 1.9673485444531864, 'pattern_counts': {'":': 0, '<': 16, '<?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': '4893924', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2391, 'n_words': 555}