<commit_before>require File.join(File.dirname(__FILE__), 'toolbar')
require File.join(File.dirname(__FILE__), 'toolbar_item')
require File.join(File.dirname(__FILE__), 'column')
require File.join(File.dirname(__FILE__), 'grid_styling')
require File.join(File.dirname(__FILE__), 'grid_columns')
require File.join(File.dirname(__FILE__), 'grid_toolbar')

module WulinMaster
  class Grid
    include GridStyling
    include GridColumns
    include GridToolbar
    
    cattr_accessor :grids
    class_attribute :controller_class, :_actions , :_title, :_model, :_path
    @@grids = []

    # Grid has been subclassed
    def self.inherited(klass)
      @@grids << klass
      klass.init
    end

    # Class methods
    # -------------------
    class << self
      # Called when the grid is subclassed
      def init
        initialize_columns
        initialize_toolbar
      end
      
      def actions
        self._actions ||= %w(add delete edit)
      end

      [:title, :model, :path].each do |attr|
        define_method attr do |*new_attr|
          (new_attr.size > 0) ? self.send("_#{attr}=".to_sym, new_attr.first) : self.send("_#{attr}".to_sym)
        end
      end

      def set_actions(*args)
        actions_str = args.map(&:to_s)
        self.toolbar.delete_if{ |t| t.title.downcase == 'add' } unless actions_str.include?('add')
        self.toolbar.delete_if{ |t| t.title.downcase == 'delete' } unless actions_str.include?('delete')
        self._actions = actions_str
      end
    end

    # Instance methods
    # --------------------
    attr_accessor :controller

    def initialize(controller_instance)
      self.controller = controller_instance
    end

    # Grid Properties that can be overriden
    def title
      self.class.title || self.class.to_s.humanize
    end

    def model
      self.class.model
    end

    def path
      self.class.path
    end

    def name
      self.class.to_s.sub('Grid', '').underscore
    end

    # Helpers for SQL and Javascript generation
    # ----------
    def sql_columns
      self.columns.map(&:sql_names).flatten.uniq.map(&:to_s)
    end

    def apply_filter(query, column_name, filtering_value)
      column = self.columns.find{|c| c.name.to_s == column_name.to_s}
      column.nil? ? query : column.apply_filter(query, filtering_value)
    end

    # Returns the includes to add to the query
    def includes
      self.columns.map{|col| col.includes}.flatten.uniq
    end

    # Returns the joins to add to the query
    def joins
      self.columns.map{|col| col.joins}.flatten.uniq
    end

    def arraify(objects)
      objects.collect do |object|
        self.columns.collect {|col| {col.name => col.json(object)} }
      end
    end

    def javascript_column_model
      @javascript_column_model = self.columns.collect(&:to_column_model).to_json
    end

    # State

    def states_for_user(user)
      return "false" if user.nil?
      result = {}
      begin
        states = GridState.where(:user_id => user.id, :grid_name => self.name)
        ["width", "sort", "order", "visibility"].each do |t|
          value = states.where(:state_type => t).first.try(:state_value)
          result.merge!(t => ActiveSupport::JSON.decode(value)) if value
        end
        result.to_json
      rescue Exception => e
        Rails.logger.info "Exception thrown while trying to get user states: #{e.inspect}"
        {}.to_json
      end
    end

    def get_actions
      puts "****************#{self.class.name}**************"<commit_msg>MISC - Remove comment types"<commit_after>require File.join(File.dirname(__FILE__), 'toolbar')
require File.join(File.dirname(__FILE__), 'toolbar_item')
require File.join(File.dirname(__FILE__), 'column')
require File.join(File.dirname(__FILE__), 'grid_styling')
require File.join(File.dirname(__FILE__), 'grid_columns')
require File.join(File.dirname(__FILE__), 'grid_toolbar')

module WulinMaster
  class Grid
    include GridStyling
    include GridColumns
    include GridToolbar
    
    cattr_accessor :grids
    class_attribute :controller_class, :_actions , :_title, :_model, :_path
    @@grids = []

    # Grid has been subclassed
    def self.inherited(klass)
      @@grids << klass
      klass.init
    end

    # Class methods
    # -------------------
    class << self
      # Called when the grid is subclassed
      def init
        initialize_columns
        initialize_toolbar
      end
      
      def actions
        self._actions ||= %w(add delete edit)
      end

      [:title, :model, :path].each do |attr|
        define_method attr do |*new_attr|
          (new_attr.size > 0) ? self.send("_#{attr}=".to_sym, new_attr.first) : self.send("_#{attr}".to_sym)
        end
      end

      def set_actions(*args)
        actions_str = args.map(&:to_s)
        self.toolbar.delete_if{ |t| t.title.downcase == 'add' } unless actions_str.include?('add')
        self.toolbar.delete_if{ |t| t.title.downcase == 'delete' } unless actions_str.include?('delete')
        self._actions = actions_str
      end
    end

    # Instance methods
    # --------------------
    attr_accessor :controller

    def initialize(controller_instance)
      self.controller = controller_instance
    end

    # Grid Properties that can be overriden
    def title
      self.class.title || self.class.to_s.humanize
    end

    def model
      self.class.model
    end

    def path
      self.class.path
    end

    def name
      self.class.to_s.sub('Grid', '').underscore
    end

    # Helpers for SQL and Javascript generation
    # ----------
    def sql_columns
      self.columns.map(&:sql_names).flatten.uniq.map(&:to_s)
    end

    def apply_filter(query, column_name, filtering_value)
      column = self.columns.find{|c| c.name.to_s == column_name.to_s}
      column.nil? ? query : column.apply_filter(query, filtering_value)
    end

    # Returns the includes to add to the query
    def includes
      self.columns.map{|col| col.includes}.flatten.uniq
    end

    # Returns the joins to add to the query
    def joins
      self.columns.map{|col| col.joins}.flatten.uniq
    end

    def arraify(objects)
      objects.collect do |object|
        self.columns.collect {|col| {col.name => col.json(object)} }
      end
    end

    def javascript_column_model
      @javascript_column_model = self.columns.collect(&:to_column_model).to_json
    end

    # State

    def states_for_user(user)
      return "false" if user.nil?
      result = {}
      begin
        states = GridState.where(:user_id => user.id, :grid_name => self.name)
        ["width", "sort", "order", "visibility"].each do |t|
          value = states.where(:state_type => t).first.try(:state_value)
          result.merge!(t => ActiveSupport::JSON.decode(value)) if value
        end
        result.to_json
      rescue Exception => e
        Rails.logger.info "Exception thrown while trying to get user states: #{e.inspect}"
        {}.to_json
      end
    end

    def get_actions
{'fraction_non_alphanumeric': 0.1265913317122014, 'fraction_numerical': 0.00028608210556429694, 'mean_word_length': 2.4925074925074924, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '1051634', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2137, 'n_words': 504}