Module: Variants::Helpers::InstanceMethods

Defined in:
lib/variants/helpers.rb

Instance Method Summary collapse

Instance Method Details

#variant(name, condition = nil) ⇒ Object Also known as: v



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
# File 'lib/variants/helpers.rb', line 6

def variant name, condition = nil

  # If Rails env is development
  if Rails.env.development?
    # Override conditions if params[:variant_name] is presented
    if self.respond_to?(:params) and not self.params[name.to_sym].nil?
      return self.params[name.to_sym].to_s == 'true'
    end
  end

  # If condition is presented, return it
  unless condition.nil?
    return condition
  end

  # Add ? to end of name
  method_name = "#{name.to_s}?"
  # And try to call it
  if self.respond_to?(method_name)
    return self.send(method_name)
  end

  # Else return false
  return false
end