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



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

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
17
# File 'lib/rtml/controller/state.rb', line 2

def find_or_build_state
  headers = request.headers
  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



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

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

#set_state_valuesObject



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

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 if value
  end
end

#stateObject



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

def state
  @terminal_state
end

#state_variable_mappingObject



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

def state_variable_mapping
  self.class.state_variable_mapping
end

#verify_stateObject



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

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