common_core_js 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3c68ef2ddc0f4e98ca5b54d0c646a918b2f70d091fe89db9951a61879af34d2
4
- data.tar.gz: 5ea645a245a197d42dabf6f697a7f94e5227d6c8416da47d8dfe875fdc28cbea
3
+ metadata.gz: e7e5ec56e4fffb8aab8840c9cc1aff9377d1291bd9b82fd91fe57841089d8b9e
4
+ data.tar.gz: c688ccfbca5218fdc210ee199b87e5b61f32405179f41268d2776b086e22e58a
5
5
  SHA512:
6
- metadata.gz: 7490de5c8f29a6a80960c50ecbb37a02122cafcc96b345c200f1cdc4cb3cf5f50f15e2fb4609f6f4a920b814c9fb123b9024a8d226a1e0f225bebe5246fe9cbb
7
- data.tar.gz: fccfc40f5851a9a0bffdc5410085073afa8798320b4abf7a770c1dd7e3070ff871e341ba613f67b7afed11ea2a69c9c685dd130b41bc301ca89386490c5dd77a
6
+ metadata.gz: 69bdb23d11a350e5f31b160840dc1f96abf8706a3716df9250e992e1e8cacbfd4b7d10d17be2103fc3f38c1064b178eec7cd2a445ead0c1c5e7269a59188560d
7
+ data.tar.gz: 29d5aea167e7cfaba9861758a72c8ecc16883ec1b70d11294e45e4ef89377d5654c12a0dcfbcac4c0d3a1b8abdace463e68fc9ca39f9aedfccbc20b18e470f0a
data/.gitignore CHANGED
@@ -11,4 +11,6 @@ test/dummy/tmp/
11
11
 
12
12
  .idea/
13
13
  *.gem
14
- .DS_Store
14
+ .DS_Store
15
+
16
+ dump.rdb
@@ -1,4 +1,80 @@
1
1
  module CommonCoreJs
2
2
  module ApplicationHelper
3
+
4
+ def datetime_field_localized(form_object, field_name, value, label, timezone = nil )
5
+ res = form_object.label(label,
6
+ field_name,
7
+ class: 'small form-text text-muted')
8
+
9
+ res << form_object.text_field(field_name, class: 'form-control',
10
+ type: 'datetime-local',
11
+ value: date_to_current_timezone(value, timezone))
12
+
13
+ res << human_timezone(Time.now, timezone)
14
+ res
15
+ end
16
+
17
+
18
+ def date_field_localized(form_object, field_name, value, label, timezone = nil )
19
+ res = form_object.label(label,
20
+ field_name,
21
+ class: 'small form-text text-muted')
22
+
23
+ res << form_object.text_field(field_name, class: 'form-control',
24
+ type: 'date',
25
+ value: date_to_current_timezone(value, timezone))
26
+
27
+ res
28
+ end
29
+
30
+ def time_field_localized(form_object, field_name, value, label, timezone = nil )
31
+ res = form_object.label(label,
32
+ field_name,
33
+ class: 'small form-text text-muted')
34
+
35
+ res << form_object.text_field(field_name, class: 'form-control',
36
+ type: 'time',
37
+ value: date_to_current_timezone(value, timezone))
38
+
39
+ res << human_timezone(Time.now, timezone)
40
+ res
41
+ end
42
+
43
+ def current_timezone
44
+ if method(:current_user)
45
+ if current_user.try(:timezone)
46
+ Time.now.in_time_zone(current_user.timezone).strftime("%z").to_i/100
47
+ else
48
+ Time.now.strftime("%z").to_i/100
49
+ end
50
+ else
51
+ raise "no method current_user is available or it does not implement timezone; please implement/override the method current_timezone"
52
+ end
53
+ end
54
+
55
+ def human_timezone(time_string, timezone)
56
+ time = time_string.in_time_zone(timezone)
57
+
58
+ if time.zone.match?(/^\w/)
59
+ time.zone
60
+ else
61
+ time.formatted_offset
62
+ end
63
+ end
64
+
65
+ def date_to_current_timezone(date, timezone = nil)
66
+ # if the timezone is nil, use the server date'
67
+ if timezone.nil?
68
+ timezone = Time.now.strftime("%z").to_i/100
69
+ end
70
+
71
+ return nil if date.nil?
72
+
73
+ begin
74
+ return date.in_time_zone(timezone).strftime("%Y-%m-%dT%H:%M")
75
+ rescue
76
+ return nil
77
+ end
78
+ end
3
79
  end
4
80
  end
@@ -8,7 +8,7 @@
8
8
 
9
9
  <% if object.errors.any? %>
10
10
  $(".flash-notices").html("<%= j render 'layouts/flash_notices' %>");
11
- $("<%= (scope + " ") if controller.common_scope %> .new-<%=singular%>-form").html("<%= j render(partial: '#{controller.namespace}errors', locals: {resource: object}) %>")
11
+ $("<%= (scope + " ") if controller.common_scope %> .new-<%=singular%>-form").html('<%= j render(partial: "#{controller.namespace}errors", locals: {resource: object}) %>')
12
12
  $("<%= (scope + " ") if controller.common_scope %> .new-<%=singular%>-form").append("<%= j render(partial: "new", locals: { singular.to_sym => object}) %><i class='fa fa-times-circle fa-2x' data-name='<%=singular%>' data-role='close-button' />").slideDown();
13
13
  <% else %>
14
14
  $("<%= (scope + " ") if controller.common_scope %> .new-<%=singular%>-form").slideUp();
@@ -6,4 +6,24 @@ require 'haml-rails'
6
6
 
7
7
  module CommonCoreJs
8
8
  # Your code goes here...
9
+ #
10
+ module ControllerHelpers
11
+ def modify_date_inputs_on_params(modified_params, authenticated_user = nil)
12
+ use_timezone = authenticated_user.timezone || Time.now.strftime("%z")
13
+
14
+ modified_params = modified_params.tap do |params|
15
+ params.keys.each{|k|
16
+ if k.ends_with?("_at") || k.ends_with?("_date")
17
+
18
+ begin
19
+ params[k] = DateTime.strptime("#{params[k]} #{use_timezone}", '%Y-%m-%dT%H:%M %z')
20
+ rescue StandardError
21
+
22
+ end
23
+ end
24
+ }
25
+ end
26
+ modified_params
27
+ end
28
+ end
9
29
  end
@@ -1,5 +1,7 @@
1
1
  module CommonCoreJs
2
2
  class Engine < ::Rails::Engine
3
3
  # isolate_namespace CommonCoreJs
4
+
5
+
4
6
  end
5
7
  end
@@ -1,3 +1,3 @@
1
1
  module CommonCoreJs
2
- VERSION = '0.3.2'
2
+ VERSION = '0.3.3'
3
3
  end
@@ -10,12 +10,11 @@ module CommonCore
10
10
 
11
11
  def initialize(*args) #:nodoc:
12
12
  super
13
-
14
- copy_file "common_core.js", "vendor/assets/javascripts/common_core.js"
15
- copy_file "common_core.scss", "vendor/assets/stylesheets/common_core.scss"
13
+ copy_file "common_core.js", "app/javascript/common_core.js"
14
+ copy_file "common_core.scss", "app/assets/stylesheets/common_core.scss"
15
+ copy_file "_flash_notices.haml", "app/views/layouts/_flash_notices.haml"
16
16
 
17
17
  end
18
-
19
18
  end
20
19
  end
21
20
 
@@ -146,6 +146,8 @@ module CommonCore
146
146
  unless @no_specs
147
147
  template "controller_spec.rb", File.join("spec/controllers#{namespace_with_dash}", "#{plural}_controller_spec.rb")
148
148
  end
149
+
150
+ template "_errors.haml", File.join("app/views#{namespace_with_dash}", "_errors.haml")
149
151
  end
150
152
 
151
153
  def list_column_headings
@@ -396,7 +398,7 @@ module CommonCore
396
398
 
397
399
  ".row
398
400
  .form-group.col-md-4
399
- = f.collection_select(:#{col.to_s}, #{assoc_name.titleize}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} , class: 'form-control')
401
+ = f.collection_select(:#{col.to_s}, #{assoc_name.titleize}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control')
400
402
  %label.small.form-text.text-muted
401
403
  #{col.to_s.humanize}"
402
404
 
@@ -424,13 +426,21 @@ module CommonCore
424
426
  end
425
427
 
426
428
  when :datetime
427
- ".row
429
+ ".row
428
430
  .form-group.col-md-4
429
- = f.text_field :#{col.to_s}, value: @#{singular}.#{col.to_s}, class: 'form-control', type: 'datetime-local'
430
- %label.form-text
431
- #{col.to_s.humanize}\n"
432
- end
433
- }.join("\n")
431
+ = datetime_field_localized(f, :#{col.to_s}, @#{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth}.timezone)"
432
+ when :date
433
+ ".row
434
+ .form-group.col-md-4
435
+ = date_field_localized(f, :#{col.to_s}, @#{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth}.timezone)"
436
+ when :time
437
+ ".row
438
+ .form-group.col-md-4
439
+ = time_field_localized(f, :#{col.to_s}, @#{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth}.timezone)"
440
+
441
+ end
442
+
443
+ }.join("\n")
434
444
  return res
435
445
  end
436
446
 
@@ -458,8 +468,22 @@ module CommonCore
458
468
  exit
459
469
  end
460
470
 
471
+ if assoc.active_record.column_names.include?("name")
472
+ display_column = "name"
473
+ elsif assoc.active_record.column_names.include?("to_label")
474
+ display_column = "to_label"
475
+ elsif assoc.active_record.column_names.include?("full_name")
476
+ display_column = "full_name"
477
+ elsif assoc.active_record.column_names.include?("display_name")
478
+ display_column = "display_name"
479
+ elsif assoc.active_record.column_names.include?("email")
480
+ display_column = "email"
481
+ else
482
+ puts "cant find any column to use as label for #{assoc.name.to_s}; any of name, to_labe, full_name, display_name, or email"
483
+ end
484
+
461
485
  " %td
462
- = #{singular}.#{assoc.name.to_s}.to_label"
486
+ = #{singular}.#{assoc.name.to_s}.#{display_column}"
463
487
 
464
488
  else
465
489
  " %td
@@ -474,7 +498,27 @@ module CommonCore
474
498
  = #{singular}.#{col}"
475
499
  when :datetime
476
500
  " %td
477
- = #{singular}.#{col}"
501
+ - unless #{singular}.#{col}.nil?
502
+ = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + human_timezone(Time.now, current_timezone)
503
+ - else
504
+ %span.alert-danger
505
+ MISSING
506
+ "
507
+ when :date
508
+ " %td
509
+ - unless #{singular}.#{col}.nil?
510
+ = #{singular}.#{col}
511
+ - else
512
+ %span.alert-danger
513
+ "
514
+ when :time
515
+ " %td
516
+ - unless #{singular}.#{col}.nil?
517
+ = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + human_timezone(Time.now, current_timezone)
518
+ - else
519
+ %span.alert-danger
520
+ "
521
+
478
522
  end
479
523
  }.join("\n")
480
524
  return res
@@ -0,0 +1,5 @@
1
+ - if resource.errors.any?
2
+ #error_explanation
3
+ - resource.errors.full_messages.each do |message|
4
+ %div.alert.alert-danger
5
+ = message
@@ -0,0 +1,7 @@
1
+ - unless notice.nil?
2
+ %div.alert.alert-notice.alert-dismissible
3
+ = notice
4
+
5
+ - unless alert.nil?
6
+ %div.alert.alert-danger.alert-dismissible
7
+ = alert
@@ -2,15 +2,30 @@
2
2
  require("jquery")
3
3
  console.log("loading common core...")
4
4
 
5
- $(function(){
6
- // always pass csrf tokens on ajax calls
7
- $.ajaxSetup({
8
- headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') }
9
- });
10
- });
5
+ require("jstimezonedetect/dist/jstz.min")
6
+ var JSTZ = require('jstimezonedetect');
7
+
8
+
9
+
11
10
 
12
11
  $(document).on('turbolinks:load', function() {
12
+
13
13
  $(document).ready(() => {
14
+ var user_timezone = '';
15
+ try {user_timezone = JSTZ.determine().name();} catch(e){}
16
+ // always pass csrf tokens on ajax calls
17
+
18
+
19
+ // $.ajaxSetup({
20
+ // beforeSend: (xhr) => {
21
+ //
22
+ // xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
23
+ // xhr.setRequestHeader( 'X-User-Timezone', user_timezone)
24
+ //
25
+ //
26
+ // }
27
+ // })
28
+
14
29
  $("body").on("click", "[data-role='close-button']", (event) => {
15
30
  var form_name;
16
31
  form_name = $(event.currentTarget).data("name");
@@ -30,7 +45,7 @@ $(document).on('turbolinks:load', function() {
30
45
  $form.find("i").remove()
31
46
  }
32
47
  })
33
- })
48
+ });
34
49
  });
35
50
 
36
51
 
@@ -22,6 +22,7 @@ class <%= controller_class_name %> < ApplicationController
22
22
  def load_<%= singular_name %>
23
23
  @<%= singular_name %> = <%= object_scope %>.find(params[:id])
24
24
  end
25
+ include CommonCoreJs::ControllerHelpers
25
26
 
26
27
  def index
27
28
  @<%= plural_name %> = <%= object_scope %><% if model_has_strings? %>.where(<%=class_name %>.arel_table[:email].matches("%#{@__general_string}%"))<% end %>.page(params[:page])
@@ -40,14 +41,14 @@ class <%= controller_class_name %> < ApplicationController
40
41
  end
41
42
 
42
43
  def create
43
- @<%=singular_name %> = <%=class_name %>.create(<%=singular_name %>_params<% if !create_merge_params.empty? %>.merge!(<%= create_merge_params %>)<%end%>)
44
+ modified_params = modify_date_inputs_on_params(<%=singular_name %>_params.dup<% if !create_merge_params.empty? %>.merge!(<%= create_merge_params %><%end%> ), <%=@auth%>)
45
+ @<%=singular_name %> = <%=class_name %>.create(modified_params)
44
46
  respond_to do |format|
45
47
  if @<%= singular_name %>.save
46
- format.js
47
48
  else
48
49
  flash[:alert] = "Oops, your <%=singular_name %> could not be saved."
49
- format.js
50
50
  end
51
+ format.js
51
52
  end
52
53
  end
53
54
 
@@ -65,7 +66,7 @@ class <%= controller_class_name %> < ApplicationController
65
66
 
66
67
  def update
67
68
  respond_to do |format|
68
- if !@<%=singular_name %>.update(<%= singular %>_params)
69
+ if !@<%=singular_name %>.update(modify_date_inputs_on_params(<%= singular %>_params, <%=@auth%> ))
69
70
  flash[:alert] = "<%=singular_name.titlecase %> could not be saved"
70
71
  end
71
72
  format.js {}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: common_core_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Fleetwood-Boldt
@@ -143,7 +143,6 @@ files:
143
143
  - app/assets/stylesheets/common_core_js/common_core.scss
144
144
  - app/controllers/common_core_js/application_controller.rb
145
145
  - app/helpers/common_core_js/application_helper.rb
146
- - app/helpers/common_core_js/generator_helper.rb
147
146
  - app/jobs/common_core_js/application_job.rb
148
147
  - app/mailers/common_core_js/application_mailer.rb
149
148
  - app/models/common_core_js/application_record.rb
@@ -170,6 +169,8 @@ files:
170
169
  - lib/generators/common_core/install_generator.rb
171
170
  - lib/generators/common_core/scaffold_generator.rb
172
171
  - lib/generators/common_core/templates/_edit.haml
172
+ - lib/generators/common_core/templates/_errors.haml
173
+ - lib/generators/common_core/templates/_flash_notices.haml
173
174
  - lib/generators/common_core/templates/_form.haml
174
175
  - lib/generators/common_core/templates/_line.haml
175
176
  - lib/generators/common_core/templates/_list.haml