Module: Rtml::Controller::State

Defined in:
lib/rtml/controller/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rtml/controller/state.rb', line 60

def included(base)
  base.instance_eval do
    hide_action :find_or_build_state, :set_state_values, :verify_state, :save_state, :state, :state_variable_mapping
    before_filter :find_or_build_state
    after_filter :save_state
    helper_method :state

    def state_variable_mapping
      @state_variable_mapping ||= Rtml::State::VariableMapping.new
    end
  end
end

Instance Method Details

#find_or_build_stateObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rtml/controller/state.rb', line 2

def find_or_build_state
  if params[:terminal] && params[:terminal][:itid]
    itid = params[:terminal][:itid]
  elsif request.terminal?
    itid = headers['ITID'] || headers['HTTP_ITID'] || params['itid']
  else
    itid = headers['ITID'] || headers['HTTP_ITID'] || params['itid'] || 'web-browser'
  end
  
  @terminal_state = Rtml::State.find_by_itid(itid) || Rtml::State.new(:itid => itid,
                                                                      :model => params[:model])

  set_state_values
  verify_state unless request.post? || params.key?(:ignore_state) || !request.rtml?
end

#save_stateObject



48
49
50
51
52
53
# File 'lib/rtml/controller/state.rb', line 48

def save_state
  if @terminal_state.changed?
    @terminal_state.save # can fail silently
  end
  true
end

#set_state_valuesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rtml/controller/state.rb', line 18

def set_state_values
  state_variable_mapping.each do |tmlvar, state_attribute|
    if params[:terminal] && params[:terminal][state_attribute]
      value = params[:terminal][state_attribute]
    else
      code = "params['#{tmlvar.to_s.gsub(/\./, '\'][\'')}']"
      begin
        value = eval(code, binding, __FILE__, __LINE__)
      rescue NoMethodError
        value = nil
      end
    end
    state[state_attribute] = value
  end
end

#stateObject



44
45
46
# File 'lib/rtml/controller/state.rb', line 44

def state
  @terminal_state
end

#state_variable_mappingObject



55
56
57
# File 'lib/rtml/controller/state.rb', line 55

def state_variable_mapping
  self.class.state_variable_mapping
end

#verify_stateObject



34
35
36
37
38
39
40
41
42
# File 'lib/rtml/controller/state.rb', line 34

def verify_state
  if @terminal_state.new_record?
    Rails.logger.debug "Terminal state for ITID #{@terminal_state[:itid]} needs to be created"
    render :rtml => 'get_state'
  elsif @terminal_state.updated_at < 1.day.ago
    Rails.logger.debug "Terminal state for ITID #{@terminal_state[:itid]} needs to be updated"
    render :rtml => 'get_state'
  end
end