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.
Files changed (111) hide show
  1. checksums.yaml +5 -5
  2. data/.version +1 -1
  3. data/bin/cli/am +67 -41
  4. data/bin/cli/assets +1 -1
  5. data/bin/cli/console +3 -2
  6. data/bin/cli/eval +11 -2
  7. data/bin/cli/exceptions +1 -1
  8. data/bin/cli/generate +4 -0
  9. data/bin/cli/render +18 -0
  10. data/bin/cli/routes +3 -1
  11. data/bin/cli/server +4 -1
  12. data/bin/lux +10 -1
  13. data/bin/txt/nginx.conf +35 -18
  14. data/lib/common/class_attributes.rb +10 -10
  15. data/lib/common/{before_and_after.rb → class_callbacks.rb} +12 -21
  16. data/lib/common/crypt.rb +10 -6
  17. data/lib/common/dynamic_class.rb +12 -7
  18. data/lib/common/generic_model.rb +6 -6
  19. data/lib/common/hash_with_indifferent_access.rb +6 -284
  20. data/lib/{lux/helper/lib/html_tag.rb → common/html_tag_builder.rb} +1 -1
  21. data/lib/common/policy.rb +2 -2
  22. data/lib/common/url.rb +6 -4
  23. data/lib/lux-fw.rb +2 -13
  24. data/lib/lux/api/api.rb +34 -47
  25. data/lib/lux/api/lib/dsl.rb +2 -2
  26. data/lib/lux/api/lib/model_api.rb +40 -23
  27. data/lib/lux/api/lib/response.rb +30 -5
  28. data/lib/lux/application/application.rb +182 -0
  29. data/lib/lux/{controller → application}/lib/nav.rb +18 -21
  30. data/lib/lux/application/lib/plugs.rb +10 -0
  31. data/lib/lux/application/lib/render.rb +58 -0
  32. data/lib/lux/application/lib/route_test.rb +64 -0
  33. data/lib/lux/cache/cache.rb +27 -16
  34. data/lib/lux/cell/cell.rb +66 -54
  35. data/lib/lux/config/config.rb +51 -27
  36. data/lib/lux/current/current.rb +122 -0
  37. data/lib/lux/{page → current}/lib/encrypt_params.rb +2 -2
  38. data/lib/lux/{page → current}/lib/static_file.rb +29 -25
  39. data/lib/lux/delayed_job/delayed_job.rb +4 -4
  40. data/lib/lux/error/error.rb +31 -16
  41. data/lib/lux/helper/helper.rb +32 -37
  42. data/lib/lux/helper/helpers/application_helper.rb +3 -0
  43. data/lib/lux/helper/helpers/html_helper.rb +3 -0
  44. data/lib/lux/helper/helpers/mailer_helper.rb +11 -0
  45. data/lib/lux/lux.rb +40 -30
  46. data/lib/lux/mailer/mailer.rb +46 -29
  47. data/lib/lux/{page → response}/lib/flash.rb +1 -1
  48. data/lib/lux/response/lib/header.rb +21 -0
  49. data/lib/lux/response/response.rb +237 -0
  50. data/lib/lux/template/template.rb +20 -18
  51. data/lib/overload/array.rb +5 -0
  52. data/lib/overload/auto_loader.rb +27 -0
  53. data/lib/overload/blank.rb +8 -2
  54. data/lib/overload/float.rb +10 -0
  55. data/lib/overload/hash.rb +5 -12
  56. data/lib/overload/integer.rb +1 -5
  57. data/lib/overload/nil.rb +5 -0
  58. data/lib/overload/object.rb +1 -0
  59. data/lib/overload/r.rb +5 -0
  60. data/lib/overload/string.rb +4 -5
  61. data/lib/overload/{date.rb → time.rb} +23 -3
  62. metadata +37 -73
  63. data/bin/forever +0 -65
  64. data/bin/job_que +0 -39
  65. data/lib/lux/api/lib/application_api.rb +0 -38
  66. data/lib/lux/api/lib/doc_builder.rb +0 -19
  67. data/lib/lux/api/lib/rescue.rb +0 -8
  68. data/lib/lux/controller/controller.rb +0 -185
  69. data/lib/lux/controller/lib/plugs.rb +0 -10
  70. data/lib/lux/html/html.rb +0 -3
  71. data/lib/lux/html/lib/form.rb +0 -81
  72. data/lib/lux/html/lib/input.rb +0 -71
  73. data/lib/lux/html/lib/input_types.rb +0 -287
  74. data/lib/lux/lib/lux.rb +0 -51
  75. data/lib/lux/page/lib/response.rb +0 -178
  76. data/lib/lux/page/page.rb +0 -292
  77. data/lib/lux/rescue_from/rescue_from.rb +0 -63
  78. data/lib/plugins/assets/assets_plug.rb +0 -31
  79. data/lib/plugins/assets/helper_module_adapter.rb +0 -51
  80. data/lib/plugins/assets/init.rb +0 -4
  81. data/lib/plugins/db_helpers/array_and_hstore.rb +0 -64
  82. data/lib/plugins/db_helpers/arrays_and_tags.rb +0 -23
  83. data/lib/plugins/db_helpers/before_save.rb +0 -44
  84. data/lib/plugins/db_helpers/cached_find_by.rb +0 -45
  85. data/lib/plugins/db_helpers/class_and_instance.rb +0 -120
  86. data/lib/plugins/db_helpers/dataset_plugin.rb +0 -101
  87. data/lib/plugins/db_helpers/filter_wrappers.rb +0 -21
  88. data/lib/plugins/db_helpers/link_plugin.rb +0 -95
  89. data/lib/plugins/db_helpers/localize_plugin.rb +0 -57
  90. data/lib/plugins/db_helpers/primary_keys.rb +0 -36
  91. data/lib/plugins/db_helpers/typero_attributes.rb +0 -69
  92. data/lib/plugins/db_logger/init.rb +0 -18
  93. data/lib/plugins/db_logger/lux_response_adapter.rb +0 -9
  94. data/lib/plugins/paginate/helper.rb +0 -32
  95. data/lib/plugins/paginate/sequel_adapter.rb +0 -18
  96. data/lib/vendor/mini_assets/lib/asset.rb +0 -71
  97. data/lib/vendor/mini_assets/lib/asset/css.rb +0 -19
  98. data/lib/vendor/mini_assets/lib/asset/js.rb +0 -17
  99. data/lib/vendor/mini_assets/lib/base.rb +0 -69
  100. data/lib/vendor/mini_assets/lib/base/javascript.rb +0 -13
  101. data/lib/vendor/mini_assets/lib/base/stylesheet.rb +0 -5
  102. data/lib/vendor/mini_assets/lib/manifest.rb +0 -18
  103. data/lib/vendor/mini_assets/lib/opts.rb +0 -16
  104. data/lib/vendor/mini_assets/mini_assets.rb +0 -74
  105. data/lib/vendor/oauth/lib/facebook.rb +0 -35
  106. data/lib/vendor/oauth/lib/github.rb +0 -37
  107. data/lib/vendor/oauth/lib/google.rb +0 -41
  108. data/lib/vendor/oauth/lib/linkedin.rb +0 -41
  109. data/lib/vendor/oauth/lib/stackexchange.rb +0 -37
  110. data/lib/vendor/oauth/lib/twitter.rb +0 -41
  111. data/lib/vendor/oauth/oauth.rb +0 -46
@@ -1,23 +0,0 @@
1
- module Sequel::Plugins::LuxArrays
2
- module ClassMethods
3
- end
4
-
5
- module DatasetMethods
6
- # def tagged_with(tag=nil, field=:tags)
7
- # return self unless tag
8
- # where("? = any (#{field})", tag)
9
- # end
10
-
11
- # only postgree
12
- # Bucket.can.all_tags -> can_tags mora biti zadnji
13
- def all_tags(field=:tags, *args)
14
- sqlq = sql.split(' FROM ')[1]
15
- sqlq = "select lower(unnest(#{field})) as tag FROM " + sqlq
16
- sqlq = "select tag as name, count(tag) as cnt from (#{sqlq}) as tags group by tag order by cnt desc"
17
- DB.fetch(sqlq).map(&:h).or([])
18
- end
19
- end
20
-
21
- end
22
-
23
- Sequel::Model.plugin :lux_arrays
@@ -1,44 +0,0 @@
1
- module Sequel::Plugins::LuxBeforeSave
2
- module InstanceMethods
3
- def validate
4
- for field in columns
5
- schema = db_schema[field]
6
-
7
- # alert, no trim on to big field length
8
- if schema[:max_length] && self[field] && self[field].length > schema[:max_length]
9
- msg = 'Field "%s" max length is %s, got %d' % [field, schema[:max_length], self[field].length]
10
- errors.add(field, msg)
11
- end
12
- end
13
- super
14
- end
15
-
16
- def before_save
17
- # basic security
18
- self[:created_at] = Time.now.utc if ! self[:id] && respond_to?(:created_at)
19
- self[:updated_at] = Time.now.utc if respond_to?(:updated_at)
20
-
21
- if respond_to?(:created_by) || respond_to?(:updated_by)
22
- errors.add(:base, 'You have to be registered to save data') unless User.current
23
- end
24
-
25
- for f in [:created_by, :updated_by]
26
- if respond_to?(f) && !self[:id] && !self[f]
27
- raise UnauthorizedError, "Current user not defined, can't set :#{f} on #{self.class}" unless User.current
28
- self[f] = User.current.id
29
- end
30
- end
31
-
32
- Lux.cache.delete(cache_key)
33
- super
34
- end
35
-
36
-
37
- def before_destroy
38
- Lux.cache.delete(cache_key)
39
- super
40
- end
41
- end
42
- end
43
-
44
- Sequel::Model.plugin :lux_before_save
@@ -1,45 +0,0 @@
1
- # module Sequel::Plugins::LuxCachedFindBy
2
-
3
- # module ClassMethods
4
-
5
- # @@cached_find_by ||= {}
6
- # @@cache_all_for ||= {}
7
-
8
- # def find id
9
- # return nil if id.blank?
10
- # key = "#{self}/#{id}"
11
- # Lux.cache.thread(key) { where(id:id).first }
12
- # end
13
-
14
- # def find_by what
15
- # where(what).first
16
- # end
17
-
18
- # ###
19
-
20
- # # in top of the class, defines cache to be run in memory
21
- # # cache_all_for 10.minutes
22
- # def cache_all_for duration
23
- # @@cache_all_for[self.to_s] = duration
24
- # end
25
-
26
- # # City.cached_find_by(120, code: nav.root)
27
- # def cached_find_by duration, opts
28
- # key = opts.keys.first
29
- # value = opts.values.first
30
- # time = Time.now.to_i
31
-
32
- # @@cached_find_by[self.to_s] = nil if @@cached_find_by[self.to_s] && (@@cached_find_by[self.to_s][0] + duration) < time
33
- # @@cached_find_by[self.to_s] ||= [time, all]
34
-
35
- # for el in @@cached_find_by[self.to_s][1]
36
- # return el.dup if el[key] == value
37
- # end
38
-
39
- # nil
40
- # end
41
-
42
- # end
43
- # end
44
-
45
- # Sequel::Model.plugin :lux_cached_find_by
@@ -1,120 +0,0 @@
1
- # http://ricostacruz.com/cheatsheets/sequel.html
2
- # http://sequel.jeremyevans.net/rdoc/files/doc/model_plugins_rdoc.html
3
-
4
- module Sequel::Plugins::LuxClassAndInstance
5
-
6
- module ClassMethods
7
- [:desc, :asc, :pluck, :ids].each do |m|
8
- Sequel::Plugins.def_dataset_methods self, m
9
- end
10
-
11
- def find id
12
- return nil if id.blank?
13
- key = "#{self}/#{id}"
14
- Lux.cache.thread(key) { where(id:id).first }
15
- end
16
-
17
- def find_by what
18
- where(what).first
19
- end
20
-
21
- # active record like define scope
22
- # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/ClassMethods.html
23
- def scope name, body=nil, &block
24
- block ||= body
25
- dataset_module{define_method(name, &block)}
26
- end
27
-
28
- # instance scope, same as scope but runs on instance
29
- # iscope(:notes) { Note.where(created_by:id) }
30
- def iscope name, body=nil, &block
31
- block ||= body
32
- define_method(name, &block)
33
- end
34
-
35
- def where_or_new filter
36
- where(filter).first || new(filter)
37
- end
38
-
39
- def where_or_create filter
40
- where(filter).first || create(filter)
41
- end
42
-
43
- end
44
-
45
- module InstanceMethods
46
- def cache_key
47
- "#{self.class}/#{id}"
48
- end
49
-
50
- def attributes
51
- ret = {}
52
- for el in columns
53
- ret[el.to_s] = send(el.to_s) rescue '-'
54
- end
55
- ret
56
- end
57
-
58
- def touch
59
- save
60
- end
61
-
62
- def to_h
63
- ret = {}
64
- for el in self.keys
65
- ret[el] = send el
66
- end
67
- ret
68
- end
69
-
70
- def creator
71
- self[:created_by] ? User.find(self[:created_by]) : nil
72
- end
73
-
74
- def parent_model
75
- model_type.constantize.find(model_id)
76
- end
77
-
78
- # has?(:name, "Name is not defined") -> errors.add("Name is not defined")
79
- # has?(:name) -> false
80
- def has?(*args)
81
- if args[1] && args[1].kind_of?(String)
82
- unless self[args[0]].present?
83
- errors.add(args[0], args[1])
84
- return false
85
- end
86
- return true
87
- end
88
- args.each { |el| return false unless self[el].present? }
89
- true
90
- end
91
-
92
- def unique?(field)
93
- select(field).xwhere('id<>?', id).count == 0
94
- end
95
-
96
- def save!
97
- save
98
- end
99
- end
100
-
101
- module DatasetMethods
102
- def desc
103
- reverse :id
104
- end
105
-
106
- def asc
107
- order :id
108
- end
109
-
110
- def pluck field
111
- select_map field
112
- end
113
-
114
- def ids
115
- select(:id).map(&:id)
116
- end
117
- end
118
- end
119
-
120
- Sequel::Model.plugin :lux_class_and_instance
@@ -1,101 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Sequel::Model
4
-
5
- module ClassMethods
6
- [:xselect, :xorder, :xfrom, :xwhere, :xlike, :last_updated, :qs_search, :for].each do |m|
7
- Sequel::Plugins.def_dataset_methods self, m
8
- end
9
- end
10
-
11
- module DatasetMethods
12
- def xselect text
13
- select Sequel.lit text
14
- end
15
-
16
- def xorder text
17
- order Sequel.lit text
18
- end
19
-
20
- def xfrom text
21
- from Sequel.lit text
22
- end
23
-
24
- def xwhere hash_or_string, *args
25
- return where(Sequel.lit("coalesce(%s,'')!=''" % hash_or_string)) if hash_or_string.is_a?(Symbol)
26
- return where(Sequel.lit(hash_or_string, *args)) if hash_or_string.is_a?(String)
27
-
28
- q = hash_or_string.select{ |k,v| v.present? && v != 0 }
29
- q.keys.blank? ? self : where(q)
30
- end
31
-
32
- def xlike search, *args
33
- unless search.blank?
34
- search = search.gsub(/'/,"''").downcase
35
- where_str = []
36
-
37
- for str in search.split(/\s+/).select(&:present?)
38
- and_str = []
39
- str = "%#{str}%"
40
-
41
- for el in args
42
- if model.db_schema[el][:db_type] == 'jsonb'
43
- like_sql = "CAST(#{el} -> '#{Locale.current}' as text) ilike '#{str}'"
44
- if Locale::DEFAULT != Locale.current
45
- and_str << "(#{like_sql}) or (CAST(#{el} -> '#{Locale::DEFAULT}' as text) ilike '#{str}')"
46
- else
47
- and_str << like_sql
48
- end
49
- else
50
- and_str << "#{el}::text ilike '#{str}'"
51
- end
52
- end
53
-
54
- where_str.push '('+and_str.join(' or ')+')'
55
- end
56
-
57
- return where(Sequel.lit(where_str.join(' and ')))
58
- end
59
- self
60
- end
61
-
62
- def last_updated
63
- field = model.db_schema[:updated_at] ? :updated_at : :id
64
- order(Sequel.desc(field)).first
65
- end
66
-
67
- # search in array of qs arguments
68
- def qs_search *args
69
- for el in args
70
- dataset = where("#{el}=?", Lux.page.params[el]) if !Lux.page.params[el].blank? && model.db_schema[el]
71
- end
72
- dataset || self
73
- end
74
-
75
- def for obj
76
- # column_names
77
- field_name = "#{obj.class.name.underscore}_id".to_sym
78
- n1 = model.to_s.underscore
79
- n2 = obj.class.to_s.underscore
80
-
81
- cname = n1[0] < n2[0] ? n1+'_'+n2.pluralize : n2+'_'+n1.pluralize
82
-
83
- if (cname.classify.constantize rescue false)
84
- where Sequel.lit 'id in (select %s_id from %s where %s_id=%i)' % [n1, cname, n2, obj.id]
85
- elsif model.db_schema["#{n2}_ids".to_sym]
86
- return where Sequel.lit '%i=any(%s_ids)' % [obj.id, n2]
87
- elsif model.db_schema[field_name]
88
- return where Sequel.lit '%s=%i' % [field_name, obj.id]
89
- elsif model.db_schema[:model_type]
90
- return where(:model_type=>obj.class.to_s, :model_id=>obj.id)
91
- elsif obj.class.to_s == 'User'
92
- if obj.respond_to?(field_name)
93
- return where Sequel.lit '%s=?' % [field_name, obj.id]
94
- end
95
- return where Sequel.lit 'created_by=%i' % obj.id
96
- else
97
- r "Unknown link for #{obj.class} (probably missing db field)"
98
- end
99
- end
100
- end
101
- end
@@ -1,21 +0,0 @@
1
- module Sequel::Plugins::FilterWrappers
2
- module ClassMethods
3
-
4
- def validate &block
5
- define_method :validate do
6
- block.call
7
- super
8
- end
9
- end
10
-
11
- def before_save &block
12
- define_method :before_save do
13
- block.call
14
- super
15
- end
16
- end
17
-
18
- end
19
- end
20
-
21
- Sequel::Model.plugin :filter_wrappers
@@ -1,95 +0,0 @@
1
- # @notes.preload(:bucket)
2
-
3
- # class Array
4
- # def preload(name)
5
- # field = "#{name.to_s.singularize}_id".to_sym
6
- # ids = self.map{ |o| o[field] }.uniq
7
- # buckets = name.to_s.classify.constantize.where(id:ids).to_a
8
-
9
- # for o in self
10
- # o.send "#{name}=", buckets.select{ |el| o.id==el.id }[0]
11
- # end
12
- # end
13
- # end
14
-
15
- module Sequel::Plugins::LuxLink
16
- module ClassMethods
17
-
18
- # link :genders, default:1, collection:[ [1, 'male'], [2, 'Female'] ]
19
- # link :genders, collection: { 1=>'male', 2=>'Female' }
20
- # Object.genders
21
- # @object.gender
22
- def link name, opts={}
23
- name_s = name.to_s.singularize
24
- name_p = name.to_s.pluralize
25
-
26
- if opts[:collection]
27
- field = opts[:field] || "#{name_s}_id"
28
-
29
- class_eval %[
30
- def #{to_s}.#{name_p}
31
- #{opts[:collection].to_json}
32
- end
33
-
34
- def #{name_s}
35
- val = self[:#{field}] || #{opts[:default] || 'nil'}
36
- return unless val
37
- for k, v in #{to_s}.#{name_p}
38
- return v if k == val
39
- end
40
- nil
41
- end
42
- ]
43
- return
44
- end
45
-
46
- klass = opts[:class] ? opts[:class].to_s : name.to_s.singularize.classify
47
-
48
- # link :country, class:Country -> many_to_one :country
49
- if name.to_s == name_s
50
- class_eval %[
51
- def #{name_s}
52
- @#{name_s}_cached ||= #{klass}.find(#{name_s}_id)
53
- end
54
- def #{name_s}=(object)
55
- @#{name_s}_cached = object
56
- end
57
- ]
58
-
59
- # link :cities
60
- # postgre integer array, has to be in form #{name.singularize}_ids
61
- # city_ids = [1,2,4]
62
- # cities -> [<City:1>, <City:2>, <City:4>]
63
- elsif new.respond_to?("#{name_s}_ids")
64
- # puts "* #{to_s}.link :#{name} -> inline #{name_s}_ids".yellow
65
- class_eval %[
66
- def #{name_p}
67
- return if #{name_s}_ids.blank?
68
- ids = #{name_s}_ids.join(',')
69
- #{klass}.where(Sequel.lit('id in ('+ids+')'))
70
- end
71
- ]
72
-
73
- # link :countries -> one_to_many :countries
74
- else
75
- # one_to_many name, opts
76
- field = "#{to_s.tableize.singularize}_id"
77
- if klass.constantize.db_schema[field.to_sym]
78
- comm = "#{klass}.where(#{field}:id).default"
79
- else
80
- # we have a link table
81
- cname = klass[0,1] > to_s[0,1] ? "#{to_s}#{klass}" : "#{klass}#{to_s}"
82
- comm = "#{klass}.xwhere('id in (select #{klass.tableize.singularize}_id from #{cname.tableize} where #{to_s.tableize.singularize}_id=?)', id)"
83
- puts "* #{to_s}.link :#{name} -> #{comm}"
84
- end
85
- class_eval %[
86
- def #{name_p}
87
- #{comm}
88
- end
89
- ]
90
- end
91
- end
92
- end
93
- end
94
-
95
- Sequel::Model.plugin :lux_link