lux-fw 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +7 -0
  2. data/.version +1 -0
  3. data/bin/cli/am +250 -0
  4. data/bin/cli/assets +37 -0
  5. data/bin/cli/console +50 -0
  6. data/bin/cli/dev +1 -0
  7. data/bin/cli/eval +15 -0
  8. data/bin/cli/exceptions +62 -0
  9. data/bin/cli/generate +82 -0
  10. data/bin/cli/get +5 -0
  11. data/bin/cli/nginx +28 -0
  12. data/bin/cli/production +1 -0
  13. data/bin/cli/routes +12 -0
  14. data/bin/cli/server +1 -0
  15. data/bin/cli/stat +1 -0
  16. data/bin/forever +65 -0
  17. data/bin/job_que +39 -0
  18. data/bin/lux +87 -0
  19. data/bin/txt/nginx.conf +29 -0
  20. data/bin/txt/siege-and-puma.txt +3 -0
  21. data/lib/common/base32.rb +47 -0
  22. data/lib/common/before_and_after.rb +71 -0
  23. data/lib/common/class_attributes.rb +66 -0
  24. data/lib/common/class_method_params.rb +94 -0
  25. data/lib/common/crypt.rb +66 -0
  26. data/lib/common/folder_model.rb +50 -0
  27. data/lib/common/generic_model.rb +62 -0
  28. data/lib/common/policy.rb +54 -0
  29. data/lib/common/string_base.rb +49 -0
  30. data/lib/common/url.rb +171 -0
  31. data/lib/lux/api/api.rb +150 -0
  32. data/lib/lux/api/lib/application_api.rb +19 -0
  33. data/lib/lux/api/lib/doc_builder.rb +18 -0
  34. data/lib/lux/api/lib/dsl.rb +73 -0
  35. data/lib/lux/api/lib/model_api.rb +145 -0
  36. data/lib/lux/api/lib/rescue.rb +18 -0
  37. data/lib/lux/cache/cache.rb +71 -0
  38. data/lib/lux/cache/lib/memcached.rb +3 -0
  39. data/lib/lux/cache/lib/null.rb +23 -0
  40. data/lib/lux/cache/lib/ram.rb +38 -0
  41. data/lib/lux/cell/cell.rb +260 -0
  42. data/lib/lux/config/config.rb +88 -0
  43. data/lib/lux/controller/controller.rb +185 -0
  44. data/lib/lux/controller/lib/nav.rb +77 -0
  45. data/lib/lux/controller/lib/plugs.rb +10 -0
  46. data/lib/lux/delayed_job/delayed_job.rb +44 -0
  47. data/lib/lux/delayed_job/lib/memory.rb +14 -0
  48. data/lib/lux/delayed_job/lib/nsq.rb +3 -0
  49. data/lib/lux/delayed_job/lib/postgre.rb +6 -0
  50. data/lib/lux/delayed_job/lib/redis.rb +19 -0
  51. data/lib/lux/error/error.rb +75 -0
  52. data/lib/lux/helper/helper.rb +109 -0
  53. data/lib/lux/html/html.rb +3 -0
  54. data/lib/lux/html/lib/form.rb +81 -0
  55. data/lib/lux/html/lib/input.rb +71 -0
  56. data/lib/lux/html/lib/input_types.rb +277 -0
  57. data/lib/lux/lux.rb +164 -0
  58. data/lib/lux/mailer/mailer.rb +73 -0
  59. data/lib/lux/page/lib/encrypt_params.rb +44 -0
  60. data/lib/lux/page/lib/flash.rb +49 -0
  61. data/lib/lux/page/lib/static_file.rb +97 -0
  62. data/lib/lux/page/page.rb +271 -0
  63. data/lib/lux/rescue_from/rescue_from.rb +61 -0
  64. data/lib/lux/template/template.rb +95 -0
  65. data/lib/lux-fw.rb +48 -0
  66. data/lib/overload/array.rb +52 -0
  67. data/lib/overload/blank.rb +62 -0
  68. data/lib/overload/date.rb +58 -0
  69. data/lib/overload/file.rb +14 -0
  70. data/lib/overload/hash.rb +86 -0
  71. data/lib/overload/hash_wia.rb +282 -0
  72. data/lib/overload/inflections.rb +199 -0
  73. data/lib/overload/integer.rb +19 -0
  74. data/lib/overload/module.rb +10 -0
  75. data/lib/overload/nil.rb +8 -0
  76. data/lib/overload/object.rb +77 -0
  77. data/lib/overload/string.rb +89 -0
  78. data/lib/overload/string_inflections.rb +7 -0
  79. data/lib/overload/struct.rb +5 -0
  80. data/lib/plugins/assets/assets_plug.rb +26 -0
  81. data/lib/plugins/assets/helper_module_adapter.rb +49 -0
  82. data/lib/plugins/assets/init.rb +4 -0
  83. data/lib/plugins/db_helpers/array_and_hstore.rb +64 -0
  84. data/lib/plugins/db_helpers/arrays_and_tags.rb +23 -0
  85. data/lib/plugins/db_helpers/before_save.rb +44 -0
  86. data/lib/plugins/db_helpers/cached_find_by.rb +45 -0
  87. data/lib/plugins/db_helpers/class_and_instance.rb +120 -0
  88. data/lib/plugins/db_helpers/dataset_plugin.rb +101 -0
  89. data/lib/plugins/db_helpers/filter_wrappers.rb +21 -0
  90. data/lib/plugins/db_helpers/link_plugin.rb +95 -0
  91. data/lib/plugins/db_helpers/localize_plugin.rb +57 -0
  92. data/lib/plugins/db_helpers/primary_keys.rb +36 -0
  93. data/lib/plugins/db_helpers/typero_attributes.rb +69 -0
  94. data/lib/plugins/db_logger/init.rb +18 -0
  95. data/lib/plugins/db_logger/lux_response_adapter.rb +9 -0
  96. data/lib/plugins/paginate/helper.rb +32 -0
  97. data/lib/plugins/paginate/sequel_adapter.rb +18 -0
  98. data/lib/vendor/mini_assets/mini_asset/base.rb +167 -0
  99. data/lib/vendor/mini_assets/mini_asset/css.rb +38 -0
  100. data/lib/vendor/mini_assets/mini_asset/js.rb +38 -0
  101. data/lib/vendor/mini_assets/mini_asset.rb +31 -0
  102. data/lib/vendor/oauth/lib/facebook.rb +35 -0
  103. data/lib/vendor/oauth/lib/github.rb +37 -0
  104. data/lib/vendor/oauth/lib/google.rb +41 -0
  105. data/lib/vendor/oauth/lib/linkedin.rb +41 -0
  106. data/lib/vendor/oauth/lib/stackexchange.rb +37 -0
  107. data/lib/vendor/oauth/lib/twitter.rb +41 -0
  108. data/lib/vendor/oauth/oauth.rb +46 -0
  109. metadata +334 -0
@@ -0,0 +1,3 @@
1
+ module Lux::DelayedJob::NSQ
2
+
3
+ end
@@ -0,0 +1,6 @@
1
+ # with _u as (select id from users order by updated_at asc limit 1)
2
+ # update users set updated_at=now() where id in (select id from _u) RETURNING id;
3
+
4
+ module Lux::DelayedJob::Postgre
5
+
6
+ end
@@ -0,0 +1,19 @@
1
+ module Lux::DelayedJob
2
+ module Redis
3
+ extend self
4
+
5
+ def que
6
+ @@server ||= Lux.config(:redis_server)
7
+ end
8
+
9
+ def push(list)
10
+ que.push Marshal.dump list
11
+ end
12
+
13
+ def pop
14
+ que.process(true) do |message|
15
+ Marshal.load(message) rescue nil
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # 400: for bad parameter request or similar
4
+ BadRequestError ||= Class.new(StandardError)
5
+
6
+ # 401: for unauthorized access
7
+ UnauthorizedError ||= Class.new(StandardError)
8
+
9
+ # 403: for unalloed access
10
+ ForbidenError ||= Class.new(StandardError)
11
+
12
+ # 404: for not found pages
13
+ NotFoundError ||= Class.new(StandardError)
14
+
15
+ # 503: for too many requests at the same time
16
+ RateLimitError ||= Class.new(StandardError)
17
+
18
+ module Lux::Error
19
+ OUT_OF_PROCESS_ERROR_PATH = Lux.root.join('tmp/request-error.txt')
20
+
21
+ extend self
22
+
23
+ def try(name)
24
+ begin
25
+ yield
26
+ rescue
27
+ log($!)
28
+ inline('%s (%s)' % [name, $!.class])
29
+ end
30
+ end
31
+
32
+ def render data
33
+ %[<html><head><title>Server error (#{Lux.page.status})</title></head><body style="background:#fdd;"><pre style="color:red; padding:10px; font-size:14pt;">#{data.gsub('<','&lt;')}</pre></body></html>]
34
+ end
35
+
36
+ def show(desc=nil)
37
+ Lux.page.status(500)
38
+ data = "Lux #{Lux.page.status} error\n\n#{desc}"
39
+ Lux.page.body! render(data)
40
+ end
41
+
42
+ def inline(name=nil, o=nil)
43
+ o ||= $!
44
+ trace = o.backtrace.select{ |el| el.index(Lux.root.to_s) }.map{ |el| el.split(Lux.root.to_s, 2)[1] }.map{ |el| "- #{el}" }.join("\n")
45
+ msg = $!.to_s.gsub('","',%[",\n "]).gsub('<','&lt;')
46
+ %[<pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:14pt; font-size:10pt;"><b style="font-size:110%;">#{name || 'Undefined name'}</b>\n\n<b>#{msg}</b>\n\n#{trace}</pre>]
47
+ end
48
+
49
+ def log(exception)
50
+ return if Lux.env == 'test'
51
+ return unless Lux.page
52
+
53
+ history = exception.backtrace.reject{ |el| el.index('/gems/') }.map{ |el| el.sub(Lux.root.to_s, '') }.join("\n")
54
+
55
+ data = '%s in %s (user: %s)' % [exception.class, Lux.page.request.url, (Lux.page.var.user.email rescue 'guest')]
56
+ data = [data, exception.message, history].join("\n\n")
57
+ key = Digest::SHA1.hexdigest exception.backtrace.first.split(' ').first
58
+
59
+ folder = Lux.root.join('log/exceptions').to_s
60
+ Dir.mkdir(folder) unless Dir.exists?(folder)
61
+ folder += "/#{exception.class.to_s.tableize.gsub('/','-')}"
62
+ Dir.mkdir(folder) unless Dir.exists?(folder)
63
+
64
+ File.write("#{folder}/#{key}.txt", data)
65
+ end
66
+
67
+
68
+ # if some other process as asset complation raises error
69
+ # use this method to show errors
70
+ def log_out_of_process_error(data)
71
+ return unless Lux.config(:show_server_errors)
72
+ File.write OUT_OF_PROCESS_ERROR_PATH, data
73
+ end
74
+
75
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ # plugins should extend this
4
+ module DefaultHelper
5
+ end
6
+
7
+ class Lux::Helper
8
+
9
+ class << self
10
+ def for group, locals={}, &block
11
+ base_class_helper = if [Symbol, String].include?(group.class)
12
+ "#{group}_helper".classify.constantize
13
+ elsif group
14
+ group
15
+ end
16
+
17
+ helper = Lux::Helper.new
18
+ helper.extend DefaultHelper
19
+ helper.extend base_class_helper if base_class_helper
20
+
21
+ # locals overide globals
22
+ for k, v in locals
23
+ helper.instance_variable_set("@#{k.to_s.sub('@','')}", v)
24
+ end
25
+
26
+ return helper.instance_exec &block if block
27
+
28
+ helper
29
+ end
30
+ end
31
+
32
+ def no_white_space
33
+ yield.gsub(/>\s+</,'><')
34
+ end
35
+
36
+ def page
37
+ Lux.page
38
+ end
39
+
40
+ def request
41
+ Lux.page.request
42
+ end
43
+
44
+ def params
45
+ Lux.page.params
46
+ end
47
+
48
+ # renders just template but it is called
49
+ # = render :_link, link:link
50
+ # = render 'main/links/_link', link:link
51
+ def render name, locals={}
52
+ if name.is_array?
53
+ return name.map { |b| render(b) }.join("\n")
54
+ elsif name.respond_to?(:db_schema)
55
+ path = Lux.thread[:last_template_path].split('/')[1]
56
+ table_name = name.class.name.tableize
57
+ locals[table_name.singularize.to_sym] = name
58
+ eval "@_#{table_name.singularize} = name"
59
+ name = "#{path}/#{table_name}/_#{table_name.singularize}"
60
+ else
61
+ name = name.to_s
62
+ name = "#{Thread.current[:lux][:last_template_path]}/#{name}" unless name.index('/')
63
+ end
64
+
65
+ ivh = instance_variables_hash
66
+
67
+ for k, v in locals
68
+ ivh["@_#{k}"] = v
69
+ end
70
+
71
+ if block_given?
72
+ name = "#{name}/layout" unless name.index('/')
73
+ Lux::Template.new(name, ivh).render_part { yield }
74
+ else
75
+ Lux::Template.new(name, ivh).render_part
76
+ end
77
+ end
78
+
79
+ def cache *args, &block
80
+ ttl = args.last.class == Hash ? args.pop[:ttl] : nil
81
+ key = 'view:'+Lux.cache.generate_key(args)+block.source_location.join(':')
82
+ Lux.cache.fetch(key, ttl) { yield }
83
+ end
84
+
85
+ def error msg
86
+ %[<pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:14pt; font-size:10pt;">#{msg}</pre>]
87
+ end
88
+
89
+ # = render 'main/tag_list', klass:Link
90
+ # = cell Main::PartsCell, :tag_filter, klass
91
+ def cell name, action, *args
92
+ klass = name.is_string? ? "#{name}_cell".classify.constantize : name
93
+ klass.cell(action, *args) rescue error("#{klass}.#{action}()\n\n#{$!.message}")
94
+ end
95
+
96
+ # add locale prefix if using locale
97
+ def localize path
98
+ return Lux.config(:localize) ? "/#{Locale.current}#{path}" : path
99
+ end
100
+
101
+ # tag :div, { 'class'=>'iform' } do
102
+ def tag name, opts={}, data=nil
103
+ data = block_given? ? yield(opts) : data
104
+ data = data.join('') if data.is_a?(Array)
105
+ opts.tag name, data
106
+ end
107
+
108
+ end
109
+
@@ -0,0 +1,3 @@
1
+ module Lux::Html
2
+
3
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ # = form Lux.page.var.user do |f|
4
+ # = f.hidden :company_id
5
+ # = f.row :name
6
+ # = f.row :counrty_id, :as=>:country
7
+ # .custom= f.input :email
8
+ # = f.submit 'Save'
9
+
10
+ class Lux::Html::Form
11
+ def initialize(action, form_opts={})
12
+ form_opts[:method] = 'get' if form_opts.delete(:get)
13
+ form_opts[:method] ||= 'get' unless action
14
+ form_opts[:method] ||= 'post'
15
+ form_opts[:method] = form_opts[:method].upcase
16
+
17
+ @action = action
18
+ @object = action if action.respond_to?(:update)
19
+ @form_opts = form_opts
20
+ end
21
+
22
+ def encrypt data
23
+ return unless data
24
+ '[protected]:%s' % Crypt.encrypt(data)
25
+ end
26
+
27
+ def hidden(name, opts={})
28
+ fname = @object.class.name.tableize.singularize rescue nil
29
+
30
+ if name.respond_to?(:update)
31
+ oname = name.class.name.tableize.singularize
32
+ if @object && @object.respond_to?("#{oname}_id") # grp
33
+ Lux::Page::EncryptParams.hidden_input "#{fname}[#{oname}_id]", name.id
34
+ else
35
+ [
36
+ Lux::Page::EncryptParams.hidden_input("#{fname}[model_id]", name.id),
37
+ Lux::Page::EncryptParams.hidden_input("#{fname}[model_type]", name.class.name),
38
+ ].join('')
39
+ end
40
+ else
41
+ name = '%s[%s]' % [fname, name] if fname && name.is_a?(Symbol)
42
+ Lux::Page::EncryptParams.hidden_input(name, opts[:value])
43
+ end
44
+ end
45
+
46
+ def input(name, opts={})
47
+ @name = name
48
+ opts[:id] ||= Lux.uid
49
+ opts[:value] ||= Lux.page.params[name] if @form_opts[:method] == 'GET'
50
+ input_object = Lux::Html::Input.new(@object)
51
+ data = input_object.render(name, opts)
52
+ @type = input_object.type
53
+ data
54
+ end
55
+
56
+ def data=(body)
57
+ @data = body
58
+ end
59
+
60
+ def render
61
+ render_form
62
+ @form_opts.tag(:form, @data)
63
+ end
64
+
65
+ def render_form
66
+ @data = %[<ul>#{@data}</ul>]
67
+ @form_opts[:class] = 'custom-class'
68
+ end
69
+
70
+ def submit(name=nil)
71
+ name ||= 'Submit'
72
+ %[<button>#{name}</button>]
73
+ end
74
+
75
+ def row(name, opts={})
76
+ node = input(name, opts)
77
+ label = %[<label for="#{opts[:id]}">#{opts[:label] || name.to_s.humanize}</label>]
78
+ %[<p class="as-#{opts[:as]}">#{label}#{node}<span class="error"></span></p>]
79
+ end
80
+ end
81
+
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # input = Lux::Html::Input.new(User.first)
4
+ # input.string :email
5
+
6
+ class Lux::Html::Input
7
+ attr_accessor :type
8
+
9
+ def initialize(obj=nil, opts={})
10
+ @object = obj
11
+ @globals = opts.dup
12
+ end
13
+
14
+ # exports @name and @opts globals
15
+ def opts_prepare(name, opts={})
16
+ unless opts[:as]
17
+ data_type = @object[name].class.name rescue 'String'
18
+ opts[:as] = :checkbox if ['TrueClass','FalseClass'].index(data_type)
19
+ end
20
+
21
+ # experimental, figure out collection unless defined
22
+ if name =~ /_id$/ && opts[:as] == :select && !opts[:collection]
23
+ class_name = name.to_s.split('_id')[0].capitalize
24
+ opts[:collection] = eval "#{class_name}.order('name').all"
25
+ end
26
+
27
+ opts[:as] ||= :select if opts[:collection]
28
+ opts[:id] ||= Lux.uid
29
+ opts[:value] ||= @object.send(name) if @object
30
+ opts[:name] = name.kind_of?(Symbol) && @object ? "#{@object.class.name.underscore}[#{name}]" : name
31
+
32
+ # convert decimal numbers to float
33
+ opts[:value] = opts[:value].to_f if opts[:value].class == BigDecimal
34
+
35
+ @label = opts.delete :label
36
+ @wrap = opts.delete(:wrap) || @globals[:wrap]
37
+ @name = name
38
+ @opts = opts
39
+ end
40
+
41
+ # if type is written in parameter :as=> use this helper function
42
+ def render(name, opts={})
43
+ if name.is_hash?
44
+ opts = name
45
+ name = :null
46
+ end
47
+
48
+ opts = opts_prepare name, opts.dup
49
+ @type = opts.delete(:as) || :text
50
+ send("as_#{@type}") rescue Lux::Error.inline("as_#{@type}")
51
+ end
52
+
53
+ def prepare_collection(data)
54
+ ret = []
55
+ for el in data
56
+ if data[0].respond_to?(:select_name)
57
+ ret.push [el.id.to_s, el.select_name]
58
+ elsif data[0].respond_to?(:name)
59
+ ret.push [el.id.to_s, el.name]
60
+ elsif data[0].kind_of?(Array)
61
+ ret.push [el[0].to_s, el[1]]
62
+ elsif data.is_hash?
63
+ ret.push el
64
+ else
65
+ ret.push [el.to_s, el]
66
+ end
67
+ end
68
+ ret
69
+ end
70
+
71
+ end
@@ -0,0 +1,277 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Lux::Html::Input
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
+ comp_style = val.split(/\n/).length + val.length/100
40
+ comp_style = 4 if comp_style < 4
41
+ comp_style = 15 if comp_style > 15
42
+ @opts[:style] = "height:#{comp_style*20}px; #{@opts[:style]};"
43
+ @opts.tag(:textarea, val)
44
+ end
45
+ alias :as_memo :as_textarea
46
+
47
+ def as_checkbox
48
+ id = Lux.uid
49
+ hidden = { :name=>@opts.delete(:name), :type=>:hidden, :value=>@opts[:value] ? 1 : 0, :id=>id }
50
+ @opts[:type] = :checkbox
51
+ @opts[:onclick] = "document.getElementById('#{id}').value=this.checked ? 1 : 0; #{@opts[:onclick]}"
52
+ @opts[:checked] = @opts.delete(:value) ? 1 : nil
53
+ @opts.tag(:input)+hidden.tag(:input)
54
+ end
55
+
56
+ def as_radio
57
+ @opts[:type] = :radio
58
+ @opts[:checked] = @opts[:value] == @object.send(@name) ? true : nil
59
+ @opts.tag(:input)
60
+ end
61
+
62
+ def as_select
63
+ body = []
64
+ collection = @opts.delete(:collection)
65
+ if nullval = @opts.delete(:null)
66
+ body.push %[<option value="">#{nullval}</option>] if nullval
67
+ end
68
+ for el in prepare_collection(collection)
69
+ body.push(%[<option value="#{el[0]}"#{@opts[:value].to_s == el[0].to_s ? ' selected=""' : nil}>#{el[1]}</option>])
70
+ end
71
+ body = body.join("\n")
72
+ @opts.tag(:select, body)
73
+ end
74
+
75
+ def as_tag
76
+ @opts[:value] = @opts[:value].or([]).join(', ') if ['Array', 'Sequel::Postgres::PGArray'].index(@opts[:value].class.name)
77
+ @opts[:id] ||= Lux.uid
78
+ @opts[:type] = :text
79
+ @opts[:onkeyup] = %[draw_tag('#{@opts[:id]}')]
80
+ @opts[:autocomplete] ||= 'off'
81
+ ret = %[
82
+ <script>
83
+ window.draw_tag = window.draw_tag || function (id) {
84
+ tags = $.map($('#'+id).val().split(/\s*,\s*/), function(el) {
85
+ val = el.replace(/\s+/,'-');
86
+ return val ? '<span class="label label-default">'+val+'</span> ' : ''
87
+ });
88
+ $('#'+id+'_tags').html(tags)
89
+ }</script>]
90
+ ret += @opts.tag(:input)
91
+ ret += %[<div id="#{@opts[:id]}_tags" style="margin-top:5px;"></div>]
92
+ ret += %[<script>if (window.$) { draw_tag('#{@opts[:id]}'); } else { window.onload = function(){ draw_tag('#{@opts[:id]}'); } }</script>]
93
+ ret
94
+ end
95
+
96
+ def as_date
97
+ @opts[:type] = 'text'
98
+ @opts[:style] = 'width: 120px; display: inline;'
99
+ @opts[:value] = @opts[:value].to_s.split(' +').first.to_s.sub(/:\d{2}$/,'')
100
+ @opts[:hint] ||= 'YEAR - MONTH - DAY'
101
+ ret = @opts.tag(:input)
102
+ ret += ' &bull; %s' % Time.ago(Time.parse(@opts[:value])) if @opts[:value].present?
103
+ ret += ' &bull; <small>%s</small>' % @opts[:hint]
104
+ ret + %[<script>new Pikaday({ field: document.getElementById('#{@opts [:id]}'), format: "YYYY-MM-DD" }); </script>]
105
+ end
106
+
107
+ def as_datetime
108
+ @opts[:type] = 'text'
109
+ @opts[:style] ||= 'width: 170px;'
110
+ @opts[:value] = @opts[:value].to_s.split(' +').first.sub(/:\d{2}$/,'') if @opts[:value].present?
111
+ ret = @opts.tag(:input)
112
+ hint = []
113
+ hint.push Time.ago(Time.parse(@opts[:value]))+'. ' if @opts[:value].present?
114
+ hint.push %[<span class="btn btn-xs" onclick="$('##{@opts[:id]}').val('#{(Time.now-1.minute).to_s.split(' +').first.sub(/:\d{2}$/,'')}')">Now!</span>]
115
+ ret + {class:'hint'}.tag(:p, hint.join(''))
116
+ end
117
+
118
+ def as_datebuttons
119
+ @opts[:type] = 'text'
120
+ @opts[:style] = 'width:100px; display:inline;'
121
+ @opts[:id] = "date_#{Lux.uid}"
122
+ id = "##{@opts[:id]}"
123
+ ret = @opts.tag(:input)
124
+ ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{DateTime.now.strftime('%Y-%m-%d')}'); return false;">Today</button>]
125
+ for el in [1, 3, 7, 14, 30]
126
+ date = DateTime.now+el.days
127
+ name = el.to_s
128
+ name += " (#{date.strftime('%a')})" if el < 7
129
+ ret += %[ <button class="btn btn-default btn-sm" onclick="$('#{id}').val('#{(DateTime.now+el.days).strftime('%Y-%m-%d')}'); return false;">+#{name}</button>]
130
+ end
131
+ ret
132
+ end
133
+
134
+ def as_user
135
+ button_text = if @opts[:value].to_i > 0
136
+ usr = User.find(@opts[:value])
137
+ "#{usr.name} (#{usr.email})"
138
+ else
139
+ 'Select user'
140
+ end
141
+
142
+ @opts[:style] = "width:auto;#{@opts[:style]};"
143
+ @opts[:onclick] = %[Popup.render(this,'Select user', '/part/users/single_user?product_id=#{@object.bucket_id}');return false;]
144
+ @opts.tag :button, button_text
145
+ end
146
+
147
+ def as_photo
148
+ @opts[:type] = 'hidden'
149
+ if @opts[:value].present?
150
+ img = Photo.find(@opts[:value])
151
+ @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>]
152
+ end
153
+ picker = @opts.tag(:input)
154
+ %[<span class="btn btn-default" onclick="Photo.pick('#{@opts[:id]}', function(id) { alert('Chosen: '+id) })">Select photo</span>#{picker}#{@image}]
155
+ end
156
+
157
+ def as_photos
158
+ @opts[:type] = 'text'
159
+ @opts[:style] = 'width:150px; display:inline;'
160
+ @opts[:class] += ' mutiple'
161
+ @images = []
162
+ if @opts[:value].present?
163
+ for el in @opts[:value].split(',').uniq
164
+ img = Photo.find(el.to_i) rescue next
165
+ @images.push %[ <img style="height:34px; cursor:pointer;" src="#{img.thumbnail}" onclick="window.open('#{img.image.remote_url}')" />]
166
+ end
167
+ end
168
+ picker = @opts.tag(:input)
169
+ %[<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>]
170
+ end
171
+
172
+ def as_admin_password
173
+ @opts[:type] = 'text'
174
+ @opts[:style] = 'display:none;'
175
+ @opts[:value] = ''
176
+ ret = @opts.tag(:input)
177
+ %[<span class="btn btn-default" onclick="$(this).hide();$('##{@opts[:id]}').show().val('').attr('type','password').focus()">Set pass</span> #{ret}]
178
+ end
179
+
180
+ def as_color
181
+ value = @opts[:value]
182
+ @opts[:style] ||= 'width:150px; float: left; margin-right: 10px;'
183
+ as_text + %[<span style="background-color: #{value.or('#fff')}; height:34px; width:150px; display: inline-block;"></span>]
184
+ end
185
+
186
+ def as_array_values
187
+ name = @opts[:name]
188
+ ret = []
189
+ values = @opts[:value].kind_of?(String) ? @opts[:value].split(',') : @opts[:value]
190
+ for el in @opts[:collection]
191
+ ret.push %[<label style="position:relative; top:4px;">
192
+ <input name="#{name}[#{el[1]}]" value="1" type="checkbox" #{values[el[1]].present? ? 'checked=""' : ''} style="position:relative;top:2px; left:2px;" />
193
+ <span style="margin-right:10px;">#{el[0]}</span>
194
+ </label>]
195
+ end
196
+ ret.join('')
197
+ end
198
+
199
+ def as_pass
200
+ value = @opts[:value]
201
+ id = @opts[:id]
202
+ @opts[:value] = ''
203
+ @opts[:style] = 'width:200px; display:inline;'
204
+ ret = @opts.tag(:input)
205
+ %[<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>]
206
+ end
207
+
208
+ def as_image
209
+ @opts[:type] = 'text'
210
+ @opts[:autocomplete] ||= 'off'
211
+ @opts[:style] = 'width:350px; float:left;'
212
+ input = @opts.tag(:input)
213
+ path_name = defined?(Storage) ? :storages : :images
214
+ input += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick="Popup.template('#{path_name}/select', function(url){ $('##{@opts[:id]}').val(url); Popup.close(); })">upload</span>]
215
+ if @opts[:value].present?
216
+ 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}]
217
+ input += %[<br /><br /><span id="img_size_#{@opts[:id]}">...</span>]
218
+ end
219
+
220
+ input
221
+ end
222
+
223
+ def as_geo
224
+ @opts[:type] = 'text'
225
+ @opts[:style] = 'width:200px; float:left;'
226
+ ret = @opts.tag(:input)
227
+ 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>]
228
+ end
229
+
230
+ def as_html_trix
231
+ %[<textarea id="trix_#{@name}" name="#{@opts[:name]}" style="display:none;">#{@opts[:value]}</textarea>
232
+ <trix-editor input="trix_#{@name}"></trix-editor>]
233
+ end
234
+
235
+ def as_button_select
236
+ body = ['<div class="btn-group">']
237
+ collection = @opts.delete(:collection)
238
+ for el in prepare_collection(collection)
239
+ opts = { class:'btn btn-sm' }
240
+ ap "#{@opts[:name]} - #{@opts[:value]} == #{el[0]}"
241
+ opts[:class] += ' btn-primary' if @opts[:value].to_s == el[0].to_s
242
+ opts[:onclick] = "$(this).parent().find('.btn').removeClass('btn-primary'); $(this).addClass('btn-primary').blur(); $(this).addClass('btn-primary').blur();"
243
+ opts[:onclick] += @opts[:onclick] ? "(#{@opts[:onclick].sub(/;\s*$/,'')})('#{el[0]}')" : "$('##{@opts[:id]}').val('#{el[0]}')"
244
+ opts[:onclick] += '; return false;'
245
+ body.push opts.tag(:button, el[1])
246
+ end
247
+ body.push '</div>'
248
+ body.push @opts.pluck(:name, :id, :value).merge(type: :hidden).tag(:input)
249
+ body.join('')
250
+ end
251
+
252
+ def as_address
253
+ val = @opts[:value]
254
+ @opts[:style] ||= 'height:55px; width:250px; float: left; margin-right:5px;'
255
+ ret = as_textarea
256
+ 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
257
+ ret
258
+ end
259
+
260
+ def as_images
261
+ path_name = defined?(Storage) ? :storages : :images
262
+
263
+ val = @opts[:value].to_s
264
+ ret = as_memo
265
+ ret += '<div style="margin-top: 7px;"></div>'
266
+ 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(' ')
267
+ ret += %[<span class="btn btn-default" style="float:left; margin-left:5px;" onclick='Popup.template("#{path_name}/select", function(url){ $("##{@opts[:id]}")[0].value += "\\n"+url; Popup.close(); })'>add image</span>]
268
+ ret
269
+ end
270
+
271
+ def as_disabled
272
+ @opts[:disabled] = true
273
+ @opts.delete(:name)
274
+ as_text
275
+ end
276
+
277
+ end