Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_requires.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.action_requires(options) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/action_requires.rb', line 3

def self.action_requires(options)
  unless options.is_a? Hash
    options = {:names => [options].flatten}
  end
  options = {:type => :and}.merge(options)
  options[:names] = [options[:names]].flatten
  big_meth = "action_requires_wrapper_" << options[:names].join("_#{options[:type].to_s}_")
  options[:names].each do |meth|
    unless method_defined?(meth)
      define_method("action_requires_#{meth}") do
        return !params[meth.to_sym].blank?
      end
    end
  end
  unless method_defined?(big_meth)
    delim = case options[:type]
    when :and
      " && "
    when :or
      " || "
    end
    eval %{
      def #{big_meth}
        unless #{options[:names].collect {|meth| "action_requires_#{meth}"}.join(delim)}
          send_to_unknown
          # return false
        end
      end
    }
  end
  options.delete(:names)
  options.delete(:type)
  before_filter big_meth.to_sym, options
end

Instance Method Details

#action_requires_idObject



38
39
40
41
# File 'lib/action_requires.rb', line 38

def action_requires_id
  param = params[:id]
  return !(param.blank? || param == "null" || (param.match(/^\D+/) unless param.is_a? Numeric))
end

#action_requires_xhrObject



43
44
45
# File 'lib/action_requires.rb', line 43

def action_requires_xhr
  send_to_unknown if !request.xhr?
end

#send_to_unknownObject



47
48
49
50
# File 'lib/action_requires.rb', line 47

def send_to_unknown
  @orig_params = params
  render :text => "!404!", :status => "404" unless performed?
end