lux-fw 0.1.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.version +1 -0
- data/bin/cli/am +250 -0
- data/bin/cli/assets +37 -0
- data/bin/cli/console +50 -0
- data/bin/cli/dev +1 -0
- data/bin/cli/eval +15 -0
- data/bin/cli/exceptions +62 -0
- data/bin/cli/generate +82 -0
- data/bin/cli/get +5 -0
- data/bin/cli/nginx +28 -0
- data/bin/cli/production +1 -0
- data/bin/cli/routes +12 -0
- data/bin/cli/server +1 -0
- data/bin/cli/stat +1 -0
- data/bin/forever +65 -0
- data/bin/job_que +39 -0
- data/bin/lux +87 -0
- data/bin/txt/nginx.conf +29 -0
- data/bin/txt/siege-and-puma.txt +3 -0
- data/lib/common/base32.rb +47 -0
- data/lib/common/before_and_after.rb +71 -0
- data/lib/common/class_attributes.rb +66 -0
- data/lib/common/class_method_params.rb +94 -0
- data/lib/common/crypt.rb +66 -0
- data/lib/common/folder_model.rb +50 -0
- data/lib/common/generic_model.rb +62 -0
- data/lib/common/policy.rb +54 -0
- data/lib/common/string_base.rb +49 -0
- data/lib/common/url.rb +171 -0
- data/lib/lux/api/api.rb +150 -0
- data/lib/lux/api/lib/application_api.rb +19 -0
- data/lib/lux/api/lib/doc_builder.rb +18 -0
- data/lib/lux/api/lib/dsl.rb +73 -0
- data/lib/lux/api/lib/model_api.rb +145 -0
- data/lib/lux/api/lib/rescue.rb +18 -0
- data/lib/lux/cache/cache.rb +71 -0
- data/lib/lux/cache/lib/memcached.rb +3 -0
- data/lib/lux/cache/lib/null.rb +23 -0
- data/lib/lux/cache/lib/ram.rb +38 -0
- data/lib/lux/cell/cell.rb +260 -0
- data/lib/lux/config/config.rb +88 -0
- data/lib/lux/controller/controller.rb +185 -0
- data/lib/lux/controller/lib/nav.rb +77 -0
- data/lib/lux/controller/lib/plugs.rb +10 -0
- data/lib/lux/delayed_job/delayed_job.rb +44 -0
- data/lib/lux/delayed_job/lib/memory.rb +14 -0
- data/lib/lux/delayed_job/lib/nsq.rb +3 -0
- data/lib/lux/delayed_job/lib/postgre.rb +6 -0
- data/lib/lux/delayed_job/lib/redis.rb +19 -0
- data/lib/lux/error/error.rb +75 -0
- data/lib/lux/helper/helper.rb +109 -0
- data/lib/lux/html/html.rb +3 -0
- data/lib/lux/html/lib/form.rb +81 -0
- data/lib/lux/html/lib/input.rb +71 -0
- data/lib/lux/html/lib/input_types.rb +277 -0
- data/lib/lux/lux.rb +164 -0
- data/lib/lux/mailer/mailer.rb +73 -0
- data/lib/lux/page/lib/encrypt_params.rb +44 -0
- data/lib/lux/page/lib/flash.rb +49 -0
- data/lib/lux/page/lib/static_file.rb +97 -0
- data/lib/lux/page/page.rb +271 -0
- data/lib/lux/rescue_from/rescue_from.rb +61 -0
- data/lib/lux/template/template.rb +95 -0
- data/lib/lux-fw.rb +48 -0
- data/lib/overload/array.rb +52 -0
- data/lib/overload/blank.rb +62 -0
- data/lib/overload/date.rb +58 -0
- data/lib/overload/file.rb +14 -0
- data/lib/overload/hash.rb +86 -0
- data/lib/overload/hash_wia.rb +282 -0
- data/lib/overload/inflections.rb +199 -0
- data/lib/overload/integer.rb +19 -0
- data/lib/overload/module.rb +10 -0
- data/lib/overload/nil.rb +8 -0
- data/lib/overload/object.rb +77 -0
- data/lib/overload/string.rb +89 -0
- data/lib/overload/string_inflections.rb +7 -0
- data/lib/overload/struct.rb +5 -0
- data/lib/plugins/assets/assets_plug.rb +26 -0
- data/lib/plugins/assets/helper_module_adapter.rb +49 -0
- data/lib/plugins/assets/init.rb +4 -0
- data/lib/plugins/db_helpers/array_and_hstore.rb +64 -0
- data/lib/plugins/db_helpers/arrays_and_tags.rb +23 -0
- data/lib/plugins/db_helpers/before_save.rb +44 -0
- data/lib/plugins/db_helpers/cached_find_by.rb +45 -0
- data/lib/plugins/db_helpers/class_and_instance.rb +120 -0
- data/lib/plugins/db_helpers/dataset_plugin.rb +101 -0
- data/lib/plugins/db_helpers/filter_wrappers.rb +21 -0
- data/lib/plugins/db_helpers/link_plugin.rb +95 -0
- data/lib/plugins/db_helpers/localize_plugin.rb +57 -0
- data/lib/plugins/db_helpers/primary_keys.rb +36 -0
- data/lib/plugins/db_helpers/typero_attributes.rb +69 -0
- data/lib/plugins/db_logger/init.rb +18 -0
- data/lib/plugins/db_logger/lux_response_adapter.rb +9 -0
- data/lib/plugins/paginate/helper.rb +32 -0
- data/lib/plugins/paginate/sequel_adapter.rb +18 -0
- data/lib/vendor/mini_assets/mini_asset/base.rb +167 -0
- data/lib/vendor/mini_assets/mini_asset/css.rb +38 -0
- data/lib/vendor/mini_assets/mini_asset/js.rb +38 -0
- data/lib/vendor/mini_assets/mini_asset.rb +31 -0
- data/lib/vendor/oauth/lib/facebook.rb +35 -0
- data/lib/vendor/oauth/lib/github.rb +37 -0
- data/lib/vendor/oauth/lib/google.rb +41 -0
- data/lib/vendor/oauth/lib/linkedin.rb +41 -0
- data/lib/vendor/oauth/lib/stackexchange.rb +37 -0
- data/lib/vendor/oauth/lib/twitter.rb +41 -0
- data/lib/vendor/oauth/oauth.rb +46 -0
- metadata +334 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
module Lux::Cache::RamCache
|
2
|
+
extend self
|
3
|
+
|
4
|
+
@@lock = Mutex.new
|
5
|
+
@@ram_cache = {}
|
6
|
+
@@ttl_cache = {}
|
7
|
+
|
8
|
+
def set(key, data, ttl=nil)
|
9
|
+
@@lock.synchronize do
|
10
|
+
@@ttl_cache[key] = Time.now.to_i + ttl if ttl
|
11
|
+
@@ram_cache[key] = data
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(key)
|
16
|
+
if ttl_check = @@ttl_cache[key]
|
17
|
+
return nil if ttl_check < Time.now.to_i
|
18
|
+
end
|
19
|
+
|
20
|
+
@@ram_cache[key]
|
21
|
+
end
|
22
|
+
|
23
|
+
def fetch(key, ttl=nil)
|
24
|
+
data = get key
|
25
|
+
return data if data
|
26
|
+
set(key, yield, ttl)
|
27
|
+
end
|
28
|
+
|
29
|
+
def delete(key)
|
30
|
+
@@lock.synchronize do
|
31
|
+
@@ram_cache.delete(key)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_multi(*args)
|
36
|
+
@@ram_cache.select{ |k,v| args.index(k) }
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,260 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Cells can be called in few ways
|
4
|
+
# Cell.call path
|
5
|
+
# Cell.action action_name, path
|
6
|
+
# Cell.new.action_name *args
|
7
|
+
|
8
|
+
class Lux::Cell
|
9
|
+
# define maser layout
|
10
|
+
# string is template, symbol is metod pointer and lambda is lambda
|
11
|
+
ClassAttributes.define self, :layout
|
12
|
+
|
13
|
+
# define helper contest, by defult derived from class name
|
14
|
+
ClassAttributes.define self, :helper
|
15
|
+
|
16
|
+
# before and after any action filter, ignored in cells, after is called just before render
|
17
|
+
BeforeAndAfter.define self, :before, :before_action, :before_render, :after
|
18
|
+
|
19
|
+
class << self
|
20
|
+
# class call method, should not be overridden
|
21
|
+
def call
|
22
|
+
Lux.page.files_in_use.push "app/cells/#{self.to_s.underscore}.rb"
|
23
|
+
|
24
|
+
cell = new
|
25
|
+
cell.filter :before
|
26
|
+
cell.call
|
27
|
+
|
28
|
+
return if Lux.page.body
|
29
|
+
|
30
|
+
# we want to exec filter after the call
|
31
|
+
cell.filter :before_action
|
32
|
+
end
|
33
|
+
|
34
|
+
# create mock function, to enable template rendering
|
35
|
+
# mock :index, :login
|
36
|
+
def mock *args
|
37
|
+
for el in args
|
38
|
+
# class_eval %[def #{el}(*args); true; end]
|
39
|
+
define_method el do
|
40
|
+
true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# simple shortcut allows direct call to action, bypasing call
|
46
|
+
def action name, object=nil
|
47
|
+
object.present? ? new.action(name, object) : new.action(name)
|
48
|
+
end
|
49
|
+
|
50
|
+
# for use in helpers, mostly
|
51
|
+
# renders only cell without layout
|
52
|
+
# = cell :tag_list, object
|
53
|
+
def cell name, *args
|
54
|
+
c = new
|
55
|
+
c.instance_variable_set '@cell_action', name
|
56
|
+
c.instance_variable_set '@no_render', true
|
57
|
+
c.send name, *args
|
58
|
+
c.render_part
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
### INSTANCE METHODS
|
63
|
+
|
64
|
+
def initialize
|
65
|
+
# before and after should be exected only once
|
66
|
+
@executed_filters = {}
|
67
|
+
@base_template = self.class.to_s.index('::') ? self.class.to_s.sub(/Cell$/,'').underscore : self.class.to_s.sub(/Cell$/,'').downcase
|
68
|
+
end
|
69
|
+
|
70
|
+
# default call method, should be overitten
|
71
|
+
# expects arguments as flat array
|
72
|
+
# usually called by router
|
73
|
+
def call
|
74
|
+
action(:index)
|
75
|
+
end
|
76
|
+
|
77
|
+
def filter fiter_name
|
78
|
+
return if @executed_filters[fiter_name]
|
79
|
+
@executed_filters[fiter_name] = true
|
80
|
+
|
81
|
+
BeforeAndAfter.execute(self, fiter_name)
|
82
|
+
end
|
83
|
+
|
84
|
+
# action(:show, 2)
|
85
|
+
# action(:select', ['users'])
|
86
|
+
def action method_name, *args
|
87
|
+
raise ArgumentError.new('Cell action called with blank action name argument') if method_name.blank?
|
88
|
+
|
89
|
+
# maybe before filter rendered page
|
90
|
+
return if Lux.page.body
|
91
|
+
|
92
|
+
method_name = method_name.to_s.gsub('-', '_').gsub(/[^\w]/, '')
|
93
|
+
|
94
|
+
Lux.log " #{self.class.to_s}(:#{method_name})".blue
|
95
|
+
Lux.page.files_in_use.push "app/cells/#{self.class.to_s.underscore}.rb"
|
96
|
+
|
97
|
+
@cell_action = method_name
|
98
|
+
|
99
|
+
unless respond_to? method_name
|
100
|
+
raise NotFoundError.new('Method %s not found' % method_name) unless Lux.verbose?
|
101
|
+
|
102
|
+
list = methods - Lux::Cell.instance_methods
|
103
|
+
err = [%[No instance method "#{method_name}" found in class "#{self.class.to_s}"]]
|
104
|
+
err.push ["Expected so see def show(id) ..."] if method_name == 'show!'
|
105
|
+
err.push %[You have defined \n- #{(list).join("\n- ")}]
|
106
|
+
return Lux.error(err.join("\n\n"))
|
107
|
+
end
|
108
|
+
|
109
|
+
filter :before
|
110
|
+
return if Lux.page.body
|
111
|
+
|
112
|
+
filter :before_action
|
113
|
+
return if Lux.page.body
|
114
|
+
|
115
|
+
send method_name, *args
|
116
|
+
filter :after
|
117
|
+
|
118
|
+
return if Lux.page.body
|
119
|
+
render
|
120
|
+
end
|
121
|
+
|
122
|
+
# render :show, id
|
123
|
+
# render :index
|
124
|
+
# render 'main/root/index'
|
125
|
+
# render :profile, name:'Dux'
|
126
|
+
# render text: 'ok'
|
127
|
+
def render name=nil, opts={}
|
128
|
+
return if Lux.page.body
|
129
|
+
return if @no_render
|
130
|
+
|
131
|
+
filter :before_render
|
132
|
+
|
133
|
+
if name.is_hash?
|
134
|
+
opts = name
|
135
|
+
name = nil
|
136
|
+
end
|
137
|
+
|
138
|
+
opts[:template] = name if name
|
139
|
+
|
140
|
+
# etag and cache string are same things, one is client based, other one is server based
|
141
|
+
if opts[:cache]
|
142
|
+
opts[:cache] = Lux.cache.generate_key(opts[:cache] )
|
143
|
+
opts[:etag] ||= opts[:cache]
|
144
|
+
end
|
145
|
+
|
146
|
+
return if opts[:etag] && Lux.page.etag(opts[:etag])
|
147
|
+
|
148
|
+
if opts[:cache]
|
149
|
+
data = Lux.cache.get(opts[:cache])
|
150
|
+
return Lux.page.body(data) if data
|
151
|
+
end
|
152
|
+
|
153
|
+
render_resolve_body(opts)
|
154
|
+
Lux.cache.set(opts[:cache], Lux.page.body) if opts[:cache]
|
155
|
+
end
|
156
|
+
|
157
|
+
# renders template to string
|
158
|
+
def render_part
|
159
|
+
Lux::Template.render_part("#{@base_template}/#{@cell_action}", instance_variables_hash, namespace)
|
160
|
+
end
|
161
|
+
|
162
|
+
def render_to_string name=nil, opts={}
|
163
|
+
opts[:set_page_body] = false
|
164
|
+
render name, opts
|
165
|
+
end
|
166
|
+
|
167
|
+
def send_file
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
private
|
172
|
+
# called be render
|
173
|
+
def render_resolve_body opts
|
174
|
+
# resolve basic types
|
175
|
+
if opts[:text]
|
176
|
+
Lux.page.content_type = 'text/plain'
|
177
|
+
return Lux.page.body(opts[:text])
|
178
|
+
elsif opts[:html]
|
179
|
+
Lux.page.content_type = 'text/html'
|
180
|
+
return Lux.page.body(opts[:html])
|
181
|
+
elsif opts[:json]
|
182
|
+
Lux.page.content_type = 'application/json'
|
183
|
+
return Lux.page.body(opts[:json])
|
184
|
+
end
|
185
|
+
|
186
|
+
ivh = instance_variables_hash
|
187
|
+
|
188
|
+
# resolve page data, without template
|
189
|
+
page_data = opts[:data] || Proc.new do
|
190
|
+
if template = opts.delete(:template)
|
191
|
+
template = template.to_s
|
192
|
+
template = "#{@base_template}/#{template}" unless template.starts_with?('/')
|
193
|
+
else
|
194
|
+
template = "#{@base_template}/#{@cell_action}"
|
195
|
+
end
|
196
|
+
|
197
|
+
Lux::Template.render_part(template, ivh, self.class.helper)
|
198
|
+
end.call
|
199
|
+
|
200
|
+
# resolve data with layout
|
201
|
+
layout = opts.delete(:layout)
|
202
|
+
layout = nil if layout.class == TrueClass
|
203
|
+
layout = false if @layout.class == FalseClass
|
204
|
+
if layout.class != FalseClass
|
205
|
+
layout_define = self.class.layout
|
206
|
+
layout = case layout_define
|
207
|
+
when String
|
208
|
+
layout = layout_define
|
209
|
+
when Symbol
|
210
|
+
send(layout_define)
|
211
|
+
when Proc
|
212
|
+
layout_define.call
|
213
|
+
else
|
214
|
+
"#{@base_template.split('/')[0]}/layout"
|
215
|
+
end
|
216
|
+
|
217
|
+
page_data = Lux::Template.new(layout, instance_variables_hash, self.class.helper).render_part do
|
218
|
+
page_data
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
Lux.page.body(page_data) unless opts[:set_page_body].is_false?
|
223
|
+
page_data
|
224
|
+
end
|
225
|
+
|
226
|
+
# helper functions
|
227
|
+
def page
|
228
|
+
Lux.page
|
229
|
+
end
|
230
|
+
|
231
|
+
def params
|
232
|
+
Lux.page.params
|
233
|
+
end
|
234
|
+
|
235
|
+
def nav
|
236
|
+
Lux.page.nav
|
237
|
+
end
|
238
|
+
|
239
|
+
def request
|
240
|
+
Lux.page.request
|
241
|
+
end
|
242
|
+
|
243
|
+
def redirect where, flash={}
|
244
|
+
Lux.page.redirect where, flash
|
245
|
+
end
|
246
|
+
|
247
|
+
def session
|
248
|
+
Lux.page.session
|
249
|
+
end
|
250
|
+
|
251
|
+
def namespace
|
252
|
+
@base_template.split('/')[0].to_sym
|
253
|
+
end
|
254
|
+
|
255
|
+
def helper(ns=nil)
|
256
|
+
Lux::Helper.for(ns || namespace)
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') }
|
4
|
+
|
5
|
+
module Lux::Config
|
6
|
+
extend self
|
7
|
+
|
8
|
+
LIVE_RELOAD ||= {}
|
9
|
+
|
10
|
+
# if we have errors in module loading, try to load them one more time
|
11
|
+
@module_error = []
|
12
|
+
|
13
|
+
# requires all files recrusive in, with spart sort
|
14
|
+
def require_all(files)
|
15
|
+
files = files.to_s
|
16
|
+
files += '/*' unless files.include?('*')
|
17
|
+
|
18
|
+
file_errors = []
|
19
|
+
glob = `echo #{files} #{files}/* #{files}/*/* #{files}/*/*/* #{files}/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n")
|
20
|
+
glob.reject! { |file| file =~ /_\d+\.rb$/ }
|
21
|
+
|
22
|
+
glob.select{ |o| o.index('.rb') }.map{ |o| o.split('.rb')[0]}.each do |ruby_file|
|
23
|
+
require ruby_file rescue file_errors.push(ruby_file)
|
24
|
+
end
|
25
|
+
|
26
|
+
file_errors.each { |klass|
|
27
|
+
@klass = klass
|
28
|
+
require klass
|
29
|
+
}
|
30
|
+
rescue
|
31
|
+
puts "* double load error: require_all #{files}\n #{@klass}.rb\n #{$!.message}".red
|
32
|
+
raise $!
|
33
|
+
end
|
34
|
+
|
35
|
+
# load specific plugin
|
36
|
+
def plugin(name)
|
37
|
+
files = Dir['%s/lib/plugins/%s/*.rb' % [Lux.fw_root, name]]
|
38
|
+
die('Plugin "%s" load error, no plugin' % name) if files.length == 0
|
39
|
+
for file in files
|
40
|
+
require file
|
41
|
+
end
|
42
|
+
# Lux.log "* plugin :#{name}"
|
43
|
+
end
|
44
|
+
|
45
|
+
# preview config in development
|
46
|
+
def show_config
|
47
|
+
for k,v in Lux.config
|
48
|
+
next if v.kind_of?(Hash)
|
49
|
+
puts "* config :#{k} = #{v.kind_of?(Hash) ? '{...}' : v}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# check all files and reload if needed
|
54
|
+
def live_require_check!
|
55
|
+
if LIVE_RELOAD.blank?
|
56
|
+
root = Lux.root.to_s
|
57
|
+
for file in $LOADED_FEATURES.select{ |f| f.index(root) || f.index('/lux/') }
|
58
|
+
LIVE_RELOAD[file] = File.mtime(file).to_i
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
for file, mtime in LIVE_RELOAD
|
63
|
+
new_file_mtime = File.mtime(file).to_i
|
64
|
+
|
65
|
+
next if mtime == new_file_mtime
|
66
|
+
LIVE_RELOAD[file] = new_file_mtime
|
67
|
+
Lux.log " Reloaded: .#{file.split(Lux.root.to_s).last.red}"
|
68
|
+
load file
|
69
|
+
end
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
def show_load_speed(load_start)
|
74
|
+
speed = ((Time.now - load_start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2')+'ms'
|
75
|
+
ram = `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000
|
76
|
+
opts = []
|
77
|
+
opts.push Lux.verbose? ? 'verbose'.yellow : 'non-verbose'.green
|
78
|
+
opts.push Lux.config(:auto_code_reload) ? 'code-reload'.yellow : 'no-code-reload'.green
|
79
|
+
|
80
|
+
"* #{'Lux'.blue} loaded #{Lux.env('RACK_ENV').green} (#{opts.join(', ')}) mode in #{speed.to_s.white}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Object
|
85
|
+
def reload!
|
86
|
+
Lux::Config.live_require_check!
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
class Lux::Controller
|
2
|
+
|
3
|
+
LUX_PRINT_ROUTES = !!ENV['LUX_PRINT_ROUTES'] unless defined?(LUX_PRINT_ROUTES)
|
4
|
+
|
5
|
+
# define common http methods as constants
|
6
|
+
[:get, :head, :post, :delete, :put, :patch].map(&:to_s).map(&:upcase).each { |m| eval "#{m} ||= '#{m}'" }
|
7
|
+
|
8
|
+
# rescue from errors
|
9
|
+
Lux::RescueFrom.define(self)
|
10
|
+
|
11
|
+
# this is needed for Rescuce actions to work without Lux prefix
|
12
|
+
define_singleton_method(:page) { Lux.page }
|
13
|
+
|
14
|
+
###
|
15
|
+
|
16
|
+
def debug
|
17
|
+
{ :@locale=>@locale, :@nav=>nav, :@subdomain=>@subdomain, :@domain=>@domain }
|
18
|
+
end
|
19
|
+
|
20
|
+
def body?
|
21
|
+
Lux.page.body ? true : false
|
22
|
+
end
|
23
|
+
|
24
|
+
def body data
|
25
|
+
Lux.page.body(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def page
|
29
|
+
Lux.page
|
30
|
+
end
|
31
|
+
|
32
|
+
def nav
|
33
|
+
Lux.page.nav
|
34
|
+
end
|
35
|
+
|
36
|
+
def params
|
37
|
+
Lux.page.params
|
38
|
+
end
|
39
|
+
|
40
|
+
def redirect where, msgs={}
|
41
|
+
Lux.page.redirect where, msgs
|
42
|
+
end
|
43
|
+
|
44
|
+
def request
|
45
|
+
Lux.page.request
|
46
|
+
end
|
47
|
+
|
48
|
+
# gets only root
|
49
|
+
def root opts
|
50
|
+
if LUX_PRINT_ROUTES
|
51
|
+
@route_target = opts
|
52
|
+
print_route
|
53
|
+
end
|
54
|
+
|
55
|
+
call opts unless nav.root
|
56
|
+
end
|
57
|
+
|
58
|
+
### plug and default prepares
|
59
|
+
|
60
|
+
def plug(name, &block)
|
61
|
+
return if body?
|
62
|
+
m = "#{name}_plug".to_sym
|
63
|
+
return Lux.error(%[Method "#{m}" not defined in Lux::Controller]) unless respond_to?(m)
|
64
|
+
send(m, &block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def rescued_main
|
68
|
+
if respond_to?(:main)
|
69
|
+
@@rescue_from_ivar.call { main }
|
70
|
+
else
|
71
|
+
Lux.error %[No instance method "main" in Lux::Controller defiend]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def print_route
|
76
|
+
route = case @route
|
77
|
+
when Regexp
|
78
|
+
@route.to_s.ljust(20).red
|
79
|
+
when Symbol
|
80
|
+
('/%s' % @route).ljust(20).green
|
81
|
+
when nil
|
82
|
+
'/'.ljust(20).green
|
83
|
+
else
|
84
|
+
@route.to_s.ljust(20).blue
|
85
|
+
end
|
86
|
+
|
87
|
+
route_target = case @route_target
|
88
|
+
when Symbol
|
89
|
+
':%s' % @route_target
|
90
|
+
when Array
|
91
|
+
action = @route_target[1] == 'print-routes' ? @route : @route_target[1]
|
92
|
+
'%s.action("%s")' % [@route_target[0], action]
|
93
|
+
else
|
94
|
+
@route_target
|
95
|
+
end
|
96
|
+
|
97
|
+
puts '%s => %s (%s)' % [route, route_target.to_s.ljust(40), @route.class]
|
98
|
+
end
|
99
|
+
|
100
|
+
def route route_object
|
101
|
+
return if body?
|
102
|
+
|
103
|
+
if route_object.class == Hash
|
104
|
+
@route = route_object.keys.first
|
105
|
+
@route_target = route_object.values.first
|
106
|
+
else
|
107
|
+
@route = route_object
|
108
|
+
end
|
109
|
+
|
110
|
+
print_route if LUX_PRINT_ROUTES
|
111
|
+
|
112
|
+
return unless if @route.is_a?(String)
|
113
|
+
page.nav.to_s.start_with?(@route)
|
114
|
+
elsif @route.is_a?(Regexp)
|
115
|
+
page.nav.to_s =~ @route
|
116
|
+
else
|
117
|
+
nav.root == @route.to_s
|
118
|
+
end
|
119
|
+
|
120
|
+
@route_object = route_object
|
121
|
+
|
122
|
+
case @route_target
|
123
|
+
when Proc
|
124
|
+
@route_target.call
|
125
|
+
when Array
|
126
|
+
call @route_target[0], @route_target[1]
|
127
|
+
else
|
128
|
+
call @route_target
|
129
|
+
end
|
130
|
+
end
|
131
|
+
alias :match :route
|
132
|
+
|
133
|
+
def get object
|
134
|
+
return unless get?
|
135
|
+
route object
|
136
|
+
end
|
137
|
+
|
138
|
+
def post object
|
139
|
+
return unless post?
|
140
|
+
route object
|
141
|
+
end
|
142
|
+
|
143
|
+
def get?
|
144
|
+
Lux.page.request.request_method == 'GET'
|
145
|
+
end
|
146
|
+
|
147
|
+
def post?
|
148
|
+
Lux.page.request.request_method == 'POST'
|
149
|
+
end
|
150
|
+
|
151
|
+
# call :api_router
|
152
|
+
# call Main::UsersCell
|
153
|
+
# call Main::UsersCell, :index
|
154
|
+
# call 'main/orgs#show'
|
155
|
+
def call object=nil, action=nil
|
156
|
+
return true if body?
|
157
|
+
|
158
|
+
case object
|
159
|
+
when Symbol
|
160
|
+
return send(object)
|
161
|
+
when String
|
162
|
+
if object.include?('#')
|
163
|
+
object = object.split('#')
|
164
|
+
object[0] = ('%s_cell' % object[0]).classify.constantize
|
165
|
+
else
|
166
|
+
object = ('%s_cell' % object).classify.constantize
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
object, action = object if object.is_a? Array
|
171
|
+
|
172
|
+
Lux.log ' %s %s # %s' % [object.to_s.blue, nav.path, @route_object]
|
173
|
+
|
174
|
+
if action
|
175
|
+
object.action action
|
176
|
+
else
|
177
|
+
object.call
|
178
|
+
end
|
179
|
+
|
180
|
+
Lux.error 'Lux cell "%s" called via route "%s" but page body is not set' % [object, @route] unless body?
|
181
|
+
|
182
|
+
true
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# experiment for different nav in rooter
|
2
|
+
|
3
|
+
class Lux::Controller::Nav
|
4
|
+
|
5
|
+
attr_reader :root, :path, :id, :full, :locale, :subdomain, :domain
|
6
|
+
|
7
|
+
# acepts path as a string
|
8
|
+
def initialize request
|
9
|
+
@path = request.path.split('/').slice(1, 100) || []
|
10
|
+
@root = @path.shift if @path.first
|
11
|
+
|
12
|
+
@subdomain = request.host.split('.')
|
13
|
+
@domain = @subdomain.pop(2).join('.')
|
14
|
+
@domain += ".#{@subdomain.pop}" if @domain.length < 6
|
15
|
+
|
16
|
+
build_full
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_full
|
20
|
+
@full = '/%s/%s' % [@root, @path.join('/')]
|
21
|
+
end
|
22
|
+
|
23
|
+
def shift_to_root
|
24
|
+
@root.tap do
|
25
|
+
@root = @path.shift
|
26
|
+
build_full
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# used to make admin.lvm.me/users to lvh.me/admin/users
|
31
|
+
def unshift name
|
32
|
+
@path.unshift @root
|
33
|
+
@root = name
|
34
|
+
build_full
|
35
|
+
end
|
36
|
+
|
37
|
+
def parse_locale
|
38
|
+
return false unless Locale.all.include?(@root)
|
39
|
+
Locale.current = shift_to_root
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def shift
|
44
|
+
@path.shift
|
45
|
+
end
|
46
|
+
|
47
|
+
def first
|
48
|
+
@path.first
|
49
|
+
end
|
50
|
+
|
51
|
+
def second
|
52
|
+
@path[1]
|
53
|
+
end
|
54
|
+
|
55
|
+
def last
|
56
|
+
@path.last
|
57
|
+
end
|
58
|
+
|
59
|
+
def rest
|
60
|
+
@path.slice(1, @path.length-1)
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
@full
|
65
|
+
end
|
66
|
+
|
67
|
+
# if sets @id to whatever function returns
|
68
|
+
# page.nav.id! { |id| StringBase.extract id }
|
69
|
+
def id! &block
|
70
|
+
@block = block if block
|
71
|
+
|
72
|
+
return unless first
|
73
|
+
@id = @block.call first
|
74
|
+
shift if @id
|
75
|
+
@id
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Lux::DelayedJob
|
2
|
+
extend self
|
3
|
+
|
4
|
+
@@server = nil
|
5
|
+
|
6
|
+
def server=(name)
|
7
|
+
adapter = "Lux::DelayedJob::#{name.to_s.capitalize}"
|
8
|
+
@@server = adapter.constantize
|
9
|
+
rescue NameError
|
10
|
+
die('No adapter %s not found' % adapter)
|
11
|
+
end
|
12
|
+
|
13
|
+
def push(object, method_to_call=nil)
|
14
|
+
die "No DelayedJob server defined" unless @@server
|
15
|
+
@@server.push [object, method_to_call]
|
16
|
+
end
|
17
|
+
|
18
|
+
def pop
|
19
|
+
obj, method_to_call = @@server.pop
|
20
|
+
return unless obj
|
21
|
+
puts "JOB POP> #{obj.to_s}.#{method_to_call}".yellow
|
22
|
+
if method_to_call
|
23
|
+
begin
|
24
|
+
obj.send(method_to_call)
|
25
|
+
rescue
|
26
|
+
puts("Lux::DelayedJob.pop FAIL for :#{method_to_call} (#{$!.message})".red)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
eval(obj)
|
30
|
+
end
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def run!(seconds=2)
|
35
|
+
puts "JOB QUE> is running for #{@@server}".green
|
36
|
+
Thread.new do
|
37
|
+
while true
|
38
|
+
print '.'
|
39
|
+
true while Lux::DelayedJob.pop
|
40
|
+
sleep seconds
|
41
|
+
end
|
42
|
+
end.join
|
43
|
+
end
|
44
|
+
end
|