lux-fw 0.1.35 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.version +1 -1
- data/bin/cli/am +67 -41
- data/bin/cli/assets +1 -1
- data/bin/cli/console +3 -2
- data/bin/cli/eval +11 -2
- data/bin/cli/exceptions +1 -1
- data/bin/cli/generate +4 -0
- data/bin/cli/render +18 -0
- data/bin/cli/routes +3 -1
- data/bin/cli/server +4 -1
- data/bin/lux +10 -1
- data/bin/txt/nginx.conf +35 -18
- data/lib/common/class_attributes.rb +10 -10
- data/lib/common/{before_and_after.rb → class_callbacks.rb} +12 -21
- data/lib/common/crypt.rb +10 -6
- data/lib/common/dynamic_class.rb +12 -7
- data/lib/common/generic_model.rb +6 -6
- data/lib/common/hash_with_indifferent_access.rb +6 -284
- data/lib/{lux/helper/lib/html_tag.rb → common/html_tag_builder.rb} +1 -1
- data/lib/common/policy.rb +2 -2
- data/lib/common/url.rb +6 -4
- data/lib/lux-fw.rb +2 -13
- data/lib/lux/api/api.rb +34 -47
- data/lib/lux/api/lib/dsl.rb +2 -2
- data/lib/lux/api/lib/model_api.rb +40 -23
- data/lib/lux/api/lib/response.rb +30 -5
- data/lib/lux/application/application.rb +182 -0
- data/lib/lux/{controller → application}/lib/nav.rb +18 -21
- data/lib/lux/application/lib/plugs.rb +10 -0
- data/lib/lux/application/lib/render.rb +58 -0
- data/lib/lux/application/lib/route_test.rb +64 -0
- data/lib/lux/cache/cache.rb +27 -16
- data/lib/lux/cell/cell.rb +66 -54
- data/lib/lux/config/config.rb +51 -27
- data/lib/lux/current/current.rb +122 -0
- data/lib/lux/{page → current}/lib/encrypt_params.rb +2 -2
- data/lib/lux/{page → current}/lib/static_file.rb +29 -25
- data/lib/lux/delayed_job/delayed_job.rb +4 -4
- data/lib/lux/error/error.rb +31 -16
- data/lib/lux/helper/helper.rb +32 -37
- data/lib/lux/helper/helpers/application_helper.rb +3 -0
- data/lib/lux/helper/helpers/html_helper.rb +3 -0
- data/lib/lux/helper/helpers/mailer_helper.rb +11 -0
- data/lib/lux/lux.rb +40 -30
- data/lib/lux/mailer/mailer.rb +46 -29
- data/lib/lux/{page → response}/lib/flash.rb +1 -1
- data/lib/lux/response/lib/header.rb +21 -0
- data/lib/lux/response/response.rb +237 -0
- data/lib/lux/template/template.rb +20 -18
- data/lib/overload/array.rb +5 -0
- data/lib/overload/auto_loader.rb +27 -0
- data/lib/overload/blank.rb +8 -2
- data/lib/overload/float.rb +10 -0
- data/lib/overload/hash.rb +5 -12
- data/lib/overload/integer.rb +1 -5
- data/lib/overload/nil.rb +5 -0
- data/lib/overload/object.rb +1 -0
- data/lib/overload/r.rb +5 -0
- data/lib/overload/string.rb +4 -5
- data/lib/overload/{date.rb → time.rb} +23 -3
- metadata +37 -73
- data/bin/forever +0 -65
- data/bin/job_que +0 -39
- data/lib/lux/api/lib/application_api.rb +0 -38
- data/lib/lux/api/lib/doc_builder.rb +0 -19
- data/lib/lux/api/lib/rescue.rb +0 -8
- data/lib/lux/controller/controller.rb +0 -185
- data/lib/lux/controller/lib/plugs.rb +0 -10
- data/lib/lux/html/html.rb +0 -3
- data/lib/lux/html/lib/form.rb +0 -81
- data/lib/lux/html/lib/input.rb +0 -71
- data/lib/lux/html/lib/input_types.rb +0 -287
- data/lib/lux/lib/lux.rb +0 -51
- data/lib/lux/page/lib/response.rb +0 -178
- data/lib/lux/page/page.rb +0 -292
- data/lib/lux/rescue_from/rescue_from.rb +0 -63
- data/lib/plugins/assets/assets_plug.rb +0 -31
- data/lib/plugins/assets/helper_module_adapter.rb +0 -51
- data/lib/plugins/assets/init.rb +0 -4
- data/lib/plugins/db_helpers/array_and_hstore.rb +0 -64
- data/lib/plugins/db_helpers/arrays_and_tags.rb +0 -23
- data/lib/plugins/db_helpers/before_save.rb +0 -44
- data/lib/plugins/db_helpers/cached_find_by.rb +0 -45
- data/lib/plugins/db_helpers/class_and_instance.rb +0 -120
- data/lib/plugins/db_helpers/dataset_plugin.rb +0 -101
- data/lib/plugins/db_helpers/filter_wrappers.rb +0 -21
- data/lib/plugins/db_helpers/link_plugin.rb +0 -95
- data/lib/plugins/db_helpers/localize_plugin.rb +0 -57
- data/lib/plugins/db_helpers/primary_keys.rb +0 -36
- data/lib/plugins/db_helpers/typero_attributes.rb +0 -69
- data/lib/plugins/db_logger/init.rb +0 -18
- data/lib/plugins/db_logger/lux_response_adapter.rb +0 -9
- data/lib/plugins/paginate/helper.rb +0 -32
- data/lib/plugins/paginate/sequel_adapter.rb +0 -18
- data/lib/vendor/mini_assets/lib/asset.rb +0 -71
- data/lib/vendor/mini_assets/lib/asset/css.rb +0 -19
- data/lib/vendor/mini_assets/lib/asset/js.rb +0 -17
- data/lib/vendor/mini_assets/lib/base.rb +0 -69
- data/lib/vendor/mini_assets/lib/base/javascript.rb +0 -13
- data/lib/vendor/mini_assets/lib/base/stylesheet.rb +0 -5
- data/lib/vendor/mini_assets/lib/manifest.rb +0 -18
- data/lib/vendor/mini_assets/lib/opts.rb +0 -16
- data/lib/vendor/mini_assets/mini_assets.rb +0 -74
- data/lib/vendor/oauth/lib/facebook.rb +0 -35
- data/lib/vendor/oauth/lib/github.rb +0 -37
- data/lib/vendor/oauth/lib/google.rb +0 -41
- data/lib/vendor/oauth/lib/linkedin.rb +0 -41
- data/lib/vendor/oauth/lib/stackexchange.rb +0 -37
- data/lib/vendor/oauth/lib/twitter.rb +0 -41
- data/lib/vendor/oauth/oauth.rb +0 -46
data/lib/common/policy.rb
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
# base caller
|
4
4
|
# UserPolicy.new(model: @model, user: User.current).can?(:update) -> can current user update @user
|
5
|
-
# ApplicationPolicy.new(user: Lux.
|
5
|
+
# ApplicationPolicy.new(user: Lux.current.var.user).can?(:admin_login?) -> can current user login to admin
|
6
6
|
|
7
7
|
# block will capture error message and be triggered only if error is present
|
8
|
-
#
|
8
|
+
# User.can?(:login) { |msg| http_error 401, "Err: #{msg}".red; return 'no access' }
|
9
9
|
|
10
10
|
class Policy
|
11
11
|
|
data/lib/common/url.rb
CHANGED
@@ -13,13 +13,15 @@ class Url
|
|
13
13
|
|
14
14
|
class << self
|
15
15
|
def current
|
16
|
-
new Lux.
|
16
|
+
new Lux.current.request.url
|
17
17
|
end
|
18
18
|
|
19
19
|
def force_locale loc
|
20
|
-
u = current
|
21
|
-
u.locale loc
|
22
|
-
u.relative
|
20
|
+
# u = current
|
21
|
+
# u.locale loc
|
22
|
+
# u.relative
|
23
|
+
|
24
|
+
'/' + loc.to_s + Lux.current.nav.full
|
23
25
|
end
|
24
26
|
|
25
27
|
def subdomain name, in_path=nil
|
data/lib/lux-fw.rb
CHANGED
@@ -1,16 +1,7 @@
|
|
1
|
-
# Gem.loaded_specs['lux-fw'].runtime_dependencies.each do |dep|
|
2
|
-
# require case dep.name
|
3
|
-
# when 'sequel_pg'; 'sequel'
|
4
|
-
# when 'hamlit-block'; 'hamlit/block'
|
5
|
-
# else dep.name
|
6
|
-
# end
|
7
|
-
# end
|
8
|
-
|
9
1
|
require 'awesome_print'
|
10
2
|
require 'as-duration'
|
11
3
|
require 'colorize'
|
12
4
|
require 'jwt'
|
13
|
-
require 'fast_blank'
|
14
5
|
require 'hamlit'
|
15
6
|
require 'hamlit/block'
|
16
7
|
require 'hashie'
|
@@ -19,9 +10,6 @@ require 'sequel'
|
|
19
10
|
|
20
11
|
require_relative './overload/object'
|
21
12
|
|
22
|
-
die 'Lux error: RACK_ENV is not defined' unless ENV['RACK_ENV']
|
23
|
-
die "Unsupported RACK_ENV [#{ENV['RACK_ENV']}]" unless ['production', 'development', 'test'].index(ENV['RACK_ENV'])
|
24
|
-
|
25
13
|
Encoding.default_internal = Encoding.default_external = 'utf-8'
|
26
14
|
|
27
15
|
Sequel.extension :inflector, :string_date_time
|
@@ -39,7 +27,7 @@ require_relative './lux/lux'
|
|
39
27
|
end
|
40
28
|
|
41
29
|
# load Tilt parsers
|
42
|
-
Tilt.register Tilt::ERBTemplate,
|
30
|
+
Tilt.register Tilt::ERBTemplate, 'erb'
|
43
31
|
Tilt.register Hamlit::Block::Template, 'haml'
|
44
32
|
|
45
33
|
# ensure we are not loading lux in lux folder
|
@@ -48,3 +36,4 @@ if Lux.root != Lux.fw_root
|
|
48
36
|
['./log', './tmp'].each { |d| `mkdir #{d}` unless Dir.exist?(d) }
|
49
37
|
end
|
50
38
|
|
39
|
+
Lux::Config.set_default_vars
|
data/lib/lux/api/api.rb
CHANGED
@@ -12,22 +12,21 @@
|
|
12
12
|
class Lux::Api
|
13
13
|
attr_accessor :message, :response
|
14
14
|
|
15
|
-
|
16
|
-
Lux::RescueFrom.define(self)
|
15
|
+
ClassCallbacks.define self, :before, :after, :on_error
|
17
16
|
|
18
17
|
class << self
|
19
18
|
# public mount method for router
|
20
19
|
def call path
|
21
20
|
return 'Unsupported API call' if !path[1] || path[3]
|
22
21
|
|
23
|
-
opts = Lux.
|
22
|
+
opts = Lux.current.params # Lux.current.request.post? ? Rack::Utils.parse_nested_query(Lux.current.request.body.read).h : Lux.current.params
|
24
23
|
|
25
24
|
if path[2]
|
26
25
|
opts[:_id] = path[1]
|
27
26
|
path[1] = path[2]
|
28
27
|
end
|
29
28
|
|
30
|
-
Lux.
|
29
|
+
Lux.current.response.body run(path[0], path[1], opts)
|
31
30
|
end
|
32
31
|
|
33
32
|
# public method for running actions on global class
|
@@ -55,7 +54,7 @@ class Lux::Api
|
|
55
54
|
begin
|
56
55
|
klass = (klass.singularize.camelize+'Api').constantize
|
57
56
|
rescue
|
58
|
-
return Lux.
|
57
|
+
return Lux.current.response.body({ error:"API #{klass} not found" })
|
59
58
|
end
|
60
59
|
|
61
60
|
klass.new.call(action.to_sym, params)
|
@@ -65,9 +64,17 @@ class Lux::Api
|
|
65
64
|
###
|
66
65
|
|
67
66
|
def call action, params={}
|
68
|
-
|
67
|
+
begin
|
68
|
+
rescued_call action, params
|
69
|
+
rescue Lux::Api::Error => e
|
70
|
+
response.error e.message if e.message.to_s != 'false'
|
71
|
+
rescue => e
|
72
|
+
rr [e.message, e.backtrace] if Lux.config(:log_to_stdout)
|
69
73
|
|
70
|
-
|
74
|
+
ClassCallbacks.execute self, :on_error, e
|
75
|
+
|
76
|
+
response.error e.message
|
77
|
+
end
|
71
78
|
|
72
79
|
response.render
|
73
80
|
end
|
@@ -76,7 +83,7 @@ class Lux::Api
|
|
76
83
|
# UserApi.new.call(:login, { email:'', pass:'' })
|
77
84
|
def rescued_call action, params={}
|
78
85
|
raise ForbidenError, "Protected action call" if [:call, :rescued_call, :params, :error].index action
|
79
|
-
|
86
|
+
error("Action #{action} not found in #{self.class.to_s}") unless respond_to? action
|
80
87
|
|
81
88
|
@response = Lux::Api::Response.new
|
82
89
|
@params = params
|
@@ -86,64 +93,44 @@ class Lux::Api
|
|
86
93
|
if @params[:_id]
|
87
94
|
eval "@object = @#{@class_name.underscore} = #{@class_name}[@params[:_id].to_i]"
|
88
95
|
@params.delete(:_id)
|
89
|
-
@response.meta :path, @object.path if @object.respond_to?(:path)
|
90
96
|
end
|
91
97
|
|
92
98
|
check_params_and_mock_instance_variables action
|
99
|
+
|
93
100
|
return if response.errors?
|
94
101
|
|
95
102
|
# execte api call and verify params if possible
|
96
|
-
|
103
|
+
ClassCallbacks.execute(self, :before)
|
97
104
|
|
98
|
-
|
105
|
+
response.data = send(action)
|
99
106
|
|
100
|
-
|
107
|
+
ClassCallbacks.execute(self, :after)
|
108
|
+
|
109
|
+
puts response.render.pretty_generate if Lux.config.log_to_stdout
|
101
110
|
end
|
102
111
|
|
103
112
|
def params
|
104
113
|
@params
|
105
114
|
end
|
106
115
|
|
107
|
-
def error what
|
108
|
-
raise Lux::Api::Error.new(what)
|
116
|
+
def error what=nil
|
117
|
+
raise Lux::Api::Error.new(what || 'false')
|
109
118
|
end
|
110
119
|
|
111
|
-
def
|
112
|
-
|
113
|
-
# api_data = instance_eval(&res) if api_data.kind_of?(Proc)
|
114
|
-
api_data = api_data.all.map{ |el| el.attributes } if api_data.class.name == 'ActiveRecord::Relation'
|
115
|
-
|
116
|
-
# sequel array of objects returned
|
117
|
-
if api_data.kind_of?(Array) && api_data[0] && api_data[0].respond_to?(:columns)
|
118
|
-
api_data = api_data.map(&:to_h)
|
119
|
-
end
|
120
|
-
|
121
|
-
# object given
|
122
|
-
if api_data.respond_to?(:attributes)
|
123
|
-
@response[:path] = api_data.path if api_data.id && api_data.respond_to?(:path)
|
124
|
-
api_data = api_data.attributes.reject{ |f| ['updated_by', 'updated_at', 'created_by', 'created_at'].index(f) }
|
125
|
-
end
|
126
|
-
|
127
|
-
response.data = api_data
|
128
|
-
|
129
|
-
# if we define _redirect then we will be redirected to exact page
|
130
|
-
# useful for file uploads
|
131
|
-
if @params[:_redirect]
|
132
|
-
if response.errors
|
133
|
-
Lux.page.flash.error @response[:errors].join(', ')
|
134
|
-
elsif response.message
|
135
|
-
Lux.page.flash.info @response[:message]
|
136
|
-
end
|
137
|
-
Lux.page.redirect(@params[:_redirect])
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
ap response.render if Lux.config.log_to_stdout
|
120
|
+
def message what
|
121
|
+
response.message = what
|
142
122
|
end
|
143
123
|
|
144
|
-
|
145
|
-
|
124
|
+
# default after block, can be overloaded
|
125
|
+
def after
|
126
|
+
return unless Lux.current
|
127
|
+
|
128
|
+
response.meta :ip, Lux.current.request.ip
|
129
|
+
response.meta :user, Lux.current.var.user ? Lux.current.var.user.email : nil
|
130
|
+
response.meta :http_status, Lux.current.response.status(200)
|
146
131
|
end
|
147
132
|
|
148
133
|
end
|
149
134
|
|
135
|
+
ApplicationApi = Class.new Lux::Api
|
136
|
+
|
data/lib/lux/api/lib/dsl.rb
CHANGED
@@ -38,9 +38,9 @@ class Lux::Api
|
|
38
38
|
errors = rules.validate(@params)
|
39
39
|
|
40
40
|
if errors.keys.length > 0
|
41
|
-
raise ArgumentError.new(errors.values.to_sentence) unless Lux.
|
41
|
+
raise ArgumentError.new(errors.values.to_sentence) unless Lux.current
|
42
42
|
|
43
|
-
Lux.
|
43
|
+
Lux.current.response.status 400
|
44
44
|
return response.error errors.values.to_sentence
|
45
45
|
end
|
46
46
|
|
@@ -36,7 +36,8 @@ class ModelApi < ApplicationApi
|
|
36
36
|
error "Object not found" unless @object
|
37
37
|
|
38
38
|
can? :read, @object
|
39
|
-
|
39
|
+
|
40
|
+
attributes
|
40
41
|
end
|
41
42
|
|
42
43
|
def create
|
@@ -61,9 +62,9 @@ class ModelApi < ApplicationApi
|
|
61
62
|
error 'object not created, error unknown'
|
62
63
|
end
|
63
64
|
|
64
|
-
|
65
|
+
add_response_object_path
|
65
66
|
|
66
|
-
|
67
|
+
attributes
|
67
68
|
end
|
68
69
|
|
69
70
|
def update
|
@@ -74,23 +75,29 @@ class ModelApi < ApplicationApi
|
|
74
75
|
end
|
75
76
|
|
76
77
|
can? :update, @object
|
78
|
+
|
79
|
+
@object.updated_at = Time.now.utc if @object.respond_to?(:updated_at)
|
77
80
|
@object.save if @object.valid?
|
81
|
+
|
78
82
|
report_errros_if_any @object
|
83
|
+
|
79
84
|
response.message '%s updated' % @class_name
|
80
|
-
|
81
|
-
|
85
|
+
|
86
|
+
add_response_object_path
|
87
|
+
|
88
|
+
attributes
|
82
89
|
end
|
83
90
|
|
84
91
|
# if you put active boolean field to objects, then they will be unactivated on destroy
|
85
|
-
def destroy
|
92
|
+
def destroy force: false
|
86
93
|
error "Object not found" unless @object
|
87
94
|
can? :delete, @object
|
88
95
|
|
89
|
-
if @object.respond_to?(:is_active)
|
96
|
+
if !force && @object.respond_to?(:is_active)
|
90
97
|
@object.update is_active: false
|
91
98
|
|
92
99
|
message 'Object deleted (exists in trashcan)'
|
93
|
-
elsif @object.respond_to?(:active)
|
100
|
+
elsif !force && @object.respond_to?(:active)
|
94
101
|
@object.update active: false
|
95
102
|
|
96
103
|
message 'Object deleted (exists in trashcan)'
|
@@ -101,7 +108,7 @@ class ModelApi < ApplicationApi
|
|
101
108
|
|
102
109
|
report_errros_if_any @object
|
103
110
|
|
104
|
-
|
111
|
+
attributes
|
105
112
|
end
|
106
113
|
|
107
114
|
def undelete
|
@@ -119,29 +126,39 @@ class ModelApi < ApplicationApi
|
|
119
126
|
response.message = 'Object raised from the dead.'
|
120
127
|
end
|
121
128
|
|
122
|
-
|
123
|
-
if obj.errors.count > 0
|
124
|
-
for k, v in obj.errors
|
125
|
-
desc = v.join(', ')
|
129
|
+
private
|
126
130
|
|
127
|
-
|
128
|
-
|
129
|
-
# desc = "#{k} #{desc}" if desc.starts_with?('is ')
|
130
|
-
end
|
131
|
+
def report_errros_if_any obj
|
132
|
+
return if obj.errors.count == 0
|
131
133
|
|
132
|
-
|
134
|
+
for k, v in obj.errors
|
135
|
+
desc = v.join(', ')
|
133
136
|
|
134
|
-
|
137
|
+
response.error k, desc
|
138
|
+
response.error desc
|
135
139
|
end
|
136
140
|
|
137
|
-
|
141
|
+
error
|
138
142
|
end
|
139
143
|
|
140
|
-
def can?
|
141
|
-
|
142
|
-
msg = 'No %s permission for %s' % [action.to_s.sub('?',''), Lux.
|
144
|
+
def can? action, object
|
145
|
+
object.can?(action) do |err|
|
146
|
+
msg = 'No %s permission for %s' % [action.to_s.sub('?',''), Lux.current.var.user ? Lux.current.var.user.email : :guests]
|
143
147
|
msg += ' on %s' % object.class.name if object
|
144
148
|
error msg
|
145
149
|
end
|
146
150
|
end
|
151
|
+
|
152
|
+
def add_response_object_path
|
153
|
+
begin
|
154
|
+
response.meta :path, @object.path if @object.respond_to?(:path)
|
155
|
+
rescue
|
156
|
+
nil
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def attributes
|
161
|
+
@object.attributes.pluck(:id, :name, :email)
|
162
|
+
end
|
163
|
+
|
147
164
|
end
|
data/lib/lux/api/lib/response.rb
CHANGED
@@ -1,9 +1,26 @@
|
|
1
1
|
class Lux::Api::Response
|
2
|
+
def self.error text
|
3
|
+
response = new
|
4
|
+
response.error text
|
5
|
+
response.render
|
6
|
+
end
|
7
|
+
|
8
|
+
###
|
9
|
+
|
2
10
|
attr_accessor :data
|
3
11
|
attr_accessor :message
|
4
12
|
|
13
|
+
def initialize
|
14
|
+
@meta = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def status num=nil
|
18
|
+
meta 'http_status', num.to_i if num && !@meta['http_status']
|
19
|
+
@meta['http_status']
|
20
|
+
end
|
21
|
+
|
5
22
|
def meta key, value
|
6
|
-
|
23
|
+
value = value.to_i if key == :status
|
7
24
|
@meta[key.to_s] = value
|
8
25
|
end
|
9
26
|
|
@@ -23,6 +40,10 @@ class Lux::Api::Response
|
|
23
40
|
@message = what
|
24
41
|
end
|
25
42
|
|
43
|
+
def redirect url
|
44
|
+
@meta['location'] = url
|
45
|
+
end
|
46
|
+
|
26
47
|
def errors?
|
27
48
|
(@error_hash || @errors) ? true : false
|
28
49
|
end
|
@@ -30,16 +51,20 @@ class Lux::Api::Response
|
|
30
51
|
def render
|
31
52
|
output = {}
|
32
53
|
|
33
|
-
output[:data] = @data if @data.present?
|
34
|
-
output[:meta] = @meta if @meta.present?
|
35
|
-
output[:message] = @message if @message.present?
|
36
|
-
|
37
54
|
if errors?
|
55
|
+
status 400
|
56
|
+
|
38
57
|
output[:error] ||= {}
|
39
58
|
output[:error][:messages] = @errors if @errors
|
40
59
|
output[:error][:hash] = @error_hash if @error_hash
|
41
60
|
end
|
42
61
|
|
62
|
+
Lux.current.response.status status
|
63
|
+
|
64
|
+
output[:data] = @data if @data.present?
|
65
|
+
output[:meta] = @meta if @meta.present?
|
66
|
+
output[:message] = @message if @message.present?
|
67
|
+
|
43
68
|
output
|
44
69
|
end
|
45
70
|
alias :to_hash :render
|
@@ -0,0 +1,182 @@
|
|
1
|
+
class Lux::Application
|
2
|
+
ClassCallbacks.define self, :before, :after, :routes, :on_error
|
3
|
+
|
4
|
+
attr_reader :route_target
|
5
|
+
|
6
|
+
# define common http methods as constants
|
7
|
+
[:get, :head, :post, :delete, :put, :patch].map(&:to_s).map(&:upcase).each { |m| eval "#{m} ||= '#{m}'" }
|
8
|
+
|
9
|
+
###
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@route_test = Lux::Application::RouteTest.new self
|
13
|
+
end
|
14
|
+
|
15
|
+
def debug
|
16
|
+
{ :@locale=>@locale, :@nav=>nav, :@subdomain=>@subdomain, :@domain=>@domain }
|
17
|
+
end
|
18
|
+
|
19
|
+
def body?
|
20
|
+
Lux.current.response.body ? true : false
|
21
|
+
end
|
22
|
+
|
23
|
+
def body data
|
24
|
+
Lux.current.response.body data
|
25
|
+
end
|
26
|
+
|
27
|
+
def current
|
28
|
+
Lux.current
|
29
|
+
end
|
30
|
+
|
31
|
+
def nav
|
32
|
+
Lux.current.nav
|
33
|
+
end
|
34
|
+
|
35
|
+
def params
|
36
|
+
Lux.current.params
|
37
|
+
end
|
38
|
+
|
39
|
+
def redirect where, msgs={}
|
40
|
+
Lux.current.redirect where, msgs
|
41
|
+
end
|
42
|
+
|
43
|
+
def request
|
44
|
+
Lux.current.request
|
45
|
+
end
|
46
|
+
|
47
|
+
# gets only root
|
48
|
+
def root cell
|
49
|
+
if RouteTest::LUX_PRINT_ROUTES
|
50
|
+
@route_target = cell
|
51
|
+
@route_test.print_route ''
|
52
|
+
end
|
53
|
+
|
54
|
+
call cell unless nav.root
|
55
|
+
end
|
56
|
+
|
57
|
+
def done?
|
58
|
+
throw :done if Lux.current.response.body
|
59
|
+
end
|
60
|
+
|
61
|
+
def get?
|
62
|
+
Lux.current.request.request_method == 'GET'
|
63
|
+
end
|
64
|
+
|
65
|
+
def post?
|
66
|
+
Lux.current.request.request_method == 'POST'
|
67
|
+
end
|
68
|
+
|
69
|
+
def plug name, &block
|
70
|
+
done?
|
71
|
+
|
72
|
+
m = "#{name}_plug".to_sym
|
73
|
+
return Lux.error(%[Method "#{m}" not defined in #{self.class}]) unless respond_to?(m)
|
74
|
+
send m, &block
|
75
|
+
|
76
|
+
done?
|
77
|
+
end
|
78
|
+
|
79
|
+
# map Main::RootCell do
|
80
|
+
# action :about
|
81
|
+
# action '/verified-user'
|
82
|
+
# end
|
83
|
+
# map api: ApiCell
|
84
|
+
def map route_object, &block
|
85
|
+
done?
|
86
|
+
|
87
|
+
if route_object.class == Hash
|
88
|
+
@route_name = route_object.keys.first
|
89
|
+
@route_target = route_object.values.first
|
90
|
+
else
|
91
|
+
@route_name = nav.root
|
92
|
+
@route_target = route_object
|
93
|
+
|
94
|
+
if block_given?
|
95
|
+
# map :dashboard do
|
96
|
+
# map o: DashboardOrgsCell
|
97
|
+
# root DashboardCell
|
98
|
+
# end
|
99
|
+
if route_object.class == Symbol
|
100
|
+
if test?(route_object)
|
101
|
+
current.nav.shift_to_root
|
102
|
+
instance_exec &block
|
103
|
+
raise NotFoundError.new %[Route "#{route_object}" matched but nothing is called]
|
104
|
+
end
|
105
|
+
|
106
|
+
return
|
107
|
+
end
|
108
|
+
else
|
109
|
+
@route_test.print_route '*'
|
110
|
+
return route_object.call
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
if block_given?
|
115
|
+
@route_test.instance_exec &block
|
116
|
+
return
|
117
|
+
end
|
118
|
+
|
119
|
+
@route_test.print_route '%s/*' % @route_name
|
120
|
+
|
121
|
+
call @route_target if test? @route_name
|
122
|
+
end
|
123
|
+
|
124
|
+
def action route_object
|
125
|
+
map route_object.values.first do
|
126
|
+
action route_object.keys.first
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def test? route
|
131
|
+
@route_test.test? route
|
132
|
+
end
|
133
|
+
|
134
|
+
# call :api_router
|
135
|
+
# call Main::UsersCell
|
136
|
+
# call Main::UsersCell, :index
|
137
|
+
# call 'main/orgs#show'
|
138
|
+
def call object=nil, action=nil
|
139
|
+
done?
|
140
|
+
|
141
|
+
case object
|
142
|
+
when Symbol
|
143
|
+
return send(object)
|
144
|
+
when Hash
|
145
|
+
object = [object.keys.first, object.values.first]
|
146
|
+
when String
|
147
|
+
if object.include?('#')
|
148
|
+
object = object.split('#')
|
149
|
+
object[0] = ('%s_cell' % object[0]).classify.constantize
|
150
|
+
else
|
151
|
+
object = ('%s_cell' % object).classify.constantize
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
object, action = object if object.is_a? Array
|
156
|
+
|
157
|
+
if action
|
158
|
+
object.action action
|
159
|
+
else
|
160
|
+
object.call
|
161
|
+
end
|
162
|
+
|
163
|
+
if body?
|
164
|
+
done?
|
165
|
+
else
|
166
|
+
Lux.error 'Lux cell "%s" called via route "%s" but page body is not set' % [object, nav.root]
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def main
|
171
|
+
catch(:done) do
|
172
|
+
begin
|
173
|
+
ClassCallbacks.execute self, :before
|
174
|
+
ClassCallbacks.execute self, :routes
|
175
|
+
ClassCallbacks.execute self, :after
|
176
|
+
rescue => e
|
177
|
+
ClassCallbacks.execute self, :on_error, e
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|