rails_com 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +11 -3
  3. data/app/assets/javascripts/rails_com/application.js +1 -1
  4. data/app/assets/javascripts/rails_com/common.js +32 -0
  5. data/app/assets/javascripts/rails_com/fetch_xhr_script.js +53 -0
  6. data/app/assets/javascripts/rails_com/footer.js +3 -0
  7. data/app/assets/stylesheets/rails_com/application.css +0 -1
  8. data/app/controllers/common_controller.rb +18 -0
  9. data/app/controllers/concerns/the_common_api.rb +15 -3
  10. data/app/helpers/rails_com/active_helper.rb +17 -6
  11. data/app/helpers/rails_com_helper.rb +6 -3
  12. data/app/models/state_machine.rb +5 -4
  13. data/app/views/shared/_alert.html.erb +1 -5
  14. data/app/views/shared/_error_messages.html.erb +1 -7
  15. data/app/views/shared/_locales.html.erb +8 -0
  16. data/config/initializers/time_format.rb +1 -0
  17. data/config/locales/en.yml +8 -6
  18. data/config/routes.rb +7 -0
  19. data/lib/assets/javascripts/semantic.js +11 -0
  20. data/lib/mina/puma.rb +63 -0
  21. data/lib/mina/sidekiq.rb +65 -0
  22. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.eot +0 -0
  23. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.otf +0 -0
  24. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.svg +946 -2670
  25. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.ttf +0 -0
  26. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.woff +0 -0
  27. data/lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.woff2 +0 -0
  28. data/lib/nondigest_assets/{images → fonts}/themes/default/assets/images/flags.png +0 -0
  29. data/lib/rails_com/controller_helper.rb +26 -1
  30. data/lib/rails_com/controllers.rb +8 -0
  31. data/lib/rails_com/core_ext/nil.rb +1 -1
  32. data/lib/rails_com/helpers/uid_helper.rb +24 -4
  33. data/lib/rails_com/rails_ext/scaffold_generator.rb +14 -0
  34. data/lib/rails_com/rails_ext/template_renderer.rb +22 -0
  35. data/lib/rails_com/routes.rb +12 -8
  36. data/lib/rails_com/sprockets/non_digest_assets.rb +0 -18
  37. data/lib/rails_com/version.rb +1 -1
  38. data/lib/rails_com.rb +11 -2
  39. data/lib/templates/erb/scaffold/_form.html.erb.tt +12 -0
  40. data/lib/templates/erb/scaffold/_search_form.html.erb.tt +7 -0
  41. data/lib/templates/erb/scaffold/edit.html.erb.tt +14 -0
  42. data/lib/templates/erb/scaffold/index.html.erb.tt +43 -0
  43. data/lib/templates/erb/scaffold/new.html.erb.tt +13 -0
  44. data/lib/templates/erb/scaffold/show.html.erb.tt +7 -0
  45. data/lib/templates/rails/scaffold_controller/controller.rb.tt +53 -0
  46. metadata +25 -16
  47. data/lib/assets/stylesheets/semantic.css +0 -19466
  48. data/lib/rails_com/helpers/qiniu_helper.rb +0 -84
  49. data/lib/rails_com/sprockets/qiniu_exporter.rb +0 -21
  50. data/lib/rails_com/sprockets.rb +0 -15
  51. data/lib/templates/erb/scaffold/_form.html.erb +0 -19
  52. data/lib/templates/erb/scaffold/edit.html.erb +0 -2
  53. data/lib/templates/erb/scaffold/index.html.erb +0 -36
  54. data/lib/templates/erb/scaffold/new.html.erb +0 -1
  55. data/lib/templates/erb/scaffold/show.html.erb +0 -13
  56. data/lib/templates/rails/scaffold_controller/controller.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 32b4755a8bebb55e2b76cf7706a5db5847faf3d3
4
- data.tar.gz: 353c60cc06071bb72a88ef9c146d2d3ec85369ed
2
+ SHA256:
3
+ metadata.gz: 91a9e381fa905dd733cdabed0016d6f6826b468e3d9a2d1af8eece1323d88d81
4
+ data.tar.gz: 95625b665362ccfea08fb4f9317a521e741adc0f765d0bef2996477aef0cc34c
5
5
  SHA512:
6
- metadata.gz: '0384054fc7e6a81ad573089fe6a4b1357dee92dd60325dc5ca0375fe20b024fd64e08d513394c9036968fcea65f047e0bb2b55929af22abb6503b3e508c69728'
7
- data.tar.gz: b92de35c6e6a552de2140bc4c6812b8098b733f61eb2dfef8df5f1a454a8b92e1dfcda849eb053c1dc68d447432755c79b75e3f3b2d23b63cce21c7330a2fa07
6
+ metadata.gz: 0ce5ae23bc46a4f2d667db8d8b21ee86b4fa8670a322074857d1cee11b0a6afd7c3e8c8cce0325cc15a6b65742c25dec7b486a48bb964e971df62f9d3b21734c
7
+ data.tar.gz: a6f1f3e942c26a68ad460f387146fe58304895fa8c46ac76c9f9053fcbf270ca4cd7288bb0cb5a6d4839275099e18ad1282fe517cfea796e34f52e508cb257c9
data/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  # RailsCom
2
2
  Short description and motivation.
3
3
 
4
- ## Usage
5
- How to use my plugin.
6
-
7
4
  ## Installation
8
5
  Add this line to your application's Gemfile:
9
6
 
@@ -20,6 +17,17 @@ Or install it yourself as:
20
17
  ```bash
21
18
  $ gem install rails_com
22
19
  ```
20
+ ## Usage
21
+ How to use my plugin.
22
+
23
+
24
+ ### View: add link
25
+ ```erb
26
+ <%= link_to locales_path, class: 'item', remote: true, id: 'locales_show' do %>
27
+ <i class="translate icon"></i>
28
+ <% end %>
29
+ ```
30
+
23
31
 
24
32
  ## Contributing
25
33
  Contribution directions go here.
@@ -1,3 +1,3 @@
1
1
  //= require rails-ujs
2
2
  //= require turbolinks
3
- //= require_self
3
+ //= require_self
@@ -0,0 +1,32 @@
1
+ function timeForLocalized(){
2
+ $('time[data-localized!="true"]').each(function(){
3
+ this.textContent = moment.utc(this.textContent).local().format('YYYY-MM-DD HH:mm:ss');
4
+ this.dataset['localized'] = 'true'
5
+ })
6
+ }
7
+
8
+ function clickCallback(e) {
9
+ if (e.target.tagName !== 'A') {
10
+ return;
11
+ }
12
+ (new Date).getTimezoneOffset();
13
+ }
14
+
15
+ //document.addEventListener('click', clickCallback, false);
16
+
17
+ document.addEventListener('DOMContentLoaded', function() {
18
+ timeForLocalized()
19
+ });
20
+
21
+ document.addEventListener('turbolinks:load', function() {
22
+ timeForLocalized()
23
+ });
24
+
25
+ document.addEventListener('turbolinks:request-start', function(event) {
26
+ var xhr = event.data.xhr;
27
+ var offset = (new Date).getTimezoneOffset();
28
+ xhr.setRequestHeader('Utc-Offset', offset);
29
+ });
30
+
31
+
32
+
@@ -0,0 +1,53 @@
1
+ function fetch_xhr_script(url, params){
2
+ var default_params = {
3
+ credentials: 'include',
4
+ headers: {
5
+ 'Accept': 'application/javascript',
6
+ 'X-CSRF-Token': document.head.querySelector("[name=csrf-token]").content
7
+ }
8
+ };
9
+
10
+ fetch(url, params).then(function(response) {
11
+ return response.text()
12
+ }).then(function(text) {
13
+ var script = document.createElement('script');
14
+ script.text = text;
15
+ document.head.appendChild(script).parentNode.removeChild(script);
16
+ }).catch(function(ex) {
17
+ console.log('parsing failed', ex)
18
+ })
19
+ }
20
+
21
+ function listenCheckedIds(name) {
22
+ var checked = 'input[name="' + name + '"]:checked';
23
+ var unchecked = 'input[name="' + name + '"][checked!="checked"]';
24
+
25
+ window.add_ids = [];
26
+ window.remove_ids = [];
27
+ $(checked).change(function(){
28
+ if(this.checked){
29
+ var index = window.remove_ids.indexOf(this.value);
30
+ window.remove_ids.splice(index, 1);
31
+ } else {
32
+ window.remove_ids.push(this.value)
33
+ }
34
+ });
35
+ $(unchecked).change(function(){
36
+ if(this.checked){
37
+ window.add_ids.push(this.value)
38
+ } else {
39
+ var index = window.add_ids.indexOf(this.value);
40
+ window.add_ids.splice(index, 1);
41
+ }
42
+ });
43
+ }
44
+
45
+ function getAddIds(){
46
+ add_str = window.add_ids.join(',');
47
+ return add_str;
48
+ }
49
+
50
+ function getRemoveIds(){
51
+ remove_str = window.remove_ids.join(',');
52
+ return remove_str
53
+ }
@@ -0,0 +1,3 @@
1
+ $('.message .close').on('click', function() {
2
+ $(this).closest('.message').fadeOut();
3
+ });
@@ -1,4 +1,3 @@
1
1
  /*
2
- *= require semantic
3
2
  *= require_self
4
3
  */
@@ -0,0 +1,18 @@
1
+ class CommonController < ApplicationController
2
+
3
+ def locales
4
+ respond_to do |format|
5
+ format.html
6
+ format.js
7
+ end
8
+ end
9
+
10
+ def not_found
11
+ params.permit!
12
+ RailsCom.not_found_logger.info "#{params[:path]}.#{params[:format]}"
13
+
14
+ head :not_found
15
+ end
16
+
17
+
18
+ end
@@ -5,9 +5,10 @@ module TheCommonApi
5
5
  rescue_from 'ActiveRecord::RecordNotFound' do |exp|
6
6
  render json: { error: exp.message, backtrace: exp.backtarce }, status: :not_found
7
7
  end
8
- rescue_from 'StandardError' do |exp|
9
- render json: { error: exp.message, backtrace: exp.backtrace }, status: 500
10
- end
8
+ # rescue_from 'StandardError' do |exp|
9
+ # render json: { error: exp.message, backtrace: exp.backtrace }, status: 500
10
+ # end
11
+ after_action :wrap_body
11
12
  end
12
13
 
13
14
  def process_errors(model)
@@ -17,4 +18,15 @@ module TheCommonApi
17
18
  }, status: 500
18
19
  end
19
20
 
21
+ def wrap_body
22
+ if self.response.media_type == 'application/json'
23
+ begin
24
+ body = JSON.parse self.response.body
25
+ rescue JSON::ParserError
26
+ body = {}
27
+ end
28
+ self.response.body = { data: body }.to_json
29
+ end
30
+ end
31
+
20
32
  end
@@ -9,6 +9,17 @@ module RailsCom::ActiveHelper
9
9
  end
10
10
  end
11
11
 
12
+ # active_asserts('active': true, expected: false)
13
+ def active_asserts(join: true, **options)
14
+ keys = options.select { |_, v| v }.keys
15
+
16
+ if join
17
+ keys.join(' ')
18
+ else
19
+ keys.last.to_s
20
+ end
21
+ end
22
+
12
23
  # path: active_helper paths: '/work/employees' or active_helper paths: ['/work/employees']
13
24
  # controller: active_helper controllers: 'xxx' or active_helper controllers: ['xxx1', 'admin/xxx2']
14
25
  # action: active_helper 'work/employee': ['index', 'show']
@@ -49,20 +60,20 @@ module RailsCom::ActiveHelper
49
60
  def filter_params(options = {})
50
61
  except = options.delete(:except)
51
62
  only = options.delete(:only)
52
- query = request.GET.dup
63
+ query = ActionController::Parameters.new(request.GET)
53
64
 
54
- if only.present?
55
- query.slice!(*only)
65
+ if only
66
+ query = query.permit(only)
56
67
  else
57
68
  excepts = []
58
- if except.is_a? Array
69
+ if except.is_a?(Array)
59
70
  excepts += except
60
- else
71
+ elsif except.present?
61
72
  excepts << except
62
73
  end
63
74
  excepts += ['commit', 'utf8', 'page']
64
75
 
65
- query.except!(*excepts)
76
+ query = query.permit!.except(*excepts)
66
77
  end
67
78
 
68
79
  query.merge!(options)
@@ -1,23 +1,26 @@
1
1
  module RailsComHelper
2
2
 
3
- def js_load(filename = nil, root: Rails.root, **options)
3
+ def js_load(filename = nil, root: nil, **options)
4
4
  filename ||= "controllers/#{controller_path}/#{action_name}"
5
+ root ||= @_rendered_from || Rails.root
5
6
  path = root + 'app/assets/javascripts' + filename.to_s
6
7
  if File.exist?(path.to_s + '.js') || File.exist?(path.to_s + '.js.erb')
7
8
  javascript_include_tag filename, options
8
9
  end
9
10
  end
10
11
 
11
- def css_load(filename = nil, root: Rails.root, **options)
12
+ def css_load(filename = nil, root: nil, **options)
12
13
  filename ||= "controllers/#{controller_path}/#{action_name}"
14
+ root ||= @_rendered_from || Rails.root
13
15
  path = root + 'app/assets/stylesheets' + filename.to_s
14
16
  if File.exist?(path.to_s + '.css')
15
17
  stylesheet_link_tag filename, options
16
18
  end
17
19
  end
18
20
 
19
- def js_ready(filename = nil, root: Rails.root, **options)
21
+ def js_ready(filename = nil, root: nil, **options)
20
22
  filename ||= "controllers/#{controller_path}/#{action_name}.ready"
23
+ root ||= @_rendered_from || Rails.root
21
24
  path = root + 'app/assets/javascripts' + filename.to_s
22
25
  if File.exist?(path.to_s + '.js') || File.exist?(path.to_s + '.js.erb')
23
26
  javascript_include_tag filename, options
@@ -9,13 +9,14 @@ module StateMachine
9
9
 
10
10
  options.each do |k, v|
11
11
  states = k.to_s.pluralize
12
- states = self.class.send(states).keys
12
+ states = self.class.send(states)
13
13
 
14
- i = states.find_index self.send(k)
15
- n = states[i+1]
14
+ i = states[self.send(k)]
15
+ n = states.key(i+1)
16
16
 
17
17
  if n == v.to_s
18
- update!(k => states[v])
18
+ assign_attributes(k => v)
19
+ save!
19
20
  else
20
21
  errors.add k, 'Next state is wrong'
21
22
  raise ActiveRecord::Rollback, 'Next state is wrong'
@@ -5,8 +5,4 @@
5
5
  </div>
6
6
  <% end %>
7
7
 
8
- <script>
9
- $('.message .close').on('click', function() {
10
- $(this).closest('.message').fadeOut();
11
- });
12
- </script>
8
+
@@ -8,10 +8,4 @@
8
8
  <% end %>
9
9
  </ul>
10
10
  </div>
11
- <% end %>
12
-
13
- <script>
14
- $('.message .close').on('click', function() {
15
- $(this).closest('.message').fadeOut();
16
- });
17
- </script>
11
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <div class="ui simple dropdown item">
2
+ <i class="translate icon"></i>
3
+ <%= I18n.locale %>
4
+ <div class="menu">
5
+ <%= link_to 'EN', { locale: 'en' }, class: 'item' %>
6
+ <%= link_to 'CN', { locale: 'zh' }, class: 'item' %>
7
+ </div>
8
+ </div>
@@ -0,0 +1 @@
1
+ Time::DATE_FORMATS[:admin] = '%Y-%m-%d %H:%M:%S'
@@ -15,9 +15,11 @@ en:
15
15
  other: "Displaying <b>all %{count}</b> %{entry_name}"
16
16
  more_pages:
17
17
  display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
18
- select:
19
- prompt: "Please select"
20
- submit:
21
- create: 'Create %{model}'
22
- update: 'Update %{model}'
23
- submit: 'Save %{model}'
18
+ select:
19
+ prompt: "Please select"
20
+ submit:
21
+ create: 'Creates %{model}'
22
+ update: 'Updates %{model}'
23
+ submit: 'Saves %{model}'
24
+ q:
25
+ submit: 'Search'
data/config/routes.rb CHANGED
@@ -1,5 +1,12 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
3
  resources :the_guards
4
+ controller :common do
5
+ get :locales
6
+ end
4
7
 
5
8
  end
9
+
10
+ Rails.application.routes.append do
11
+ match '*path' => 'common#not_found', via: :all
12
+ end