Class: Optimacms::PageServices::PageRouteService

Inherits:
Object
  • Object
show all
Defined in:
lib/optimacms/page_services/page_route_service.rb

Constant Summary collapse

REGEX_VARIABLE =
'[a-z\d\_]+'

Class Method Summary collapse

Class Method Details

.count_url_parts(url) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/optimacms/page_services/page_route_service.rb', line 91

def self.count_url_parts(url)
  url_parts = url.split '/'
  n = url_parts.count

  # if nothing after the last /
  n-=1 if url_parts.last == ''

  n
end

.count_url_vars(url) ⇒ Object



101
102
103
104
105
# File 'lib/optimacms/page_services/page_route_service.rb', line 101

def self.count_url_vars(url)
  #m = url.scan /\{\$[^}]+\}/
  m = url.scan /\:#{REGEX_VARIABLE}/
  m.count
end

.find_page_by_url(url, lang = '') ⇒ Object



5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/optimacms/page_services/page_route_service.rb', line 5

def self.find_page_by_url(url, lang='')
  where_base = 'is_folder = 0 AND enabled = 1 AND '
  order = 'url_parts_count DESC, url_vars_count ASC'

  if url.blank?
    where = where_base + 'url = \'\''
  else
    #where = where_base + '\'' + url + '\' REGEXP parsed_url'
    where = where_base + '\'' + url + '\''+ " REGEXP parsed_url"
  end

  # result
  pagedata = PageData.new
  pagedata.url = url

  # try 1
  rows = Page.where(where).order(order).all
  pagedata.page = rows[0] if rows.count>0

  # try 2
  if pagedata.page.nil? && url.blank?
    where = where_base + "url = '#{lang}'"
    rows = Page.where(where).order(order).all

    pagedata.page = rows[0] if row.count>0
  end

  return pagedata if pagedata.page.nil?


  # params from url
  pagedata.url_vars = get_url_vars(pagedata.url, pagedata.page)

  # controller
  if pagedata.page.controller_action.present?
    pagedata.controller, pagedata.action = parse_controller_action(pagedata.page.controller_action)
  end


  pagedata
end

.get_url_vars(url, page_row) ⇒ Object



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
# File 'lib/optimacms/page_services/page_route_service.rb', line 107

def self.get_url_vars(url, page_row)
  return {} if page_row.nil?
  return {} if url.blank?

  parsed_url = page_row.parsed_url.sub '/', '\/'

  #if (preg_match('/' . $parsed_url . '/', $url, $values_matches)) {
  m_params_in_url = url.scan /#{parsed_url}/

  return nil if m_params_in_url.empty?

  values_matches = m_params_in_url[0]

  # search for variables in URL
  #keys_matches = page_row.url.scan /\{\$([^}\/]+)\}/
  keys_matches = page_row.url.scan /\:(#{REGEX_VARIABLE})/
  if !keys_matches.empty?
    keys_matches = keys_matches.map{|r| r[0]}
    if keys_matches.count == values_matches.count
      res = {}
      values_matches.each_with_index do |val, key|
        res[keys_matches[key].to_sym] = val
      end
      #url_vars = array_combine($keys_matches, $values_matches);
      return res
    else
      #user_error('Keys and values has different number of elements');
      return {}
    end

  elsif m_params_in_url.count == 0
    # no variables found, and parsed_url has no variables => return empty []
    return {}
  else
    # smth wrong
    return {}
  end

end

.make_url(u, p = {}) ⇒ Object



147
148
149
150
151
152
# File 'lib/optimacms/page_services/page_route_service.rb', line 147

def self.make_url(u, p={})
  res = u.gsub(/\:#{REGEX_VARIABLE}/) do
    |name| (p[name[1..-1].to_sym] || '')
  end
  res
end

.parse_controller_action(s) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/optimacms/page_services/page_route_service.rb', line 47

def self.parse_controller_action(s)
  m = s.match /^(\w+)#(\w+)$/
  if m
    [m[1], m[2]]
  else
    return nil
  end
end

.parse_url(url) ⇒ Object



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
# File 'lib/optimacms/page_services/page_route_service.rb', line 59

def self.parse_url(url)
  if url.blank?
    return '^$'
  end

  url_parts = url.split '/'
  a = []
  url_parts.each do |part|
    # check for brackets inside brackets
    return nil if part =~ /\{[^{}]*[{}][^{}]*\}/

    # check if any single brackets or symbol $ without brackets or bracket without $
    return nil if part =~ /(\{[^}]*$|^[^{]*\}|[^{]\$|\{[^\$])/

    p = part

    # variables
    #p.gsub! /\{\$[^}]+\}/, '([^/]+)'
    p.gsub! /(\:#{REGEX_VARIABLE})([-\.]|$)/, '([^/]+)\2'


    # escape system symbols
    # replace '.'
    p.sub! '.', '[.]'


    a << p
  end

  return '^'+url_parts.join('\/')+'$'
end