Class: RoutingFilter::SectionRouter

Inherits:
Filter
  • Object
show all
Defined in:
lib/knitkit/routing_filter/section_router.rb

Instance Method Summary collapse

Instance Method Details

#around_generate(params, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/knitkit/routing_filter/section_router.rb', line 28

def around_generate(params, &block)   
  yield.tap do |path|
    result = result.first if result.is_a?(Array)
    if result !~ %r(^/([\w]{2,4}/)?admin) and result =~ generate_pattern
      section = WebsiteSection.find $2.to_i
      result.sub! "#{$1}/#{$2}", "#{section.path[1..section.path.length]}#{$3}"
    end
  end
end

#around_recognize(path, env, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/knitkit/routing_filter/section_router.rb', line 7

def around_recognize(path, env, &block)
  website = Website.find_by_host(env["HTTP_HOST"])
  if website
    paths = paths_for_website(website)
    if path.to_sym == :/
      home_page_url = website.configurations.first.get_configuration_item(ConfigurationItemType.find_by_internal_identifier('homepage_url')).options.first.value
      valid_section = website.website_sections.detect{|website_section| website_section.path == home_page_url }
      type = valid_section.type.pluralize.downcase
      path.sub!('/', "/#{$1}#{type}/#{valid_section.id}#{$3}")
    else
      if !Rails.application.config.knitkit.ignored_prefix_paths.include?(path) and path !~ %r(^/([\w]{2,4}/)?admin) and !paths.empty? and path =~ recognize_pattern(paths)
        if section = website_section_by_path(website, $2)
          type = section.type.pluralize.downcase
          path.sub! %r(^/([\w]{2,4}/)?(#{paths})(?=/|\.|$)), "/#{$1}#{type}/#{section.id}#{$3}"
        end
      end
    end
  end
  yield
end