Method: Kiss::Action.action_instance_for_path

Defined in:
lib/kiss/action.rb

.action_instance_for_path(path, request, params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/kiss/action.rb', line 20

def action_instance_for_path(path, request, params)
  action_subdir = ''
  action_path = '/'
  action_found = false
  args = []
  objects = {}
  breadcrumbs = []
  object = nil
  object_part = nil
  collection = nil
  
  request.redirect_action('/') if path == ''
  
  action_class = self.get_child_class
  
  parts = path.sub(/\A\/*/, '').split(/\/+/)
  parts.push('') if path[-1, 1] == '/'
  
  while part = parts.shift
    action_path += part
    part = (object_part ? 'view' : @@controller.default_action) if part.empty?
    
    # =column syntax not yet supported
    # when adding this support, be sure to change Action#redirect_action also
    
    if part =~ /\A([\w\-\.\+\%]+)\=([\w\-\.\+\%]+)\Z/
      params[$1.url_unescape] = $2.url_unescape
      action_path += '/' + part
      next
    end
    
    if part !~ /\A[\w\-\.]*\Z/i
      raise Kiss::FileNotFoundError::InvalidAction, 'invalid action path'
    end
              
    if redirect = action_class.aliases[part]
      action, options = redirect
      request.redirect_action(
        absolute_uri( action + '/' + parts.join('/'), action_path ),
        options
      )
    end
    
    test_path = @@controller.action_dir + action_subdir + '/' + part
    if @@controller.directory_exists?(test_path)
      action_subdir += '/' + part
      action_class = action_class.get_child_class(part, true)
      object = object_part = nil
      
      collection = action_class.breadcrumb
      
      action_path += '/'
      if action_class.breadcrumbs.empty?
        if File.file?("#{@@controller.action_dir}#{action_class.name}index.rb") ||
            File.file?("#{@@controller.action_dir}#{action_class.name}index.rhtml")
          breadcrumbs << [action_class.breadcrumb, action_path]
        end
      else
        breadcrumbs.push(*action_class.breadcrumbs)
      end
    else
      # remove .rb extension, if any
      part.sub!(/(\.rb)+\Z/, '')
      
      # does part have an extension?
      if part =~ /\A(.+)\.(\w+)\Z/
        extension = $2
        part = $1
      else
        extension = 'rhtml'
      end
      
      test_path = @@controller.action_dir + action_subdir + '/' + part
      if File.file?("#{test_path}.rb") || File.file?("#{test_path}.#{(extension == 'xls') ? 'txt' : extension}")            
        action_found = true
        action_class = action_class.get_child_class(part)
        break
      else
        unless part =~ /\A[\w\.\-]+\Z/
          raise Kiss::FileNotFoundError, "invalid URI"
        end
        
        object_options = action_class.object_options
        if object_options && (object_options[:column] != :id || part =~ /\A\d+\Z/)
          object_part = part
          object = request.dbm[object_options[:class_name]].find(object_options[:column] => object_part)
          objects[object_options[:variable_name]] = object
          
          action_path += '/'
          
          if !object
            raise Kiss::FileNotFoundError, "resource '#{test_path.html_escape}' not found"
          end
          
          object_breadcrumb = action_class.object_breadcrumb
          object_display_name = object[object.class.display_column] ||
            (object.send(object.class.display_column) rescue nil)
          if (object_breadcrumb != false)
            breadcrumbs << [
              (object_breadcrumb.is_a?(Proc) ? object_breadcrumb.call(object) : object_breadcrumb) ||
                object_display_name,
              action_path
            ]
          end
        
          next
        else
          if part == 'view'
            request.redirect_action(path + '..')
          end
          raise Kiss::FileNotFoundError, "resource '#{test_path.html_escape}' not found"
        end
      end
    end
  end
  
  # if no action found, add a trailing slash and try again
  request.redirect_action(path + '/') unless action_found
  
  # keep rest of path_info in args
  args.push(*parts)
  
  action = action_class.new(request, action_path, extension, collection, object, object_part, object_display_name, breadcrumbs, args, params)
  objects.each_pair do |key, value|
    action.instance_variable_set(key, value)
  end
  action
end