lux-fw 0.2.3 → 0.5.32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/bin/README.md +33 -0
  4. data/bin/build_gem +76 -0
  5. data/bin/cli/config.rb +44 -0
  6. data/bin/cli/console.rb +61 -0
  7. data/bin/cli/dbconsole.rb +8 -0
  8. data/bin/cli/eval.rb +32 -0
  9. data/bin/cli/generate.rb +88 -0
  10. data/bin/cli/get.rb +12 -0
  11. data/bin/cli/new.rb +22 -0
  12. data/bin/cli/routes.rb +90 -0
  13. data/bin/cli/secrets.rb +40 -0
  14. data/bin/cli/server.rb +24 -0
  15. data/bin/cli/stats.rb +133 -0
  16. data/bin/lux +24 -65
  17. data/lib/common/class_attributes.rb +40 -64
  18. data/lib/common/class_callbacks.rb +34 -51
  19. data/lib/common/crypt.rb +10 -8
  20. data/lib/common/free_struct.rb +42 -0
  21. data/lib/common/hash_with_indifferent_access.rb +1 -1
  22. data/lib/common/html_tag_builder.rb +26 -2
  23. data/lib/common/url.rb +58 -40
  24. data/lib/lux/application/application.rb +290 -117
  25. data/lib/lux/application/lib/nav.rb +64 -38
  26. data/lib/lux/application/lib/render.rb +2 -1
  27. data/lib/lux/cache/cache.rb +87 -62
  28. data/lib/lux/cache/lib/{ram.rb → memory.rb} +5 -7
  29. data/lib/lux/cache/lib/null.rb +6 -8
  30. data/lib/lux/config/config.rb +109 -52
  31. data/lib/lux/config/lib/plugin.rb +65 -0
  32. data/lib/lux/config/lib/secrets.rb +48 -0
  33. data/lib/lux/controller/controller.rb +241 -0
  34. data/lib/lux/current/current.rb +33 -47
  35. data/lib/lux/current/lib/session.rb +72 -0
  36. data/lib/lux/delayed_job/delayed_job.rb +14 -7
  37. data/lib/lux/delayed_job/lib/memory.rb +7 -5
  38. data/lib/lux/delayed_job/lib/redis.rb +1 -1
  39. data/lib/lux/error/error.rb +164 -62
  40. data/lib/lux/event_bus/event_bus.rb +27 -0
  41. data/lib/lux/lux.rb +103 -66
  42. data/lib/lux/mailer/mailer.rb +23 -25
  43. data/lib/lux/response/lib/file.rb +81 -0
  44. data/lib/lux/response/lib/header.rb +14 -1
  45. data/lib/lux/response/response.rb +64 -56
  46. data/lib/lux/view/cell.rb +102 -0
  47. data/lib/lux/{helper → view}/helper.rb +39 -23
  48. data/lib/lux/view/lib/cell_helpers.rb +29 -0
  49. data/lib/lux/{helper/helpers/mailer_helper.rb → view/lib/helper_modules.rb} +7 -1
  50. data/lib/lux/{template/template.rb → view/view.rb} +21 -24
  51. data/lib/lux-fw.rb +4 -2
  52. data/lib/overload/array.rb +12 -6
  53. data/lib/overload/blank.rb +0 -1
  54. data/lib/overload/dir.rb +18 -0
  55. data/lib/overload/file.rb +1 -6
  56. data/lib/overload/hash.rb +56 -13
  57. data/lib/overload/integer.rb +2 -2
  58. data/lib/overload/it.rb +4 -4
  59. data/lib/overload/object.rb +37 -8
  60. data/lib/overload/{r.rb → raise_variants.rb} +23 -4
  61. data/lib/overload/string.rb +22 -6
  62. data/misc/demo/app/cells/demo_cell.rb +12 -0
  63. data/misc/demo/app/controllers/application_controller.rb +7 -0
  64. data/misc/demo/app/controllers/main/root_controller.rb +9 -0
  65. data/misc/demo/app/routes.rb +5 -0
  66. data/misc/demo/config/application.rb +14 -0
  67. data/misc/demo/config/assets.rb +6 -0
  68. data/misc/demo/config/environment.rb +7 -0
  69. data/misc/puma_auto_tune.rb +43 -0
  70. data/misc/unicorn.rb +37 -0
  71. data/{lib/lux → plugins}/api/api.rb +46 -29
  72. data/plugins/api/lib/attr.rb +31 -0
  73. data/{lib/lux → plugins}/api/lib/dsl.rb +3 -6
  74. data/{lib/lux → plugins}/api/lib/error.rb +0 -0
  75. data/{lib/lux → plugins}/api/lib/model_api.rb +51 -12
  76. data/{lib/lux → plugins}/api/lib/response.rb +31 -17
  77. data/{bin/cli/am → plugins/db/auto_migrate/auto_migrate.rb} +18 -35
  78. data/plugins/db/helpers/array_search.rb +27 -0
  79. data/plugins/db/helpers/before_save_filters.rb +32 -0
  80. data/plugins/db/helpers/composite_primary_keys.rb +36 -0
  81. data/plugins/db/helpers/core.rb +94 -0
  82. data/plugins/db/helpers/dataset_methods.rb +138 -0
  83. data/plugins/db/helpers/enums_plugin.rb +52 -0
  84. data/plugins/db/helpers/find_precache.rb +31 -0
  85. data/plugins/db/helpers/link_objects.rb +84 -0
  86. data/plugins/db/helpers/schema_checks.rb +83 -0
  87. data/plugins/db/helpers/typero_adapter.rb +71 -0
  88. data/plugins/db/logger/config.rb +22 -0
  89. data/plugins/db/logger/lux_response_adapter.rb +10 -0
  90. data/plugins/db/paginate/helper.rb +32 -0
  91. data/plugins/db/paginate/sequel_adapter.rb +23 -0
  92. data/plugins/exceptions/simple_exception.rb +64 -0
  93. data/plugins/favicon/favicon.rb +10 -0
  94. data/plugins/html/html_form.rb +118 -0
  95. data/plugins/html/html_input.rb +98 -0
  96. data/plugins/html/html_menu.rb +79 -0
  97. data/plugins/html/input_types.rb +346 -0
  98. data/plugins/js_widgets/js_widgets.rb +15 -0
  99. data/plugins/oauth/lib/facebook.rb +35 -0
  100. data/plugins/oauth/lib/github.rb +38 -0
  101. data/plugins/oauth/lib/google.rb +41 -0
  102. data/plugins/oauth/lib/linkedin.rb +41 -0
  103. data/plugins/oauth/lib/stackexchange.rb +41 -0
  104. data/plugins/oauth/lib/twitter.rb +38 -0
  105. data/plugins/oauth/oauth.rb +42 -0
  106. data/{lib/common → plugins/policy}/policy.rb +6 -7
  107. data/tasks/loader.rb +49 -0
  108. metadata +151 -49
  109. data/bin/cli/assets +0 -41
  110. data/bin/cli/console +0 -51
  111. data/bin/cli/dev +0 -1
  112. data/bin/cli/eval +0 -24
  113. data/bin/cli/exceptions +0 -62
  114. data/bin/cli/generate +0 -86
  115. data/bin/cli/get +0 -5
  116. data/bin/cli/nginx +0 -34
  117. data/bin/cli/production +0 -1
  118. data/bin/cli/render +0 -18
  119. data/bin/cli/routes +0 -14
  120. data/bin/cli/server +0 -4
  121. data/bin/cli/stat +0 -1
  122. data/bin/cli/systemd +0 -36
  123. data/bin/txt/nginx.conf +0 -46
  124. data/bin/txt/siege-and-puma.txt +0 -3
  125. data/lib/common/base32.rb +0 -47
  126. data/lib/common/dynamic_class.rb +0 -28
  127. data/lib/common/folder_model.rb +0 -50
  128. data/lib/common/generic_model.rb +0 -62
  129. data/lib/lux/application/lib/plugs.rb +0 -10
  130. data/lib/lux/application/lib/route_test.rb +0 -64
  131. data/lib/lux/cache/lib/memcached.rb +0 -3
  132. data/lib/lux/cell/cell.rb +0 -261
  133. data/lib/lux/current/lib/static_file.rb +0 -103
  134. data/lib/lux/helper/helpers/application_helper.rb +0 -3
  135. data/lib/lux/helper/helpers/html_helper.rb +0 -3
  136. data/lib/overload/auto_loader.rb +0 -27
  137. data/lib/overload/module.rb +0 -10
  138. data/lib/overload/string_inflections.rb +0 -8
@@ -0,0 +1,79 @@
1
+ # menu = HtmlMenu.new request.path
2
+ # menu.add 'Home', '/'
3
+ # menu.add 'People', '/people', lambda { |path| path.index('peor') }
4
+ # menu.add 'Jobs', '/jobs', { icon: true }
5
+ # menu.to_a
6
+ # ---
7
+ # [["Home", "/", {}, true], ["People", "/people", {}, false], ["Jobs", "/jobs", { icon: true }, false]]
8
+
9
+ class HtmlMenu
10
+ attr_accessor :path
11
+ attr_accessor :data
12
+
13
+ def initialize path
14
+ @path = path.to_s
15
+ @data = []
16
+ end
17
+
18
+ # item 'Links', '/link'
19
+ # item('Links', '/link') { ... }
20
+ def add name, path, opt1=nil, opt2=nil, &block
21
+ test, opts = opt1.is_a?(Hash) ? [opt2, opt1] : [opt1, opt2 || {}]
22
+
23
+ if @data.first
24
+ active = nil
25
+ active = false if test.class == FalseClass && !@data.first
26
+
27
+ if !@is_activated && @data.first && path != @data.first[1]
28
+ test ||= block || path
29
+ active = item_active(test)
30
+ @is_activated ||= active
31
+ end
32
+
33
+ @data.push [name, path, opts, active]
34
+ else
35
+ @data.push [name, path, opts, test.class == FalseClass ? false : nil]
36
+ end
37
+ end
38
+
39
+ # is menu item active?
40
+ def item_active data
41
+ case data
42
+ when Symbol
43
+ @path.include?(data.to_s)
44
+ when String
45
+ @path.starts_with? data
46
+ when Regexp
47
+ @path =~ data
48
+ when Proc
49
+ !! data.call(@path)
50
+ when Integer
51
+ true
52
+ when TrueClass
53
+ true
54
+ when FalseClass
55
+ false
56
+ else
57
+ raise ArgumentError.new("Unhandled class #{data.class} for #{data}")
58
+ end
59
+ end
60
+
61
+ # return result as a list
62
+ def to_a
63
+ @data[0][3] = true if ! @is_activated && @data[0][3].class != FalseClass
64
+ @data
65
+ end
66
+
67
+ # return result as list of hashes
68
+ def to_h
69
+ to_a.map do |it|
70
+ {
71
+ name: it[0],
72
+ path: it[1],
73
+ opts: it[2],
74
+ active: it[3],
75
+ }
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,346 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HtmlInput
4
+
5
+ # if you call .memo which is not existant, it translates to .as_memo with opts_prepare first
6
+ # def method_missing(meth, *args, &block)
7
+ # opts_prepare *args
8
+ # send "as_#{meth}"
9
+ # end
10
+
11
+ #############################
12
+ # custom fields definitions #
13
+ #############################
14
+
15
+ def as_string
16
+ @opts[:type] = 'text'
17
+ @opts[:autocomplete] ||= 'off'
18
+ @opts.tag(:input)
19
+ end
20
+ alias :as_text :as_string
21
+
22
+ def as_password
23
+ @opts[:type] = 'password'
24
+ @opts.tag(:input)
25
+ end
26
+
27
+ def as_hidden
28
+ @opts[:type] = 'hidden'
29
+ @opts.tag(:input)
30
+ end
31
+
32
+ def as_file
33
+ @opts[:type] = 'file'
34
+ @opts.tag(:input)
35
+ end
36
+
37
+ def as_textarea
38
+ val = @opts.delete(:value) || ''
39
+ val = val.join($/) if val.is_array?
40
+ comp_style = val.split(/\n/).length + val.length/100
41
+ comp_style = 6 if comp_style < 6
42
+ comp_style = 15 if comp_style > 15
43
+ @opts[:style] = "height:#{comp_style*20}px; #{@opts[:style]};"
44
+ @opts.tag(:textarea, val)
45
+ end
46
+ alias :as_memo :as_textarea
47
+
48
+ def as_checkbox
49
+ id = Lux.current.uid
50
+ hidden = { :name=>@opts.delete(:name), :type=>:hidden, :value=>@opts[:value] ? 1 : 0, :id=>id }
51
+ @opts[:type] = :checkbox
52
+ @opts[:onclick] = "document.getElementById('#{id}').value=this.checked ? 1 : 0; #{@opts[:onclick]}"
53
+ @opts[:checked] = @opts.delete(:value) ? 1 : nil
54
+ @opts.tag(:input)+hidden.tag(:input)
55
+ end
56
+
57
+ def as_checkboxes
58
+ body = []
59
+ collection = @opts.delete(:collection)
60
+
61
+ @opts[:type] = :checkbox
62
+
63
+ null = @opts.delete(:null)
64
+ value = @opts.delete(:value).to_s
65
+
66
+ body.push %[<label>#{opts.tag(:input)} #{null}</label>] if null
67
+
68
+ prepare_collection(collection).each do |el|
69
+ opts = @opts.dup
70
+ opts[:value] = el[0]
71
+ opts[:checked] = true if value == el[0].to_s
72
+
73
+ body.push %[<label>#{opts.tag(:input)} #{el[1]}</label>]
74
+ end
75
+
76
+ '<div class="form-checkboxes">%s</div>' % body.join("\n")
77
+ end
78
+
79
+ def as_select
80
+ body = []
81
+ collection = @opts.delete(:collection)
82
+
83
+ @opts[:class] ||= 'form-select'
84
+
85
+ if nullval = @opts.delete(:null)
86
+ body.push %[<option value="">#{nullval}</option>] if nullval
87
+ end
88
+
89
+ for el in prepare_collection(collection)
90
+ body.push(%[<option value="#{el[0]}"#{@opts[:value].to_s == el[0].to_s ? ' selected=""' : nil}>#{el[1]}</option>])
91
+ end
92
+
93
+ body = body.join("\n")
94
+ @opts.tag(:select, body)
95
+ end
96
+
97
+ def as_radio
98
+ return as_radios if @opts[:collection]
99
+
100
+ @opts[:type] = :radio
101
+ @opts[:checked] = @opts[:value] == @object.send(@name) ? true : nil
102
+ @opts.tag(:input)
103
+ end
104
+
105
+ def as_radios
106
+ body = []
107
+ collection = @opts.delete(:collection)
108
+
109
+ @opts[:type] = :radio
110
+
111
+ null = @opts.delete(:null)
112
+ value = @opts.delete(:value).to_s
113
+
114
+ body.push %[<label>#{opts.tag(:input)} #{null}</label>] if null
115
+
116
+ prepare_collection(collection).each do |el|
117
+ opts = @opts.dup
118
+ opts[:value] = el[0]
119
+ opts[:checked] = true if value == el[0].to_s
120
+
121
+ body.push %[<label>#{opts.tag(:input)} #{el[1]}</label>]
122
+ end
123
+
124
+ '<div class="form-radios">%s</div>' % body.join("\n")
125
+ end
126
+
127
+ def as_tag
128
+ @opts[:value] = @opts[:value].or([]).join(', ') if ['Array', 'Sequel::Postgres::PGArray'].index(@opts[:value].class.name)
129
+ @opts[:id] ||= Lux.current.uid
130
+ @opts[:type] = :text
131
+ @opts[:onkeyup] = %[draw_tag('#{@opts[:id]}')]
132
+ @opts[:autocomplete] ||= 'off'
133
+ @opts[:style] = ['display: block; width: 100%;', @opts[:style]].join(';')
134
+
135
+ ret = %[
136
+ <script>
137
+ window.draw_tag = window.draw_tag || function (id) {
138
+ tags = $.map(String($('#'+id).val()).split(/\s*,\s*/), function(el) {
139
+ val = el.replace(/\s+/,'-');
140
+ return val ? '<span class="label label-default">'+val+'</span> ' : ''
141
+ });
142
+ $('#'+id+'_tags').html(tags)
143
+ }</script>]
144
+ ret += @opts.tag(:input)
145
+ ret += %[<div id="#{@opts[:id]}_tags" style="margin-top:5px;"></div>]
146
+ ret += %[<script>if (window.$) { draw_tag('#{@opts[:id]}'); } else { window.onload = function(){ draw_tag('#{@opts[:id]}'); } }</script>]
147
+ ret
148
+ end
149
+
150
+ def as_date
151
+ @opts[:type] = 'text'
152
+ @opts[:style] = 'width: 120px; display: inline;'
153
+ @opts[:value] = @opts[:value].strftime('%d.%m.%Y') rescue @opts[:value]
154
+ @opts[:autocomplete] = :off
155
+
156
+ ret = @opts.tag(:input)
157
+ ret += '<span class="date-ago"> &bull; %s</span>' % Time.ago(Time.parse(@opts[:value])) if @opts[:value].present? && !@opts.delete(:no_ago)
158
+ # ret += ' &bull; <small>%s</small>' % @opts[:hint]
159
+ ret + %[<script>new Pikaday({ field: document.getElementById('#{@opts [:id]}'), format: "DD.MM.YYYY" }); </script>]
160
+ end
161
+
162
+ def as_datetime
163
+ value = @opts[:value]
164
+ id = @opts[:id]
165
+
166
+ value_day = value ? value.strftime('%Y-%m-%d') : ''
167
+ value_time = value ? value.strftime('%H:%M') : ''
168
+ value_all = value ? value.strftime('%H:%M') : ''
169
+
170
+ base = { class: 'form-control', onchange: "datetime_set('#{id}');", style: 'width: 160px; display: inline;' }
171
+
172
+ input_day = base.merge({ type: :date, id: '%s_day' % id, value: value_day }).tag :input
173
+ input_time = base.merge({ style: 'width: 110px; display: inline;', type: :time, id: '%s_time' % id, value: value_time }).tag :input
174
+ input_all = base.merge({ style: 'width: 150px; display: inline;', type: :text, id: id, name: @opts[:name], onfocus: 'blur();' }).tag :input
175
+ script = %[<script>window.datetime_set = function(id) { $('#'+id).val($('#'+id+'_day').val()+' '+$('#'+id+'_time').val()); }; datetime_set('#{id}');</script>]
176
+ desc = value ? '&mdash;' + Time.ago(value) : ''
177
+
178
+ [input_day, input_time, input_all, script, desc].join(' ')
179
+ end
180
+
181
+ def as_datebuttons
182
+ @opts[:type] = 'text'
183
+ @opts[:style] = 'width:100px; display:inline;'
184
+ @opts[:id] = "date_#{Lux.current.uid}"
185
+ id = "##{@opts[:id]}"
186
+ ret = @opts.tag(:input)
187
+ ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{DateTime.now.strftime('%Y-%m-%d')}'); return false;">Today</button>]
188
+ for el in [1, 3, 7, 14, 30]
189
+ date = DateTime.now+el.days
190
+ name = el.to_s
191
+ name += " (#{date.strftime('%a')})" if el < 7
192
+ ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{(DateTime.now+el.days).strftime('%Y-%m-%d')}'); return false;">+#{name}</button>]
193
+ end
194
+ ret
195
+ end
196
+
197
+ def as_user
198
+ button_text = if @opts[:value].to_i > 0
199
+ usr = User.find(@opts[:value])
200
+ "#{usr.name} (#{usr.email})"
201
+ else
202
+ 'Select user'
203
+ end
204
+
205
+ @opts[:style] = "width:auto;#{@opts[:style]};"
206
+ @opts[:onclick] = %[Dialog.render(this,'Select user', '/part/users/single_user?product_id=#{@object.bucket_id}');return false;]
207
+ @opts.tag :button, button_text
208
+ end
209
+
210
+ def as_photo
211
+ @opts[:type] = 'hidden'
212
+ if @opts[:value].present?
213
+ img = Photo.find(@opts[:value])
214
+ @image = %[ <img id="#{@opts[:id]}_image" style="height:34px; cursor:pointer;" src="#{img.thumbnail}" onclick="window.open('#{img.image.remote_url}')" /> <span class="btn btn-default btn-xs" onclick="$('##{@opts[:id]}').val('');$('##{@opts[:id]}_image').remove();$(this).remove();">&times;</span>]
215
+ end
216
+ picker = @opts.tag(:input)
217
+ %[<span class="btn btn-default" onclick="Photo.pick('#{@opts[:id]}', function(id) { alert('Chosen: '+id) })">Select photo</span>#{picker}#{@image}]
218
+ end
219
+
220
+ def as_photos
221
+ @opts[:type] = 'text'
222
+ @opts[:style] = 'width:150px; display:inline;'
223
+ @opts[:class] += ' mutiple'
224
+ @images = []
225
+ if @opts[:value].present?
226
+ for el in @opts[:value].split(',').uniq
227
+ img = Photo.find(el.to_i) rescue next
228
+ @images.push %[ <img style="height:34px; cursor:pointer;" src="#{img.thumbnail}" onclick="window.open('#{img.image.remote_url}')" />]
229
+ end
230
+ end
231
+ picker = @opts.tag(:input)
232
+ %[<span class="btn btn-default" onclick="Photo.pick('#{@opts[:id]}', function(id) { alert('Chosen: '+id) })">Add photo</span> #{picker}<div class="images" style="padding-top:5px;">#{@images.join(' ')}</div>]
233
+ end
234
+
235
+ def as_admin_password
236
+ @opts[:type] = 'text'
237
+ @opts[:style] = 'display:none;'
238
+ @opts[:value] = ''
239
+ ret = @opts.tag(:input)
240
+ %[<span class="btn btn-default" onclick="$(this).hide();$('##{@opts[:id]}').show().val('').attr('type','password').focus()">Set pass</span> #{ret}]
241
+ end
242
+
243
+ def as_color
244
+ value = @opts[:value]
245
+ @opts[:style] ||= 'width:150px; float: left; margin-right: 10px;'
246
+ as_text + %[<span style="background-color: #{value.or('#fff')}; height:34px; width:150px; display: inline-block;"></span>]
247
+ end
248
+
249
+ def as_array_values
250
+ name = @opts[:name]
251
+ ret = []
252
+ values = @opts[:value].kind_of?(String) ? @opts[:value].split(',') : @opts[:value]
253
+ for el in @opts[:collection]
254
+ ret.push %[<label style="position:relative; top:4px;">
255
+ <input name="#{name}[#{el[1]}]" value="1" type="checkbox" #{values[el[1]].present? ? 'checked=""' : ''} style="position:relative;top:2px; left:2px;" />
256
+ <span style="margin-right:10px;">#{el[0]}</span>
257
+ </label>]
258
+ end
259
+ ret.join('')
260
+ end
261
+
262
+ def as_pass
263
+ value = @opts[:value]
264
+ id = @opts[:id]
265
+ @opts[:value] = ''
266
+ @opts[:style] = 'width:200px; display:inline;'
267
+ ret = @opts.tag(:input)
268
+ %[<span class="btn btn-default" onclick="$(this).hide(); $(this).next().show().focus();">#{value.present? ? 'Change' : 'Set'} password</span><span id="s-#{id}" style="display:none;">#{ret} or <a onclick="p=$(this).parent(); p.hide(); p.prev().show(); return false;" href="#">cancel</a></span>]
269
+ end
270
+
271
+ def as_image
272
+ @opts[:type] = 'text'
273
+ @opts[:style] = 'width:350px; float:left;'
274
+ @opts[:autocomplete] ||= 'off'
275
+
276
+ input = @opts.tag(:input)
277
+ path_name = 'image_upload_dialog'
278
+ input += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick="Dialog.template('#{path_name}', function(url){ $('##{@opts[:id]}').val(url); Dialog.close(); })">upload</span>]
279
+
280
+ if @opts[:value].present?
281
+ input = %[<img onload="i = new Image(); i.src='#{@opts[:value]}'; $('#img_size_#{@opts[:id]}').html(i.width+' x '+i.height)" src="#{@opts[:value]}" onclick="window.open('#{@opts[:value]}')" style="width:100px; border:1px solid #ccc; float:left; margin-right:10px;" /> #{input}]
282
+ input += %[<br /><br /><span id="img_size_#{@opts[:id]}">...</span>]
283
+ end
284
+
285
+ '<span style="clear: both; display: block;"></span>' + input
286
+ end
287
+
288
+ def as_geo
289
+ @opts[:type] = 'text'
290
+ @opts[:style] = 'width:200px; float:left;'
291
+ ret = @opts.tag(:input)
292
+ ret += %[ <a target="new" href="http://maps.google.com/maps?q=loc:#{@opts[:value]}" style="display:block; margin-top:7px;">&nbsp;Show on map</a>]
293
+ end
294
+
295
+ def as_html_trix
296
+ %[
297
+ <div class="hide-for-popup" style="width: 100%;">
298
+ <textarea id="trix_#{@name}" name="#{@opts[:name]}" style="display:none;">#{@opts[:value]}</textarea>
299
+ <trix-editor input="trix_#{@name}"></trix-editor>
300
+ </div>
301
+ ]
302
+ end
303
+
304
+ def as_button_select
305
+ body = ['<div class="btn-group">']
306
+ collection = @opts.delete(:collection)
307
+ for el in prepare_collection(collection)
308
+ opts = { class:'btn btn-sm' }
309
+ ap "#{@opts[:name]} - #{@opts[:value]} == #{el[0]}"
310
+ opts[:class] += ' btn-primary' if @opts[:value].to_s == el[0].to_s
311
+ opts[:onclick] = "$(this).parent().find('.btn').removeClass('btn-primary'); $(this).addClass('btn-primary').blur(); $(this).addClass('btn-primary').blur();"
312
+ opts[:onclick] += @opts[:onclick] ? "(#{@opts[:onclick].sub(/;\s*$/,'')})('#{el[0]}')" : "$('##{@opts[:id]}').val('#{el[0]}')"
313
+ opts[:onclick] += '; return false;'
314
+ body.push opts.tag(:button, el[1])
315
+ end
316
+ body.push '</div>'
317
+ body.push @opts.pluck(:name, :id, :value).merge(type: :hidden).tag(:input)
318
+ body.join('')
319
+ end
320
+
321
+ def as_address
322
+ val = @opts[:value]
323
+ @opts[:style] ||= 'height:55px; width:250px; float: left; margin-right:5px;'
324
+ ret = as_textarea
325
+ ret += %[<div><a class="btn btn-default btn-xs" onclick="window.open('https://www.google.hr/maps?q='+$('##{@opts[:id]}').val()); return false;">open in new window</a></div>] if val.to_s.length > 5
326
+ ret
327
+ end
328
+
329
+ def as_images
330
+ path_name = defined?(Storage) ? :storages : :images
331
+
332
+ val = @opts[:value].to_s
333
+ ret = as_memo
334
+ ret += '<div style="margin-top: 7px;"></div>'
335
+ ret += val.split("\n").map{ |url| %[<a href="#{url}" target="_new"><img src="#{url}" style="height:50px; margin-right:5px; border:1px solid #ccc;" /></a>] }.join(' ')
336
+ ret += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick='Dialog.template("#{path_name}/select", function(url){ $("##{@opts[:id]}")[0].value += "\\n"+url; Dialog.close(); })'>add image</span>]
337
+ ret
338
+ end
339
+
340
+ def as_disabled
341
+ @opts[:disabled] = true
342
+ @opts.delete(:name)
343
+ as_text
344
+ end
345
+
346
+ end
@@ -0,0 +1,15 @@
1
+ ApplicationHelper.class_eval do
2
+
3
+ def widget name, opts={}
4
+ tag, name = name.split(':') if name === String
5
+
6
+ tag = :div
7
+ id = Lux.current.uid
8
+
9
+ data = block_given? ? yield : nil
10
+
11
+ { class: 'w %s' % name, id: id, 'data-json': opts.to_json }.tag(tag, data) +
12
+ %[<script>Widget.bind('#{id}');</script>]
13
+ end
14
+
15
+ end
@@ -0,0 +1,35 @@
1
+ # https://developers.facebook.com
2
+ # https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
3
+
4
+ class LuxOauth::Facebook < LuxOauth
5
+ def login
6
+ 'https://www.facebook.com/v2.8/dialog/oauth?scope=email&client_id=%s&redirect_uri=%s' % [@opts.key, CGI::escape(redirect_url)]
7
+ end
8
+
9
+ def format_response opts
10
+ {
11
+ email: opts['email'],
12
+ avatar: '//graph.facebook.com/%s/picture?type=large' % opts['id'],
13
+ name: opts['name']
14
+ }
15
+ end
16
+
17
+ def callback session_code
18
+ result = RestClient.post('https://graph.facebook.com/v2.8/oauth/access_token', {
19
+ redirect_uri: redirect_url,
20
+ client_id: @opts.key,
21
+ client_secret: @opts.secret,
22
+ code: session_code
23
+ }, { :accept => :json })
24
+
25
+ access_token = JSON.parse(result)['access_token']
26
+
27
+ response = RestClient.get('https://graph.facebook.com/me', {
28
+ :accept => :json,
29
+ :params => { :access_token => access_token }
30
+ })
31
+
32
+ format_response JSON.parse response
33
+ end
34
+ end
35
+
@@ -0,0 +1,38 @@
1
+ # https://github.com/settings/developers
2
+ # https://github.com/github/platform-samples/tree/master/api/ruby/basics-of-authentication
3
+ # https://github.com/settings/applications
4
+
5
+ class LuxOauth::Github < LuxOauth
6
+ def login
7
+ "https://github.com/login/oauth/authorize?scope=user:email&client_id=#{@opts.key}"
8
+ end
9
+
10
+ def format_response opts
11
+ {
12
+ email: opts['email'],
13
+ avatar: opts['avatar_url'],
14
+ github: opts['login'],
15
+ company: opts['company'],
16
+ location: opts['location'],
17
+ bio: opts['description'],
18
+ name: opts['name']
19
+ }
20
+ end
21
+
22
+ def callback session_code
23
+ result = RestClient.post('https://github.com/login/oauth/access_token', {
24
+ client_id: @opts.key,
25
+ client_secret: @opts.secret,
26
+ code: session_code
27
+ }, { :accept => :json })
28
+
29
+ # extract token and granted scopes
30
+ access_token = JSON.parse(result)['access_token']
31
+ # scopes = JSON.parse(result)['scope'].split(',')
32
+
33
+ opts = JSON.parse(RestClient.get('https://api.github.com/user', {:params => {:access_token => access_token}, :accept => :json}))
34
+
35
+ format_response opts
36
+ end
37
+ end
38
+
@@ -0,0 +1,41 @@
1
+ # https://console.developers.google.com
2
+ # https://developers.google.com/identity/protocols/googlescopes
3
+
4
+ class LuxOauth::Google < LuxOauth
5
+ def scope
6
+ [
7
+ 'https://www.googleapis.com/auth/userinfo.email',
8
+ 'https://www.googleapis.com/auth/userinfo.profile'
9
+ ]
10
+ end
11
+
12
+ def format_response opts
13
+ {
14
+ email: opts['email'],
15
+ name: opts['name'],
16
+ avatar: opts['picture'],
17
+ locale: opts['locale'],
18
+ gender: opts['gender']
19
+ }
20
+ end
21
+
22
+ def login
23
+ "https://accounts.google.com/o/oauth2/auth?client_id=#{@opts.key}&redirect_uri=#{redirect_url}&scope=#{scope.join('%20')}&response_type=code"
24
+ end
25
+
26
+ def callback session_code
27
+ result = RestClient.post('https://www.googleapis.com/oauth2/v3/token', {
28
+ grant_type: 'authorization_code',
29
+ client_id: @opts.key,
30
+ client_secret: @opts.secret,
31
+ code: session_code,
32
+ redirect_uri: redirect_url
33
+ })
34
+
35
+ hash = JSON.parse(result)
36
+
37
+ user = JSON.parse RestClient.get('https://www.googleapis.com/oauth2/v1/userinfo', { :params => {:access_token => hash['access_token'], :alt=>:json }})
38
+
39
+ format_response user
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # https://developer.linkedin.com/docs/oauth2
2
+ # https://developer.linkedin.com/docs/fields/basic-profile
3
+
4
+ class LuxOauth::Linkedin < LuxOauth
5
+ def scope
6
+ [
7
+ 'r_basicprofile',
8
+ 'r_emailaddress'
9
+ ]
10
+ end
11
+
12
+ def login
13
+ "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=#{@opts.key}&redirect_uri=#{redirect_url}&state=987654321&scope=#{scope.join('%20')}"
14
+ end
15
+
16
+ def format_response opts
17
+ {
18
+ email: opts['emailAddress'],
19
+ linkedin: opts['publicProfileUrl'],
20
+ description: opts['specialties'],
21
+ location: opts['location'],
22
+ avatar: opts['pictureUrl'],
23
+ name: "#{opts['firstName']} #{opts['lastName']}"
24
+ }
25
+ end
26
+
27
+ def callback session_code
28
+ result = RestClient.post('https://www.linkedin.com/oauth/v2/accessToken', {
29
+ grant_type: 'authorization_code',
30
+ client_id: @opts.key,
31
+ client_secret: @opts.secret,
32
+ code: session_code,
33
+ redirect_uri: redirect_url
34
+ })
35
+
36
+ access_token = JSON.parse(result)['access_token']
37
+ opts = JSON.parse RestClient::Request.execute(:method=>:get, :url=>'https://api.linkedin.com/v1/people/~:(id,picture-url,first-name,last-name,email-address,public-profile-url,specialties,location)?format=json', :headers => {'Authorization'=>"Bearer #{access_token}"})
38
+
39
+ format_response opts
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ # https://api.stackexchange.com/docs/authentication
2
+
3
+ class LuxOauth::Stackexchange < LuxOauth
4
+ def intialize
5
+ raise ArgumentError.new('OAUTH_ID needed') unless @opts.id
6
+ end
7
+
8
+ def login
9
+ 'https://stackexchange.com/oauth?client_id=%d&redirect_uri=%s' % [@opts.id, CGI::escape(redirect_url)]
10
+ end
11
+
12
+ def format_response opts
13
+ {
14
+ stackexchnage_user_id: opts['items'].first['user_id'],
15
+ user: opts['items'].first
16
+ }
17
+ end
18
+
19
+ def callback session_code
20
+ result = RestClient.post('https://stackexchange.com/oauth/access_token', {
21
+ redirect_uri: redirect_url,
22
+ client_id: @opts.id,
23
+ client_secret: @opts.secret,
24
+ code: session_code
25
+ }, { :accept => :json })
26
+
27
+ access_token = result.to_s.css_to_hash['access_token']
28
+
29
+ response = RestClient.get('https://api.stackexchange.com/2.2/me', {
30
+ accept: :json,
31
+ params: {
32
+ site: 'stackoverflow',
33
+ access_token: access_token,
34
+ key: @opts.key
35
+ }
36
+ })
37
+
38
+ format_response JSON.parse response
39
+ end
40
+ end
41
+
@@ -0,0 +1,38 @@
1
+ class LuxOauth::Twitter < LuxOauth
2
+ # def scope
3
+ # [
4
+ # 'r_basicprofile',
5
+ # 'r_emailaddress'
6
+ # ]
7
+ # end
8
+
9
+ # def login
10
+ # 'https://api.twitter.com/oauth/authorize?oauth_token=%s' % @key
11
+ # end
12
+
13
+ # def format_response opts
14
+ # {
15
+ # email: opts['emailAddress'],
16
+ # linkedin: opts['publicProfileUrl'],
17
+ # description: opts['specialties'],
18
+ # location: opts['location'],
19
+ # avatar: opts['pictureUrl'],
20
+ # name: "#{opts['firstName']} #{opts['lastName']}"
21
+ # }
22
+ # end
23
+
24
+ # def callback(session_code)
25
+ # result = RestClient.post('https://www.linkedin.com/oauth/v2/accessToken', {
26
+ # grant_type: 'authorization_code',
27
+ # client_id: @key,
28
+ # client_secret: @secret,
29
+ # code: session_code,
30
+ # redirect_uri: redirect_url
31
+ # })
32
+
33
+ # access_token = JSON.parse(result)['access_token']
34
+ # opts = JSON.parse RestClient::Request.execute(:method=>:get, :url=>'https://api.linkedin.com/v1/people/~:(id,picture-url,first-name,last-name,email-address,public-profile-url,specialties,location)?format=json', :headers => {'Authorization'=>"Bearer #{access_token}"})
35
+
36
+ # format_response opts
37
+ # end
38
+ end