Class: Lux::Application::Nav

Inherits:
Object
  • Object
show all
Defined in:
lib/lux/application/lib/nav.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Nav

acepts path as a string



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lux/application/lib/nav.rb', line 10

def initialize request
  @path        = request.path.split('/').slice(1, 100) || []
  @original    = @path.dup
  @request     = request
  @querystring = {}.to_hwia
  @ids         = []
  @shifted     = []

  set_variables
  set_domain request
  set_format
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



7
8
9
# File 'lib/lux/application/lib/nav.rb', line 7

def domain
  @domain
end

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/lux/application/lib/nav.rb', line 6

def format
  @format
end

#idsObject (readonly)

Returns the value of attribute ids.



7
8
9
# File 'lib/lux/application/lib/nav.rb', line 7

def ids
  @ids
end

#originalObject (readonly)

Returns the value of attribute original.



7
8
9
# File 'lib/lux/application/lib/nav.rb', line 7

def original
  @original
end

#querystringObject (readonly)

Returns the value of attribute querystring.



7
8
9
# File 'lib/lux/application/lib/nav.rb', line 7

def querystring
  @querystring
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



7
8
9
# File 'lib/lux/application/lib/nav.rb', line 7

def subdomain
  @subdomain
end

Instance Method Details

#baseObject



126
127
128
# File 'lib/lux/application/lib/nav.rb', line 126

def base
  @base ||= Lux.current.request.url.split('/').first(3).join('/')
end

#idObject



156
157
158
# File 'lib/lux/application/lib/nav.rb', line 156

def id
  @ids.last
end

#lastObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lux/application/lib/nav.rb', line 61

def last
#   if block_given?
#     # replace root in place if yields not nil
#     return unless @path.last.present?
#     result = yield(@path.last) || return
#     @path.pop
#     result
#   else
    @path.last
#   end
end

#localeObject

accept only two strings locale nav.locale { _1.length == 2 ? _1 : nil }



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lux/application/lib/nav.rb', line 136

def locale
  if @locale
    return @locale.to_s == '' ? nil : @locale
  end

  if @path[0].to_s.downcase =~ /^[a-z]{2}(-[a-z]{2})?$/
    if @locale = yield(@path[0])
      @path.shift
    else
      @locale = ''
    end
  end

  @locale
end

#locale=(name) ⇒ Object



152
153
154
# File 'lib/lux/application/lib/nav.rb', line 152

def locale= name
  @locale = name.present? ? name.to_s : nil
end

#path(*args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lux/application/lib/nav.rb', line 83

def path *args
  if args.first
    # contruct path
    # /upload_dialog/is_image:true/model:posts/id:2/field:image_url
    # = nav.path :model, :id, :field -> /upload_dialog/model:posts/id:2/field:image_url
    parts  = @original.select {|el| !el.include?(':') }
    parts += args.map do |el|
      if value = Lux.current.params[el]
        [el, value].join(':')
      end
    end.compact
    '/' + parts.join('/')
  elsif block_given?
    @path = @path.map { _1.gsub('-', '_') }
  else
    @path
  end
end

#path=(list) ⇒ Object



102
103
104
# File 'lib/lux/application/lib/nav.rb', line 102

def path= list
  @path = list
end

#path_idObject

replace nav path with id, when mached (works with resourceful routes map ‘controler’) nav.path_id { _1.split(‘-’).last.string_id rescue nil } /foo/test-cbjy/bar -> [‘foo’, :id, ‘bar]



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lux/application/lib/nav.rb', line 163

def path_id
  @path = @path.map do |el|
    if result = yield(el)
      @ids.push result
      :id
    else
      el
    end
  end

  @ids.last
end

#pop(replace_with = nil) ⇒ Object

pop element of the path



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lux/application/lib/nav.rb', line 49

def pop replace_with = nil
  if block_given?
    if result = yield(@path.last)
      @path.pop
      @path.unshift replace_with if replace_with
      result
    end
  else
    @path.shift
  end
end

#remove_wwwObject

removes leading www. www.foo.bar/path -> foo.bar/path



108
109
110
111
112
113
114
# File 'lib/lux/application/lib/nav.rb', line 108

def remove_www
  url = Lux.current.request.url

  if url.include?('://www.')
    Lux.current.response.redirect_to url.sub('://www.', '://')
  end
end

#rename_domain(from_domain, to_domain) ⇒ Object

nav.rename_domain ‘localhost’, ‘lvh.me’ localhost:3000/foo?bar=123 -> lvh.me:3000/foo?bar=123



118
119
120
121
122
123
# File 'lib/lux/application/lib/nav.rb', line 118

def rename_domain from_domain, to_domain
  if from_domain == @domain
    url = Url.new Lux.current.request.url
    Lux.current.response.redirect_to url.domain(to_domain).to_s
  end
end

#rootObject



23
24
25
# File 'lib/lux/application/lib/nav.rb', line 23

def root
  @path.first
end

#root=(value) ⇒ Object



27
28
29
# File 'lib/lux/application/lib/nav.rb', line 27

def root= value
  @path[0] = value
end

#shiftObject



31
32
33
34
35
# File 'lib/lux/application/lib/nav.rb', line 31

def shift
  @path.shift.tap do |value|
    @shifted.push value
  end
end

#to_sObject



130
131
132
# File 'lib/lux/application/lib/nav.rb', line 130

def to_s
  @path.join('/').sub(/\/$/, '')
end

#unshift(name = nil) ⇒ Object

used to make admin.lvm.me/users to lvh.me/admin/users



38
39
40
41
42
43
44
45
46
# File 'lib/lux/application/lib/nav.rb', line 38

def unshift name = nil
  if name
    @path.unshift name
    @path = @path.flatten
    name
  else
    @path.unshift @shifted.pop
  end
end

#url(*args) ⇒ Object

get Url object initialized with request.url - relative current.nav.url(:foo, 1).to_s # /path?foo=1



75
76
77
78
79
80
81
# File 'lib/lux/application/lib/nav.rb', line 75

def url *args
  if args.first
    Url.current.qs(*args)
  else
    Url.current
  end
end