Sha256: 4aa165d69e1b4ccc5e9fb3e4ec70cc6c0d5a868607478f5dba911c42f937206e

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

module ActiveScaffold::Actions
  module Mark

    def self.included(base)
      base.before_filter :mark_authorized?, :only => [:mark_all]
      base.prepend_before_filter :assign_marked_records_to_model
      base.helper_method :marked_records
    end

    def mark_all
      if mark_all?
        do_mark_all
      else
        do_demark_all
      end
      do_list
      respond_to_action(:list) 
    end
 protected
 
    # We need to give the ActiveRecord classes a handle to currently marked records. We don't want to just pass the object,
    # because the object may change. So we give ActiveRecord a proc that ties to the
    # marked_records_method on this ApplicationController.
    def assign_marked_records_to_model
      active_scaffold_config.model.marked_records = marked_records
    end
    
    def marked_records
      active_scaffold_session_storage[:marked_records] ||= Set.new
    end
    
    def mark_all?
      @mark_all ||= [true, 'true', 1, '1', 'T', 't'].include?(params[:value].class == String ? params[:value].downcase : params[:value])
    end

    def do_mark_all
      each_record_in_scope {|record| marked_records << record.id}
    end

    def do_demark_all
      each_record_in_scope {|record| marked_records.delete(record.id)}
    end
    
    # The default security delegates to ActiveRecordPermissions.
    # You may override the method to customize.
    def mark_authorized?
      authorized_for?(:action => :read)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_scaffold_vho-3.0.12 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.11 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.10 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.9 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.8 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.7 lib/active_scaffold/actions/mark.rb
active_scaffold_vho-3.0.6 lib/active_scaffold/actions/mark.rb