<commit_before># Base methods for CRUD actions
# Simply override any methods in your action controller you want to be customised
# Don't forget to add:
#   map.resources :plural_model_name_here
# to your routes.rb file.
# Full documentation about CRUD and resources go here:
# -> http://caboo.se/doc/classes/ActionController/Resources.html#M003716
# Example (add to your controller):
# crudify :foo, {:title_attribute => 'name'}

module Crud

  def self.append_features(base)
    super
    base.extend(ClassMethods)
  end

  module ClassMethods

    def crudify(model_name, new_options = {})
      singular_name = model_name.to_s
      class_name = singular_name.camelize
      plural_name = singular_name.pluralize

      options = {
        :title_attribute => "title",
        :order => 'position ASC',
        :conditions => '',
        :sortable => true,
        :searchable => true,
        :include => [],
        :paging => true,
        :search_conditions => '',
        :redirect_to_url => "admin_#{plural_name}_url"
      }.merge!(new_options)

      module_eval %(
        before_filter :find_#{singular_name}, :only => [:update, :destroy, :edit, :show]

        def new
          @#{singular_name} = #{class_name}.new
        end

        def create
          # if the position field exists, set this object as last object, given the conditions of this class.
          if #{class_name}.column_names.include?("position")
            params[:#{singular_name}].merge!({:position => ((#{class_name}.maximum(:position, :conditions => "#{options[:conditions]}")||-1) + 1)})
          end

          if (@#{singular_name} = #{class_name}.create(params[:#{singular_name}])).valid?
            unless request.xhr?
              flash[:notice] = t('refinery.crudify.created', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            else
              flash.now[:notice] = t('refinery.crudify.created', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            end
            unless from_dialog?
              unless params[:continue_editing] =~ /true|on|1/
                redirect_back_or_default(#{options[:redirect_to_url]})
              else
                unless request.xhr?
                  redirect_to :back
                else
                  render :partial => "/shared/message"
                end
              end
            else
              render :text => "<script type='text/javascript'>parent.window.location = '\#{#{options[:redirect_to_url]}}';</script>"
            end
          else
            unless request.xhr?
              render :action => 'new'
            else
              render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
            end
          end
        end

        def edit
          @#{singular_name} = #{class_name}.find(params[:id])
        end

        def update
          if @#{singular_name}.update_attributes(params[:#{singular_name}])
            unless request.xhr?
              flash[:notice] = t('refinery.crudify.updated', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            else
              flash.now[:notice] = t('refinery.crudify.updated', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            end
            unless from_dialog?
              unless params[:continue_editing] =~ /true|on|1/
                redirect_back_or_default(#{options[:redirect_to_url]})
              else
                unless request.xhr?
                  redirect_to :back
                else
                  render :partial => "/shared/message"
                end
              end
            else
              render :text => "<script type='text/javascript'>parent.window.location = '\#{#{options[:redirect_to_url]}}';</script>"
            end
          else
            unless request.xhr?
              render :action => 'edit'
            else
              render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
            end
          end
        end

        def destroy
          flash.now[:notice] = t('refinery.crudify.destroyed', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'") if @#{singular_name}.destroy
          redirect_to #{options[:redirect_to_url]}
        end

        def find_#{singular_name}
          @#{singular_name} = #{class_name}.find(params[:id], :include => %w(#{options[:include].join(' ')}))
        end

        def find_all_#{plural_name}
          @#{plural_name} = #{class_name}.find  :all,
                                                :order => "#{options[:order]}",
                                                :conditions => "#{options[:conditions]}",
                                                :include => %w(#{options[:include].join(' ')})
        end

        def paginate_all_#{plural_name}
          @#{plural_name} = #{class_name}.paginate :page => params[:page],
                                                   :order => "#{options[:order]}",
                                                   :conditions => "#{options[:conditions]}",
                                                   :include => %w(#{options[:include].join(' ')})
        end

        def search_all_#{plural_name}
          @#{plural_name} = #{class_name}.find_with_index params[:search],
                                                   :order => "#{options[:order]}",
                                                   :conditions => "#{options[:search_conditions]}",<commit_msg>flash.now was inhibiting destroy flashes as revealed in cucumber features.<commit_after># Base methods for CRUD actions
# Simply override any methods in your action controller you want to be customised
# Don't forget to add:
#   map.resources :plural_model_name_here
# to your routes.rb file.
# Full documentation about CRUD and resources go here:
# -> http://caboo.se/doc/classes/ActionController/Resources.html#M003716
# Example (add to your controller):
# crudify :foo, {:title_attribute => 'name'}

module Crud

  def self.append_features(base)
    super
    base.extend(ClassMethods)
  end

  module ClassMethods

    def crudify(model_name, new_options = {})
      singular_name = model_name.to_s
      class_name = singular_name.camelize
      plural_name = singular_name.pluralize

      options = {
        :title_attribute => "title",
        :order => 'position ASC',
        :conditions => '',
        :sortable => true,
        :searchable => true,
        :include => [],
        :paging => true,
        :search_conditions => '',
        :redirect_to_url => "admin_#{plural_name}_url"
      }.merge!(new_options)

      module_eval %(
        before_filter :find_#{singular_name}, :only => [:update, :destroy, :edit, :show]

        def new
          @#{singular_name} = #{class_name}.new
        end

        def create
          # if the position field exists, set this object as last object, given the conditions of this class.
          if #{class_name}.column_names.include?("position")
            params[:#{singular_name}].merge!({:position => ((#{class_name}.maximum(:position, :conditions => "#{options[:conditions]}")||-1) + 1)})
          end

          if (@#{singular_name} = #{class_name}.create(params[:#{singular_name}])).valid?
            unless request.xhr?
              flash[:notice] = t('refinery.crudify.created', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            else
              flash.now[:notice] = t('refinery.crudify.created', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            end
            unless from_dialog?
              unless params[:continue_editing] =~ /true|on|1/
                redirect_back_or_default(#{options[:redirect_to_url]})
              else
                unless request.xhr?
                  redirect_to :back
                else
                  render :partial => "/shared/message"
                end
              end
            else
              render :text => "<script type='text/javascript'>parent.window.location = '\#{#{options[:redirect_to_url]}}';</script>"
            end
          else
            unless request.xhr?
              render :action => 'new'
            else
              render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
            end
          end
        end

        def edit
          @#{singular_name} = #{class_name}.find(params[:id])
        end

        def update
          if @#{singular_name}.update_attributes(params[:#{singular_name}])
            unless request.xhr?
              flash[:notice] = t('refinery.crudify.updated', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            else
              flash.now[:notice] = t('refinery.crudify.updated', :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
            end
            unless from_dialog?
              unless params[:continue_editing] =~ /true|on|1/
                redirect_back_or_default(#{options[:redirect_to_url]})
              else
                unless request.xhr?
                  redirect_to :back
                else
                  render :partial => "/shared/message"
                end
              end
            else
              render :text => "<script type='text/javascript'>parent.window.location = '\#{#{options[:redirect_to_url]}}';</script>"
            end
          else
            unless request.xhr?
              render :action => 'edit'
            else
              render :partial => "/shared/admin/error_messages_for", :locals => {:symbol => :#{singular_name}, :object => @#{singular_name}}
            end
          end
        end

        def destroy
          if @#{singular_name}.destroy
            flash[:notice] = t('refinery.crudify.destroyed', 
                               :what => "'\#{@#{singular_name}.#{options[:title_attribute]}}'")
          end
          redirect_to #{options[:redirect_to_url]}
        end

        def find_#{singular_name}
          @#{singular_name} = #{class_name}.find(params[:id], :include => %w(#{options[:include].join(' ')}))
        end

        def find_all_#{plural_name}
          @#{plural_name} = #{class_name}.find  :all,
                                                :order => "#{options[:order]}",
                                                :conditions => "#{options[:conditions]}",
                                                :include => %w(#{options[:include].join(' ')})
        end

        def paginate_all_#{plural_name}
          @#{plural_name} = #{class_name}.paginate :page => params[:page],
                                                   :order => "#{options[:order]}",
                                                   :conditions => "#{options[:conditions]}",
                                                   :include => %w(#{options[:include].join(' ')})
        end

        def search_all_#{plural_name}
          @#{plural_name} = #{class_name}.find_with_index params[:search],
                                                   :order => "#{options[:order]}",
                                                   :conditions => "#{options[:search_conditions]}",
{'fraction_non_alphanumeric': 0.14253095635373672, 'fraction_numerical': 0.0017563888644945992, 'mean_word_length': 1.8328358208955224, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 97, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4210851', 'n_tokens_mistral': 3067, 'n_tokens_neox': 3020, 'n_words': 599}