lux-fw 0.5.36 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/bin/README.md +26 -13
  4. data/bin/cli/benchmark.rb +18 -0
  5. data/bin/cli/cerb.rb +90 -0
  6. data/bin/cli/config.rb +4 -11
  7. data/bin/cli/console.rb +72 -30
  8. data/bin/cli/eval.rb +5 -27
  9. data/bin/cli/generate.rb +9 -11
  10. data/bin/cli/get.rb +14 -6
  11. data/bin/cli/memory.rb +12 -0
  12. data/bin/cli/new.rb +2 -0
  13. data/bin/cli/secrets.rb +3 -31
  14. data/bin/cli/server.rb +28 -9
  15. data/bin/cli/stats.rb +26 -20
  16. data/bin/cli/template.rb +10 -0
  17. data/bin/generate_doc +168 -0
  18. data/bin/lux +73 -10
  19. data/lib/README.md +18 -8
  20. data/lib/common/crypt.rb +60 -9
  21. data/lib/common/string_base.rb +54 -15
  22. data/lib/common/struct_opts.rb +30 -0
  23. data/lib/common/time_difference.rb +75 -0
  24. data/lib/loader.rb +41 -0
  25. data/lib/lux/application/README.md +67 -105
  26. data/lib/lux/application/application.rb +87 -329
  27. data/lib/lux/application/lib/magic_routes.rb +22 -0
  28. data/lib/lux/application/lib/nav.rb +218 -99
  29. data/lib/lux/application/lib/render.rb +47 -67
  30. data/lib/lux/application/lib/routes.rb +242 -0
  31. data/lib/lux/application/lib/shared.rb +47 -0
  32. data/lib/lux/application/lux_adapter.rb +6 -0
  33. data/lib/lux/application/lux_config.rb +26 -0
  34. data/lib/lux/cache/Find Results +24 -0
  35. data/lib/lux/cache/README.md +28 -35
  36. data/lib/lux/cache/cache.rb +78 -26
  37. data/lib/lux/cache/lib/memcached_server.rb +36 -0
  38. data/lib/lux/cache/lib/memory_server.rb +44 -0
  39. data/lib/lux/cache/lib/null_server.rb +33 -0
  40. data/lib/lux/cache/lib/sqlite_server.rb +62 -0
  41. data/lib/lux/cache/lux_adapter.rb +19 -0
  42. data/lib/lux/config/config.rb +93 -144
  43. data/lib/lux/config/lux_adapter.rb +27 -0
  44. data/lib/lux/controller/README.md +67 -12
  45. data/lib/lux/controller/controller.rb +237 -187
  46. data/lib/lux/current/README.md +29 -10
  47. data/lib/lux/current/current.rb +166 -77
  48. data/lib/lux/current/lib/current.rb +6 -0
  49. data/lib/lux/current/lib/encrypt_params.rb +38 -36
  50. data/lib/lux/current/lib/session.rb +86 -65
  51. data/lib/lux/current/lux_adapter.rb +5 -0
  52. data/lib/lux/environment/README.md +24 -0
  53. data/lib/lux/environment/environment.rb +91 -0
  54. data/lib/lux/environment/lux_adapter.rb +13 -0
  55. data/lib/lux/error/README.md +18 -19
  56. data/lib/lux/error/error.rb +207 -152
  57. data/lib/lux/error/lux_adapter.rb +9 -0
  58. data/lib/lux/logger/README.md +45 -0
  59. data/lib/lux/logger/lux_adapter.rb +27 -0
  60. data/lib/lux/lux.rb +72 -129
  61. data/lib/lux/mailer/README.md +12 -5
  62. data/lib/lux/mailer/lux_config.rb +4 -0
  63. data/lib/lux/mailer/mailer.rb +81 -59
  64. data/lib/lux/plugin/README.md +19 -0
  65. data/lib/lux/plugin/lux_adapter.rb +8 -0
  66. data/lib/lux/plugin/plugin.rb +65 -0
  67. data/lib/lux/render/README.md +100 -0
  68. data/lib/lux/render/render.rb +12 -0
  69. data/lib/lux/response/README.md +57 -1
  70. data/lib/lux/response/lib/file.rb +87 -65
  71. data/lib/lux/response/lib/flash.rb +49 -34
  72. data/lib/lux/response/lib/header.rb +34 -33
  73. data/lib/lux/response/response.rb +252 -164
  74. data/lib/lux/template/helper.rb +130 -0
  75. data/lib/lux/template/inline_render_proxy.rb +31 -0
  76. data/lib/lux/template/lux_adapter.rb +5 -0
  77. data/lib/lux/template/template.rb +127 -0
  78. data/lib/lux-fw.rb +15 -33
  79. data/lib/overload/array.rb +33 -7
  80. data/lib/overload/blank.rb +8 -3
  81. data/lib/overload/boolean.rb +46 -0
  82. data/lib/overload/class.rb +28 -0
  83. data/lib/overload/dir.rb +117 -7
  84. data/lib/overload/file.rb +40 -5
  85. data/lib/overload/float.rb +38 -3
  86. data/lib/overload/hash.rb +81 -91
  87. data/lib/overload/integer.rb +15 -0
  88. data/lib/overload/json.rb +26 -0
  89. data/lib/overload/nil.rb +4 -8
  90. data/lib/overload/object.rb +91 -24
  91. data/lib/overload/pathname.rb +11 -0
  92. data/lib/overload/raise_variants.rb +26 -36
  93. data/lib/overload/string.rb +100 -27
  94. data/lib/overload/thread_simple.rb +100 -0
  95. data/lib/overload/time.rb +60 -38
  96. data/tasks/loader.rb +20 -7
  97. metadata +170 -169
  98. data/bin/.DS_Store +0 -0
  99. data/bin/build_gem +0 -77
  100. data/bin/cli/dbconsole.rb +0 -8
  101. data/bin/cli/routes.rb +0 -90
  102. data/lib/.DS_Store +0 -0
  103. data/lib/common/.DS_Store +0 -0
  104. data/lib/common/class_attributes.rb +0 -51
  105. data/lib/common/class_callbacks.rb +0 -47
  106. data/lib/common/free_struct.rb +0 -42
  107. data/lib/common/hash_with_indifferent_access.rb +0 -74
  108. data/lib/common/html_tag_builder.rb +0 -91
  109. data/lib/common/method_attr.rb +0 -69
  110. data/lib/common/url.rb +0 -191
  111. data/lib/lux/.DS_Store +0 -0
  112. data/lib/lux/README.md +0 -10
  113. data/lib/lux/cache/.DS_Store +0 -0
  114. data/lib/lux/cache/lib/memory.rb +0 -36
  115. data/lib/lux/cache/lib/null.rb +0 -21
  116. data/lib/lux/config/README.md +0 -63
  117. data/lib/lux/config/lib/plugin.rb +0 -65
  118. data/lib/lux/config/lib/secrets.rb +0 -48
  119. data/lib/lux/current/.DS_Store +0 -0
  120. data/lib/lux/delayed_job/.DS_Store +0 -0
  121. data/lib/lux/delayed_job/README.md +0 -3
  122. data/lib/lux/delayed_job/delayed_job.rb +0 -51
  123. data/lib/lux/delayed_job/lib/memory.rb +0 -16
  124. data/lib/lux/delayed_job/lib/nsq.rb +0 -3
  125. data/lib/lux/delayed_job/lib/postgre.rb +0 -6
  126. data/lib/lux/delayed_job/lib/redis.rb +0 -19
  127. data/lib/lux/event_bus/README.md +0 -36
  128. data/lib/lux/event_bus/event_bus.rb +0 -27
  129. data/lib/lux/view/README.md +0 -85
  130. data/lib/lux/view/cell.rb +0 -102
  131. data/lib/lux/view/helper.rb +0 -120
  132. data/lib/lux/view/lib/cell_helpers.rb +0 -29
  133. data/lib/lux/view/view.rb +0 -95
  134. data/lib/overload/it.rb +0 -29
  135. data/plugins/api/README.md +0 -49
  136. data/plugins/api/api.rb +0 -153
  137. data/plugins/api/lib/attr.rb +0 -31
  138. data/plugins/api/lib/dsl.rb +0 -52
  139. data/plugins/api/lib/error.rb +0 -3
  140. data/plugins/api/lib/model_api.rb +0 -203
  141. data/plugins/api/lib/response.rb +0 -85
  142. data/plugins/db/.DS_Store +0 -0
  143. data/plugins/db/README.md +0 -29
  144. data/plugins/db/auto_migrate/auto_migrate.rb +0 -268
  145. data/plugins/db/auto_migrate/db.rake +0 -15
  146. data/plugins/db/helpers/array_search.rb +0 -27
  147. data/plugins/db/helpers/before_save_filters.rb +0 -32
  148. data/plugins/db/helpers/composite_primary_keys.rb +0 -36
  149. data/plugins/db/helpers/core.rb +0 -94
  150. data/plugins/db/helpers/dataset_methods.rb +0 -138
  151. data/plugins/db/helpers/enums_plugin.rb +0 -52
  152. data/plugins/db/helpers/find_precache.rb +0 -31
  153. data/plugins/db/helpers/link_objects.rb +0 -84
  154. data/plugins/db/helpers/schema_checks.rb +0 -83
  155. data/plugins/db/helpers/typero_adapter.rb +0 -71
  156. data/plugins/db/logger/config.rb +0 -22
  157. data/plugins/db/logger/lux_response_adapter.rb +0 -10
  158. data/plugins/db/paginate/helper.rb +0 -32
  159. data/plugins/db/paginate/sequel_adapter.rb +0 -23
  160. data/plugins/exceptions/exceptions.rake +0 -43
  161. data/plugins/exceptions/simple_exception.rb +0 -64
  162. data/plugins/favicon/favicon.rb +0 -10
  163. data/plugins/html/README.md +0 -3
  164. data/plugins/html/html_form.rb +0 -118
  165. data/plugins/html/html_input.rb +0 -98
  166. data/plugins/html/html_menu.rb +0 -79
  167. data/plugins/html/input_types.rb +0 -346
  168. data/plugins/js_widgets/README.md +0 -5
  169. data/plugins/js_widgets/js/html_tag.coffee +0 -42
  170. data/plugins/js_widgets/js/widgets.coffee +0 -161
  171. data/plugins/js_widgets/js_widgets.rb +0 -15
  172. data/plugins/oauth/lib/facebook.rb +0 -35
  173. data/plugins/oauth/lib/github.rb +0 -38
  174. data/plugins/oauth/lib/google.rb +0 -41
  175. data/plugins/oauth/lib/linkedin.rb +0 -41
  176. data/plugins/oauth/lib/stackexchange.rb +0 -41
  177. data/plugins/oauth/lib/twitter.rb +0 -38
  178. data/plugins/oauth/oauth.rb +0 -42
  179. data/plugins/policy/policy.rb +0 -53
  180. data/tasks/nginx.rake +0 -23
  181. /data/lib/lux/{view/lib → mailer}/helper_modules.rb +0 -0
@@ -1,85 +0,0 @@
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
-
10
- attr_accessor :data
11
- attr_accessor :message
12
-
13
- def initialize
14
- @meta = {}.h_wia
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
-
22
- def meta key, value
23
- value = value.to_i if key == :status
24
- @meta[key.to_s] = value
25
- end
26
-
27
- def error key, data=nil
28
- unless data
29
- data = key
30
- key = :base
31
- end
32
-
33
- key = key.to_s
34
-
35
- @errors ||= {}
36
- @errors[key] ||= []
37
- @errors[key].push data unless @errors[key].include?(data)
38
- end
39
-
40
- def errors?
41
- !!@errors
42
- end
43
-
44
- def message what
45
- @message = what
46
- end
47
-
48
- def redirect url
49
- @meta['location'] = url
50
- end
51
-
52
- def event name, opts={}
53
- @meta['event'] ||= []
54
- @meta['event'].push([name, opts])
55
- end
56
-
57
- def render
58
- output = {}
59
-
60
- if @errors
61
- status 400
62
-
63
- errors = @errors.inject({}) { |t, (k, v)| t[k] = v.join(', '); t }
64
- base = errors.values.uniq
65
-
66
- errors.delete('base')
67
-
68
- output[:error] ||= {}
69
- output[:error][:messages] = base
70
- output[:error][:hash] = errors if errors.keys.first
71
- end
72
-
73
- output[:data] = @data if @data.present?
74
- output[:meta] = @meta if @meta.present?
75
- output[:message] = @message if @message.present?
76
-
77
- output
78
- end
79
- alias :to_hash :render
80
-
81
- def write
82
- Lux.current.response.status status
83
- Lux.current.response.body render
84
- end
85
- end
data/plugins/db/.DS_Store DELETED
Binary file
data/plugins/db/README.md DELETED
@@ -1,29 +0,0 @@
1
- ### Create Sequel plugin
2
-
3
- ```
4
- module Sequel::Plugins::LuxHelp
5
-
6
- module ClassMethods
7
- def bla
8
- model # => refrence to model
9
- end
10
- end
11
-
12
- module InstanceMethods
13
-
14
- end
15
-
16
- module DatasetMethods
17
-
18
- end
19
-
20
- end
21
-
22
- Sequel::Model.plugin :lux_help
23
- ```
24
-
25
- Default scope: http://stackoverflow.com/questions/11669880/default-scope-in-sequel
26
-
27
- ### init
28
-
29
- If init.rb is defined it will be called, else all files will be loaded.
@@ -1,268 +0,0 @@
1
- # https://github.com/jeremyevans/sequel/blob/master/doc/schema_modification.rdoc
2
-
3
- class AutoMigrate
4
- attr_accessor :fields
5
-
6
- class << self
7
- def table table_name, opts={}
8
- die "Table [#{table_name}] not in plural -> expected [#{table_name.to_s.pluralize}]" unless table_name.to_s.pluralize == table_name.to_s
9
-
10
- die 'Table name "%s" is not permited' % table_name if [:categories].include?(table_name)
11
-
12
- unless DB.table_exists?(table_name.to_s)
13
- # http://sequel.jeremyevans.net/rdoc/files/doc/schema_modification_rdoc.html
14
- DB.create_table table_name do
15
- primary_key :id, Integer
16
- index :id, unique: true
17
- end
18
- end
19
-
20
- t = new table_name, opts
21
- yield t
22
- t.fix_fields
23
- t.update
24
- end
25
-
26
- def migrate &block
27
- instance_eval &block
28
- end
29
-
30
- def enable_extension name
31
- DB.run 'CREATE EXTENSION IF NOT EXISTS %s;' % name
32
- end
33
-
34
- def transaction_do text
35
- begin
36
- DB.run 'BEGIN; %s ;COMMIT;' % text
37
- rescue
38
- puts caller[1].red
39
- puts text.yellow
40
- raise $!
41
- end
42
- end
43
-
44
- end
45
-
46
- ###
47
-
48
- def initialize table_name, opts={}
49
- @fields = {}
50
- @opts = opts
51
- @table_name = table_name
52
-
53
- klass = @table_name.to_s.classify
54
-
55
- Object.send(:remove_const, klass) if Object.const_defined?(klass)
56
-
57
- eval %[class ::%s < Sequel::Model; end;] % klass
58
- @object = klass.constantize.new
59
- end
60
-
61
- def transaction_do text
62
- self.class.transaction_do text
63
- end
64
-
65
- def log_run what
66
- puts ' %s' % what.green
67
- self.class.transaction_do what
68
- end
69
-
70
- def fix_fields
71
- for vals in @fields.values
72
- type = vals[0]
73
- opts = vals[1]
74
-
75
- opts[:limit] ||= 255 if type == :string
76
- opts[:default] ||= false if type == :boolean
77
- opts[:null] = true unless opts[:null].class.name == 'FalseClass'
78
- opts[:array] ||= false
79
- opts[:unique] ||= false
80
- opts[:default] = [] if opts[:array]
81
- end
82
- end
83
-
84
- def get_db_column_type field
85
- type, opts = @fields[field]
86
- db_type = type
87
- db_type = :varchar if type == :string
88
- db_type = Time if type == :datetime
89
-
90
- if opts[:array]
91
- db_type = '%s(%s)' % [db_type, opts[:limit]] if type == :string
92
- db_type = '%s[]' % db_type
93
- else
94
- db_type = 'varchar(%s)' % opts[:limit] if opts[:limit]
95
- end
96
-
97
- db_type
98
- end
99
-
100
- def update
101
- puts "Table #{@table_name.to_s.yellow}, #{@fields.keys.length} fields"
102
-
103
- # remove extra fields
104
- if @opts[:drop].class != FalseClass
105
- for field in (@object.columns - @fields.keys - [:id])
106
- print "Remove colum #{@table_name}.#{field} (y/N): ".light_blue
107
- if STDIN.gets.chomp.downcase.index('y')
108
- DB.drop_column @table_name, field
109
- puts " drop_column #{field}".green
110
- end
111
- end
112
- end
113
-
114
- # loop trough defined fileds in schema
115
- for field, opts_in in @fields
116
- type = opts_in[0]
117
- opts = opts_in[1]
118
-
119
- db_type = get_db_column_type(field)
120
-
121
- # create missing columns
122
- unless @object.columns.index(field.to_sym)
123
- DB.add_column @table_name, field, db_type, opts
124
-
125
- if opts[:array]
126
- default = type == :string ? "ARRAY[]::character varying[]" : "ARRAY[]::integer[]"
127
- transaction_do "ALTER TABLE #{@table_name} ALTER COLUMN #{field} SET DEFAULT #{default};"
128
- end
129
-
130
- puts " add_column #{field}, #{db_type}, #{opts.to_json}".green
131
- next
132
- end
133
-
134
- if current = @object.db_schema[field]
135
- # unhandled db schema changes will not happen
136
- # ---
137
- # field - field name
138
- # current - current db_schema
139
- # type - new proposed type in schema
140
- # opts - new proposed types
141
-
142
- # if we have type set as array and in db it is not array, fix that
143
- if opts[:array]
144
- # covert to array unless is array
145
- if !current[:db_type].include?('[]')
146
- transaction_do %[
147
- alter table #{@table_name} alter #{field} drop default;
148
- alter table #{@table_name} alter #{field} type #{current[:db_type]}[] using array[#{field}];
149
- alter table #{@table_name} alter #{field} set default '{}';
150
- ]
151
-
152
- puts " Coverted #{@table_name}.#{field} to array type".green
153
- elsif !current[:default]
154
- # force default for array to be present
155
- default = type == :string ? "ARRAY[]::character varying[]" : "ARRAY[]::integer[]"
156
- transaction_do %[alter table #{@table_name} alter #{field} set default #{default};]
157
- end
158
- end
159
-
160
- # if we have array but scema says no array
161
- if !opts[:array] && current[:db_type].include?('[]')
162
- m = current[:type] == :integer ? "#{field}[0]" : "array_to_string(#{field}, ',')"
163
-
164
- transaction_do %[
165
- alter table #{@table_name} alter #{field} drop default;
166
- alter table #{@table_name} alter #{field} type #{current[:db_type].sub('[]','')} using #{m};
167
- ]
168
-
169
- puts " Coverted #{@table_name}.#{field}[] to non array type".red
170
- end
171
-
172
- # if varchar limit size has changed
173
- if type == :string && !opts[:array] && current[:max_length] != opts[:limit]
174
- transaction_do "ALTER TABLE #{@table_name} ALTER COLUMN #{field} TYPE varchar(#{opts[:limit]});"
175
- puts " #{field} limit, #{current[:max_length]}-> #{opts[:limit]}".green
176
- end
177
-
178
- # covert from varchar to text
179
- if type == :text && current[:max_length]
180
- transaction_do "ALTER TABLE #{@table_name} ALTER COLUMN #{field} SET DATA TYPE text"
181
- puts " #{field} limit from #{current[:max_length]} to no limit (text type)".green
182
- end
183
-
184
- # null true or false
185
- if current[:allow_null] != opts[:null]
186
- to_run = " #{field} #{opts[:null] ? 'DROP' : 'SET'} NOT NULL"
187
- log_run "ALTER TABLE #{@table_name} ALTER COLUMN #{to_run}"
188
- end
189
-
190
- # covert string to date
191
- if current[:type] == :string && [:date, :datetime].include?(type)
192
- log_run "ALTER TABLE #{@table_name} ALTER COLUMN #{field} TYPE #{type.to_s.upcase} using #{field}::#{type};"
193
- end
194
-
195
- #ap [current, field, type, opts] if current[:type] == :string && @table_name == :informators
196
-
197
- # field default changed
198
- if current[:default].to_s != opts[:default].to_s
199
- # skip for arrays
200
- next if opts[:array]
201
- next if current[:default].to_s.index('{}') and opts[:default] == []
202
- next if current[:default].to_s.starts_with?("'#{opts[:default]}':")
203
-
204
- if opts[:default].to_s.blank?
205
- log_run "ALTER TABLE #{@table_name} ALTER COLUMN #{field} drop default"
206
- else
207
- log_run "ALTER TABLE #{@table_name} ALTER COLUMN #{field} SET DEFAULT '#{opts[:default]}'; update #{@table_name} set #{field}='#{opts[:default]}' where #{field} is null;"
208
- end
209
- end
210
-
211
- if opts[:index]
212
- add_index(field)
213
- end
214
- end
215
- end
216
-
217
- end
218
-
219
- def add_index field
220
- type = @table_name.to_s.classify.constantize.new.db_schema[field][:db_type] rescue nil
221
-
222
- begin
223
- if type.index('[]')
224
- DB.run %[CREATE INDEX #{@table_name}_#{field}_gin_index on "#{@table_name}" USING GIN ("#{field}");]
225
- puts " * added array GIN index on #{field}".green
226
- else
227
- DB.add_index(@table_name, field)
228
- puts " * added index on #{field}".green
229
- end
230
- rescue; end
231
- end
232
-
233
- def rename field_old, field_new
234
- existing_fields = @table_name.to_s.classify.constantize.new.columns
235
-
236
- if existing_fields.index(field_old.to_sym) && ! existing_fields.index(field_new.to_sym)
237
- DB.rename_column(@table_name, field_old, field_new)
238
- puts " * renamed #{@table_name}.#{field_old} to #{@table_name}.#{field_new}"
239
- puts ' * please run auto migration again'
240
- exit
241
- end
242
- end
243
-
244
- def method_missing type, *args
245
- name = args[0]
246
- opts = args[1] || {}
247
-
248
- if [:string, :integer, :text, :boolean, :datetime, :date, :jsonb, :geography].index(type)
249
- @fields[name.to_sym] = [type, opts]
250
- elsif type == :decimal
251
- opts[:precision] ||= 8
252
- opts[:scale] ||= 2
253
- @fields[name.to_sym] = [:decimal, opts]
254
- elsif type == :timestamps
255
- opts[:null] ||= false
256
- @fields[:created_at] = [:datetime, opts]
257
- @fields[:created_by] = [:integer, opts]
258
- @fields[:updated_at] = [:datetime, opts]
259
- @fields[:updated_by] = [:integer, opts]
260
- elsif type == :polymorphic
261
- name ||= :model
262
- @fields["#{name}_id".to_sym] = [:integer, opts.merge(index: true) ]
263
- @fields["#{name}_type".to_sym] = [:string, opts.merge(limit: 100, index: "#{name}_id")]
264
- else
265
- puts "Unknown #{type.to_s.red}"
266
- end
267
- end
268
- end
@@ -1,15 +0,0 @@
1
- namespace :db do
2
- desc 'Automigrate schema'
3
- task am: :env do
4
-
5
- # Sequel extension and plugin test
6
- DB.run %[DROP TABLE IF EXISTS lux_tests;]
7
- DB.run %[CREATE TABLE lux_tests (int_array integer[] default '{}', text_array text[] default '{}');]
8
- class LuxTest < Sequel::Model; end;
9
- LuxTest.new.save
10
- die('"DB.extension :pg_array" not loaded') unless LuxTest.first.int_array.class == Sequel::Postgres::PGArray
11
- DB.run %[DROP TABLE IF EXISTS lux_tests;]
12
-
13
- require './config/schema.rb'
14
- end
15
- end
@@ -1,27 +0,0 @@
1
- Sequel::Model.dataset_module do
2
- # only postgree
3
- # Bucket.can.all_tags -> can_tags mora biti zadnji
4
- def all_tags field=:tags, *args
5
- sqlq = sql.split(' FROM ')[1]
6
- sqlq = "select lower(unnest(#{field})) as tag FROM " + sqlq
7
- sqlq = "select tag as name, count(tag) as cnt from (#{sqlq}) as tags group by tag order by cnt desc"
8
- DB.fetch(sqlq).map(&:h).or([])
9
- end
10
-
11
- # were users.id in (select unnest(user_ids) from doors)
12
- # def where_unnested klass
13
- # target_table = klass.to_s.tableize
14
- # where("#{table_name}.id in (select unnest(#{table_name.singularize}_ids) from #{target_table})")
15
- # end
16
- # assumes field name is tags
17
- def where_any data, field
18
- return self unless data.present?
19
-
20
- if data.is_a?(Array)
21
- xwhere data.map { |v| "#{v}=any(#{field})" }.join(' or ')
22
- else
23
- xwhere('?=any(%s)' % field, data)
24
- end
25
- end
26
- end
27
-
@@ -1,32 +0,0 @@
1
- module Sequel::Plugins::LuxBeforeSave
2
- module InstanceMethods
3
- def before_save
4
- # timestamps
5
- self[:created_at] = Time.now.utc if !self[:id] && respond_to?(:created_at)
6
- self[:updated_at] ||= Time.now.utc if respond_to?(:updated_at)
7
-
8
- # return error if user needed and not defined
9
- if !User.current && (respond_to?(:created_by) || respond_to?(:updated_by))
10
- errors.add(:base, 'You have to be registered to save data')
11
- return
12
- end
13
-
14
- # add timestamps
15
- self[:created_by] = User.current.id if respond_to?(:created_by) && !id
16
- self[:updated_by] ||= User.current.id if respond_to?(:updated_by)
17
-
18
- # delete cache key if defined
19
- Lux.cache.delete(cache_key)
20
-
21
- super
22
- end
23
-
24
- def before_destroy
25
- Lux.cache.delete(cache_key)
26
-
27
- super
28
- end
29
- end
30
- end
31
-
32
- Sequel::Model.plugin :lux_before_save
@@ -1,36 +0,0 @@
1
- # define composite key and check before save
2
- # class OrgUser < ApplicationModel
3
- # primary_keys :org_id, :user_id
4
- # end
5
-
6
- module Sequel::Plugins::PrimaryKeys
7
- module ClassMethods
8
- def primary_keys(*args)
9
- unless args[0]
10
- return respond_to?(:_primary_keys) ? _primary_keys : [:id]
11
- end
12
-
13
- define_singleton_method(:_primary_keys) { args }
14
- end
15
- end
16
-
17
- module InstanceMethods
18
- def before_save
19
- klass = self.class
20
-
21
- if klass.respond_to?(:_primary_keys)
22
- check = klass._primary_keys.inject(klass.dataset) do |record, field|
23
- record = record.where(field =>send(field))
24
- end
25
-
26
- check = check.xwhere('id<>?', id) if self[:id]
27
-
28
- raise StandardError, 'Record allredy exists (primary_keys check)' if check.first
29
- end
30
-
31
- super
32
- end
33
- end
34
- end
35
-
36
- Sequel::Model.plugin :primary_keys
@@ -1,94 +0,0 @@
1
- # http://ricostacruz.com/cheatsheets/sequel.html
2
- # http://sequel.jeremyevans.net/rdoc/files/doc/model_plugins_rdoc.html
3
-
4
- class Sequel::Model
5
- module ClassMethods
6
- def find_by what
7
- where(what).first
8
- end
9
-
10
- # active record like define scope
11
- # http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/ClassMethods.html
12
- def scope name, body=nil, &block
13
- block ||= body
14
- dataset_module{define_method(name, &block)}
15
- end
16
-
17
- # instance scope, same as scope but runs on instance
18
- # iscope(:notes) { Note.where(created_by:id) }
19
- def iscope name, body=nil, &block
20
- block ||= body
21
- define_method(name, &block)
22
- end
23
-
24
- def first_or_new filter
25
- where(filter).first || new(filter)
26
- end
27
-
28
- def first_or_create filter
29
- where(filter).first || create(filter)
30
- end
31
- end
32
-
33
- module InstanceMethods
34
- def cache_key
35
- "#{self.class}/#{id}"
36
- end
37
-
38
- def attributes
39
- ret = {}
40
- for el in columns
41
- ret[el.to_s] = send(el.to_s) rescue '-'
42
- end
43
- ret
44
- end
45
-
46
- def touch
47
- self[:updated_at] = Time.now.utc
48
- save columns: [:updated_at]
49
- end
50
-
51
- def to_h
52
- ret = {}
53
- for el in self.keys
54
- ret[el] = send el
55
- end
56
- ret
57
- end
58
-
59
- def creator
60
- self[:created_by] ? User.find(self[:created_by]) : nil
61
- end
62
-
63
- def parent_model
64
- model_type.constantize.find(model_id)
65
- end
66
-
67
- # has?(:name, "Name is not defined") -> errors.add("Name is not defined")
68
- # has?(:name) -> false
69
- def has?(*args)
70
- if args[1] && args[1].kind_of?(String)
71
- unless self[args[0]].present?
72
- errors.add(args[0], args[1])
73
- return false
74
- end
75
- return true
76
- end
77
- args.each { |el| return false unless self[el].present? }
78
- true
79
- end
80
-
81
- def unique?(field)
82
- select(field).xwhere('id<>?', id).count == 0
83
- end
84
-
85
- def save!
86
- save
87
- end
88
- end
89
-
90
- module DatasetMethods
91
- end
92
- end
93
-
94
-