Class: Lux::Application::Nav
Instance Attribute Summary collapse
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#format ⇒ Object
Returns the value of attribute format.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#original ⇒ Object
readonly
Returns the value of attribute original.
-
#querystring ⇒ Object
readonly
Returns the value of attribute querystring.
-
#subdomain ⇒ Object
readonly
Returns the value of attribute subdomain.
Instance Method Summary collapse
- #base ⇒ Object
- #id ⇒ Object
-
#initialize(request) ⇒ Nav
constructor
acepts path as a string.
- #last ⇒ Object
-
#locale ⇒ Object
accept only two strings locale nav.locale { _1.length == 2 ? _1 : nil }.
- #locale=(name) ⇒ Object
- #path(*args) ⇒ Object
- #path=(list) ⇒ Object
-
#path_id ⇒ Object
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].
-
#pop(replace_with = nil) ⇒ Object
pop element of the path.
-
#remove_www ⇒ Object
removes leading www.
-
#rename_domain(from_domain, to_domain) ⇒ Object
nav.rename_domain ‘localhost’, ‘lvh.me’ localhost:3000/foo?bar=123 -> lvh.me:3000/foo?bar=123.
- #root ⇒ Object
- #root=(value) ⇒ Object
- #shift ⇒ Object
- #to_s ⇒ Object
-
#unshift(name = nil) ⇒ Object
used to make admin.lvm.me/users to lvh.me/admin/users.
-
#url(*args) ⇒ Object
get Url object initialized with request.url - relative current.nav.url(:foo, 1).to_s # /path?foo=1.
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
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
7 8 9 |
# File 'lib/lux/application/lib/nav.rb', line 7 def domain @domain end |
#format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/lux/application/lib/nav.rb', line 6 def format @format end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
7 8 9 |
# File 'lib/lux/application/lib/nav.rb', line 7 def ids @ids end |
#original ⇒ Object (readonly)
Returns the value of attribute original.
7 8 9 |
# File 'lib/lux/application/lib/nav.rb', line 7 def original @original end |
#querystring ⇒ Object (readonly)
Returns the value of attribute querystring.
7 8 9 |
# File 'lib/lux/application/lib/nav.rb', line 7 def querystring @querystring end |
#subdomain ⇒ Object (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
#base ⇒ Object
126 127 128 |
# File 'lib/lux/application/lib/nav.rb', line 126 def base @base ||= Lux.current.request.url.split('/').first(3).join('/') end |
#id ⇒ Object
156 157 158 |
# File 'lib/lux/application/lib/nav.rb', line 156 def id @ids.last end |
#last ⇒ Object
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 |
#locale ⇒ Object
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_id ⇒ Object
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_www ⇒ Object
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 |
#root ⇒ Object
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 |
#shift ⇒ Object
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_s ⇒ Object
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 |