<commit_before>module ActiveScaffold
  module Core
    def self.included(base)
      base.extend(ClassMethods)
      base.module_eval do
        # TODO: these should be in actions/core
        before_filter :handle_user_settings
        before_filter :check_input_device
      end

      base.helper_method :touch_device?
      base.helper_method :hover_via_click?
      base.helper_method :active_scaffold_constraints
    end

    def active_scaffold_config
      self.class.active_scaffold_config
    end

    def active_scaffold_config_for(klass)
      self.class.active_scaffold_config_for(klass)
    end

    def active_scaffold_session_storage_key(id = nil)
      id ||= params[:eid] || "#{params[:controller]}#{"_#{nested_parent_id}" if nested?}"
      "as:#{id}"
    end

    def active_scaffold_session_storage(id = nil)
      session_index = active_scaffold_session_storage_key(id)
      session[session_index] ||= {}
      session[session_index]
    end

    def clear_storage
      session_index = active_scaffold_session_storage_key
      session.delete(session_index) unless session[session_index].present?
    end

    # at some point we need to pass the session and params into config. we'll just take care of that before any particular action occurs by passing those hashes off to the UserSettings class of each action.
    def handle_user_settings
      if self.class.uses_active_scaffold?
        active_scaffold_config.actions.each do |action_name|
          conf_instance = active_scaffold_config.send(action_name) rescue next
          next if conf_instance.class::UserSettings == ActiveScaffold::Config::Base::UserSettings # if it hasn't been extended, skip it
          storage = active_scaffold_config.store_user_settings ? active_scaffold_session_storage : {}
          conf_instance.user = conf_instance.class::UserSettings.new(conf_instance, storage, params)
        end
      end
    end

    def check_input_device
      if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod|iPad)/i]
        session[:input_device_type] = 'TOUCH'
        session[:hover_supported] = false
      else
        session[:input_device_type] = 'MOUSE'
        session[:hover_supported] = true
      end if session[:input_device_type].nil?
     end

    def touch_device?
      session[:input_device_type] == 'TOUCH'
    end

    def hover_via_click?
      session[:hover_supported] == false
    end
      
    module ClassMethods
      def active_scaffold(model_id = nil, &block)
        extend Prefixes
        # initialize bridges here
        ActiveScaffold::Bridges.run_all

        # converts Foo::BarController to 'bar' and FooBarsController to 'foo_bar' and AddressController to 'address'
        model_id = self.to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore unless model_id

        # run the configuration
        @active_scaffold_config = ActiveScaffold::Config::Core.new(model_id)
        @active_scaffold_config_block = block
        self.links_for_associations
        
        self.active_scaffold_superclasses_blocks.each {|superblock| self.active_scaffold_config.configure &superblock}
        self.active_scaffold_config.sti_children = nil # reset sti_children if set in parent block
        self.active_scaffold_config.configure &block if block_given?
        self.active_scaffold_config._configure_sti unless self.active_scaffold_config.sti_children.nil?
        self.active_scaffold_config._load_action_columns

        # defines the attribute read methods on the model, so record.send() doesn't find protected/private methods instead
        klass = self.active_scaffold_config.model
        klass.define_attribute_methods unless klass.attribute_methods_generated?
        # include the rest of the code into the controller: the action core and the included actions
        module_eval do
          include ActiveScaffold::Finder
          include ActiveScaffold::Constraints
          include ActiveScaffold::AttributeParams
          include ActiveScaffold::Actions::Core
          active_scaffold_config.actions.each do |mod|
            name = mod.to_s.camelize
            include "ActiveScaffold::Actions::#{name}".constantize

            # sneak the action links from the actions into the main set<commit_msg>Work around removed attribute_methods_generated? in Rails 4.0.4<commit_after>module ActiveScaffold
  module Core
    def self.included(base)
      base.extend(ClassMethods)
      base.module_eval do
        # TODO: these should be in actions/core
        before_filter :handle_user_settings
        before_filter :check_input_device
      end

      base.helper_method :touch_device?
      base.helper_method :hover_via_click?
      base.helper_method :active_scaffold_constraints
    end

    def active_scaffold_config
      self.class.active_scaffold_config
    end

    def active_scaffold_config_for(klass)
      self.class.active_scaffold_config_for(klass)
    end

    def active_scaffold_session_storage_key(id = nil)
      id ||= params[:eid] || "#{params[:controller]}#{"_#{nested_parent_id}" if nested?}"
      "as:#{id}"
    end

    def active_scaffold_session_storage(id = nil)
      session_index = active_scaffold_session_storage_key(id)
      session[session_index] ||= {}
      session[session_index]
    end

    def clear_storage
      session_index = active_scaffold_session_storage_key
      session.delete(session_index) unless session[session_index].present?
    end

    # at some point we need to pass the session and params into config. we'll just take care of that before any particular action occurs by passing those hashes off to the UserSettings class of each action.
    def handle_user_settings
      if self.class.uses_active_scaffold?
        active_scaffold_config.actions.each do |action_name|
          conf_instance = active_scaffold_config.send(action_name) rescue next
          next if conf_instance.class::UserSettings == ActiveScaffold::Config::Base::UserSettings # if it hasn't been extended, skip it
          storage = active_scaffold_config.store_user_settings ? active_scaffold_session_storage : {}
          conf_instance.user = conf_instance.class::UserSettings.new(conf_instance, storage, params)
        end
      end
    end

    def check_input_device
      if request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod|iPad)/i]
        session[:input_device_type] = 'TOUCH'
        session[:hover_supported] = false
      else
        session[:input_device_type] = 'MOUSE'
        session[:hover_supported] = true
      end if session[:input_device_type].nil?
     end

    def touch_device?
      session[:input_device_type] == 'TOUCH'
    end

    def hover_via_click?
      session[:hover_supported] == false
    end
      
    module ClassMethods
      def active_scaffold(model_id = nil, &block)
        extend Prefixes
        # initialize bridges here
        ActiveScaffold::Bridges.run_all

        # converts Foo::BarController to 'bar' and FooBarsController to 'foo_bar' and AddressController to 'address'
        model_id = self.to_s.split('::').last.sub(/Controller$/, '').pluralize.singularize.underscore unless model_id

        # run the configuration
        @active_scaffold_config = ActiveScaffold::Config::Core.new(model_id)
        @active_scaffold_config_block = block
        self.links_for_associations
        
        self.active_scaffold_superclasses_blocks.each {|superblock| self.active_scaffold_config.configure &superblock}
        self.active_scaffold_config.sti_children = nil # reset sti_children if set in parent block
        self.active_scaffold_config.configure &block if block_given?
        self.active_scaffold_config._configure_sti unless self.active_scaffold_config.sti_children.nil?
        self.active_scaffold_config._load_action_columns

        # defines the attribute read methods on the model, so record.send() doesn't find protected/private methods instead
        klass = self.active_scaffold_config.model
        # Rails 4.0.4 has removed attribute_methods_generated,
        # and made define_attribute_methods threadsave to call multiple times.
        # Check for that here.
        if ((Rails::VERSION::MAJOR == 4) && !klass.respond_to?(:attribute_methods_generated)) ||
            !klass.attribute_methods_generated?
          klass.define_attribute_methods
        end
        # include the rest of the code into the controller: the action core and the included actions
        module_eval do
          include ActiveScaffold::Finder
          include ActiveScaffold::Constraints
          include ActiveScaffold::AttributeParams
          include ActiveScaffold::Actions::Core
          active_scaffold_config.actions.each do |mod|
            name = mod.to_s.camelize
            include "ActiveScaffold::Actions::#{name}".constantize

            # sneak the action links from the actions into the main set
{'fraction_non_alphanumeric': 0.0735195530726257, 'fraction_numerical': 0.0007821229050279329, 'mean_word_length': 3.297167546807489, 'pattern_counts': {'":': 0, '<': 3, '<?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': '635745', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2546, 'n_words': 584}