<commit_before>module ExtJS
  module Model
    
    def self.included(model)
      model.send(:extend, ClassMethods)
      model.send(:include, InstanceMethods)
      model.class_eval do
        ##
        # @config {Array} List of DataReader fields to render.  This should probably not be a cattr_accessor
        # since it's only used internally.  The user adds fields via the Class method extjs_fields instead.
        #
        cattr_accessor :extjs_record_fields
        ##
        # @config {String} extjs_mapping_template This a template used to render mapped field-names.
        # One could use the Rails standard "{association}[{property}]" as well.
        #
        cattr_accessor :extjs_mapping_template
      end
      model.extjs_record_fields = []
      model.extjs_mapping_template = "_{property}"
    end

    ##
    # InstanceMethods
    #
    module InstanceMethods
      
      ##
      # Converts a model instance to a record compatible with ExtJS
      # @params {Mixed} params A list of fields to use instead of this Class's extjs_record_fields
      #
      def to_record(*params)
        if self.class.extjs_record_fields.empty?
          self.class.extjs_fields(*self.class.extjs_column_names)
        end
        
        fields  = (params.empty?) ? self.class.extjs_record_fields : self.class.process_fields(*params)
        assns   = self.class.extjs_associations
        pk      = self.class.extjs_primary_key
        
        # build the initial field data-hash
        data    = {pk.to_s => self.send(pk)}
         
        fields.each do |field|
          if refl = assns[field[:name]] || assns[field[:name].to_sym]
            if refl[:type] === :belongs_to
              assn = self.send(field[:name])
              
              if assn.respond_to?(:to_record)
                data[field[:name]] = assn.to_record field[:fields]
              elsif (field[:fields])
                data[field[:name]] = {}
                field[:fields].each do |property|
                  data[field[:name]][property] = assn.send(property) if assn.respond_to?(property)
                end
              else
                data[field[:name]] = {} # belongs_to assn that doesn't respond to to_record and no fields list
              end
              # Append associations foreign_key to data
              data[refl[:foreign_key].to_s] = self.send(refl[:foreign_key])
            elsif refl[:type] === :many
              data[field[:name]] = self.send(field[:name]).collect {|r| r.to_record}  #CAREFUL!!!!!!!!!!!!1
            end
          else
            value = self.send(field[:name])
            data[field[:name]] = value.respond_to?(:to_record) ? value.to_record : value
          end
        end
        data
      end
    end
    
    ##
    # ClassMethods
    #
    module ClassMethods
      ##
      # render AR columns to Ext.data.Record.create format
      # eg: {name:'foo', type: 'string'}<commit_msg>Fixed a bug caused by the new way of processing field configurations<commit_after>module ExtJS
  module Model
    
    def self.included(model)
      model.send(:extend, ClassMethods)
      model.send(:include, InstanceMethods)
      model.class_eval do
        ##
        # @config {Array} List of DataReader fields to render.  This should probably not be a cattr_accessor
        # since it's only used internally.  The user adds fields via the Class method extjs_fields instead.
        #
        cattr_accessor :extjs_record_fields
        ##
        # @config {String} extjs_mapping_template This a template used to render mapped field-names.
        # One could use the Rails standard "{association}[{property}]" as well.
        #
        cattr_accessor :extjs_mapping_template
      end
      model.extjs_record_fields = []
      model.extjs_mapping_template = "_{property}"
    end

    ##
    # InstanceMethods
    #
    module InstanceMethods
      
      ##
      # Converts a model instance to a record compatible with ExtJS
      # @params {Mixed} params A list of fields to use instead of this Class's extjs_record_fields
      #
      def to_record(*params)
        if self.class.extjs_record_fields.empty?
          self.class.extjs_fields(*self.class.extjs_column_names)
        end
        
        fields  = (params.empty?) ? self.class.extjs_record_fields : self.class.process_fields(*params)
        assns   = self.class.extjs_associations
        pk      = self.class.extjs_primary_key
        
        # build the initial field data-hash
        data    = {pk.to_s => self.send(pk)}
         
        fields.each do |field|
          if refl = assns[field[:name]] || assns[field[:name].to_sym]
            if refl[:type] === :belongs_to
              assn = self.send(field[:name])
              
              if assn.respond_to?(:to_record)
                data[field[:name]] = assn.to_record field[:fields]
              elsif (field[:fields])
                data[field[:name]] = {}
                field[:fields].each do |property|
                  data[field[:name]][property[:name]] = assn.send(property[:name]) if assn.respond_to?(property[:name])
                end
              else
                data[field[:name]] = {} # belongs_to assn that doesn't respond to to_record and no fields list
              end
              # Append associations foreign_key to data
              data[refl[:foreign_key].to_s] = self.send(refl[:foreign_key])
            elsif refl[:type] === :many
              data[field[:name]] = self.send(field[:name]).collect {|r| r.to_record}  #CAREFUL!!!!!!!!!!!!1
            end
          else
            value = self.send(field[:name])
            data[field[:name]] = value.respond_to?(:to_record) ? value.to_record : value
          end
        end
        data
      end
    end
    
    ##
    # ClassMethods
    #
    module ClassMethods
      ##
      # render AR columns to Ext.data.Record.create format
      # eg: {name:'foo', type: 'string'}
{'fraction_non_alphanumeric': 0.10281784636028178, 'fraction_numerical': 0.0003354579000335458, 'mean_word_length': 2.0116161616161614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1601426', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1687, 'n_words': 416}