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
@@ -20,6 +20,31 @@ module RailsCom::ControllerHelper
20
20
  end
21
21
  end
22
22
 
23
+ def set_locale
24
+ if params[:locale]
25
+ session[:locale] = params[:locale]
26
+ end
27
+
28
+ I18n.locale = session[:locale] || I18n.default_locale
29
+ end
30
+
31
+ def detect_filter(filter)
32
+ callback = self.__callbacks[:process_action].find { |i| i.filter == filter.to_sym }
33
+ return false unless callback
34
+
35
+ _if = callback.instance_variable_get(:@if).map do |c|
36
+ c.call(self)
37
+ end
38
+
39
+ _unless = callback.instance_variable_get(:@unless).map do |c|
40
+ !c.call(self)
41
+ end
42
+
43
+ !(_if + _unless).uniq.include?(false)
44
+ end
45
+
23
46
  end
24
47
 
25
- ActionController::Base.include RailsCom::ControllerHelper
48
+ ActiveSupport.on_load :action_controller_base do
49
+ include RailsCom::ControllerHelper
50
+ end
@@ -0,0 +1,8 @@
1
+ module RailsCom::Controllers
2
+ extend self
3
+
4
+ def controller controller_path
5
+ (controller_path + '_controller').classify.constantize
6
+ end
7
+
8
+ end
@@ -1,7 +1,7 @@
1
1
  class NilClass
2
2
 
3
3
  def to_d
4
- to_s.to_d
4
+ BigDecimal(0)
5
5
  end
6
6
 
7
7
  end
@@ -13,15 +13,35 @@ module UidHelper
13
13
  str
14
14
  end
15
15
 
16
- def nsec_uuid(prefix = '')
16
+ def nsec_uuid(prefix = '', suffix = rand_string)
17
17
  time = Time.now
18
18
  str = time.to_i.to_s + time.nsec.to_s
19
- uuid str.to_i, prefix, rand_string
19
+ uuid str.to_i, prefix, suffix
20
20
  end
21
21
 
22
- def sec_uuid(prefix = '')
22
+ def sec_uuid(prefix = '', suffix = rand_string(2))
23
23
  time = Time.now
24
- uuid time.to_i, prefix, rand_string(2)
24
+ uuid time.to_i, prefix, suffix
25
+ end
26
+
27
+ def decode_uuid(uuid, prefix: true)
28
+ if prefix
29
+ str_arr = uuid.split('-')
30
+ str = str_arr[1..-1].join
31
+ else
32
+ str_arr = uuid.split('-')
33
+ str = str_arr.join
34
+ end
35
+
36
+ if str.size >= 12
37
+ str = str[0..11]
38
+ str.to_i(36).to_s
39
+ elsif str.size >= 6 && str.size < 12
40
+ str = str[0..5]
41
+ str.to_i(36).to_s
42
+ else
43
+ raise 'Can not parse the format string!'
44
+ end
25
45
  end
26
46
 
27
47
  def rand_string(len = 4)
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/erb/scaffold/scaffold_generator'
2
+
3
+ class Erb::Generators::ScaffoldGenerator
4
+ source_root File.expand_path('../../../templates/erb/scaffold', __FILE__)
5
+
6
+ undef available_views
7
+ private
8
+ def available_views
9
+ ['index', 'edit', 'show', 'new', '_form', '_search_form']
10
+ end
11
+
12
+ end
13
+
14
+
@@ -0,0 +1,22 @@
1
+ module RailsExt
2
+ module TemplateRenderer
3
+
4
+ # record where the view rendered from, main project or which engine
5
+ # used by view helper methods: js_load, css_load, js_ready
6
+ def render_template(template, layout_name = nil, locals = nil)
7
+ path = template.identifier
8
+
9
+ result = path.match /(?<=\/)[a-zA-Z0-9_-]+(?=\/app\/views)/
10
+ result = result.to_s.split('-').first.to_s + '/engine'
11
+
12
+ engine = result.classify.safe_constantize
13
+
14
+ @view.instance_variable_set(:@_rendered_from, engine.root) if engine
15
+
16
+ super
17
+ end
18
+
19
+ end
20
+ end
21
+
22
+ ActionView::TemplateRenderer.prepend RailsExt::TemplateRenderer
@@ -1,21 +1,25 @@
1
1
  module RailsCom::Routes
2
2
  extend self
3
3
 
4
+ def verbs(controller, action)
5
+ routes_wrapper.select { |i| i[:controller] == controller.to_s && i[:action] == action.to_s }.map { |i| i[:verb] }.uniq
6
+ end
7
+
8
+ def actions(controller)
9
+ routes_wrapper.select { |i| i[:controller] == controller.to_s }.map { |i| i[:action] }.uniq
10
+ end
11
+
4
12
  def routes_wrapper
5
- ary = []
13
+ return @routes_wrapper if @routes_wrapper.present?
6
14
 
15
+ @routes_wrapper = []
7
16
  routes.each do |route|
8
- ary << detail(route)
17
+ @routes_wrapper << detail(route)
9
18
  end
10
19
 
11
- ary
12
- end
13
-
14
- def actions(controller)
15
- routes_wrapper.select { |i| i[:controller] == controller.to_s }.map { |i| i[:action] }.uniq
20
+ @routes_wrapper
16
21
  end
17
22
 
18
- private
19
23
  def detail(route)
20
24
  wrap = ActionDispatch::Routing::RouteWrapper.new(route)
21
25
  info = route.defaults
@@ -8,24 +8,6 @@ module NonDigestAssets
8
8
  environment.paths.find_all { |i| i.include? 'nondigest_assets' }.each do |src|
9
9
  r_src = src.to_s + '/.'
10
10
  FileUtils.cp_r r_src, self.dir, verbose: true
11
-
12
- if Sprockets.config[:sync].to_s == 'qiniu'
13
- f_src = src.to_s + '/**/*'
14
- path_src = Pathname.new src
15
- Dir.glob(f_src).select { |f| File.file?(f) }.each do |file|
16
- key = Pathname.new(file).relative_path_from(path_src)
17
- QiniuHelper.upload file, 'assets/' + key.to_s
18
- end
19
- end
20
- end
21
- end
22
-
23
- def remove(filename)
24
- super
25
-
26
- if Sprockets.config[:sync].to_s == 'qiniu'
27
- QiniuHelper.delete 'assets/' + filename.to_s
28
- logger.info "--> Removed from Qiniu: #{ filename }"
29
11
  end
30
12
  end
31
13
 
@@ -1,3 +1,3 @@
1
1
  module RailsCom
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/rails_com.rb CHANGED
@@ -2,15 +2,24 @@ require 'rails_com/version'
2
2
 
3
3
  require 'rails_com/routes'
4
4
  require 'rails_com/models'
5
+ require 'rails_com/controllers'
5
6
  require 'rails_com/model_helper'
6
7
  require 'rails_com/controller_helper'
7
- require 'rails_com/sprockets'
8
+ require 'rails_com/sprockets/non_digest_assets'
8
9
 
9
10
  require 'rails_com/helpers/uid_helper'
10
11
  require 'rails_com/helpers/jobber'
11
- require 'rails_com/helpers/qiniu_helper'
12
12
 
13
13
  require 'rails_com/core_ext/hash'
14
14
  require 'rails_com/core_ext/nil'
15
15
 
16
+ require 'rails_com/rails_ext/template_renderer'
17
+ require 'rails_com/rails_ext/scaffold_generator'
18
+
16
19
  require 'rails_com/engine'
20
+
21
+ module RailsCom
22
+ mattr_accessor :not_found_logger
23
+
24
+ self.not_found_logger = ActiveSupport::Logger.new('log/not_found.log')
25
+ end
@@ -0,0 +1,12 @@
1
+ <%%= form_with model: @<%= model_resource_name %>, local: true do |f| %>
2
+ <%%= render 'shared/error_messages', target: @<%= singular_table_name %> %>
3
+ <% attributes.each do |attribute| -%>
4
+ <% if attribute.password_digest? -%>
5
+ <%%= f.password_field :password %>
6
+ <%%= f.password_field :password_confirmation %>
7
+ <% else -%>
8
+ <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %>
9
+ <% end -%>
10
+ <% end -%>
11
+ <%%= f.submit %>
12
+ <%% end %>
@@ -0,0 +1,7 @@
1
+ <%%= search_form_with css: { wrapper_all: 'inline field', wrapper_input: 'field', wrapper_submit: 'field', submit: 'ui green button'} do |f| %>
2
+ <div class="fields">
3
+ <%%= f.submit 'Search' %>
4
+ </div>
5
+ <%% end %>
6
+
7
+
@@ -0,0 +1,14 @@
1
+ <div>
2
+
3
+ <div class="ui segment breadcrumb">
4
+ <%%= link_to 'Back', <%= index_helper %>_path, class: 'section' %>
5
+ <div class="divider"> / </div>
6
+ <div class="active section">Edit</div>
7
+ </div>
8
+
9
+ <div class="ui segment">
10
+ <%%= render 'form' %>
11
+ </div>
12
+
13
+ </div>
14
+
@@ -0,0 +1,43 @@
1
+ <div>
2
+
3
+ <div class="ui top attached menu borderless">
4
+ <div class="header item"><%= plural_table_name.titleize %></div>
5
+ </div>
6
+
7
+ <div class="ui segment bottom attached">
8
+ <%%= render 'search_form' %>
9
+ </div>
10
+
11
+ <div class="ui segment top attached">
12
+ <%%= link_to 'New <%= singular_table_name.titleize %>', new_<%= singular_table_name %>_path, class: 'ui teal button' %>
13
+ </div>
14
+
15
+ <table class="ui bottom attached table">
16
+ <thead>
17
+ <tr>
18
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
19
+ <th><%= attribute.human_name %></th>
20
+ <% end -%>
21
+ </tr>
22
+ </thead>
23
+
24
+ <tbody>
25
+ <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
26
+ <tr>
27
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
28
+ <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
29
+ <% end -%>
30
+ <td>
31
+ <%%= link_to 'Show', <%= singular_table_name %>_path(<%= singular_table_name %>), class: 'ui blue label' %>
32
+ <%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: 'ui pink label' %>
33
+ <%%= link_to 'Destroy', <%= singular_table_name %>_path(<%= singular_table_name %>), method: :delete, data: { confirm: 'Are you sure?' }, class: 'ui red label' %>
34
+ </td>
35
+ </tr>
36
+ <%% end %>
37
+ </tbody>
38
+ </table>
39
+
40
+ <%%= paginate @<%= plural_table_name %> %>
41
+ </div>
42
+
43
+
@@ -0,0 +1,13 @@
1
+ <div>
2
+
3
+ <div class="ui segment breadcrumb">
4
+ <%%= link_to 'Back', <%= index_helper %>_path, class: 'section' %>
5
+ <div class="divider"> / </div>
6
+ <div class="active section">Add</div>
7
+ </div>
8
+
9
+ <div class="ui segment">
10
+ <%%= render 'form' %>
11
+ </div>
12
+
13
+ </div>
@@ -0,0 +1,7 @@
1
+ <% attributes.reject(&:password_digest?).each do |attribute| -%>
2
+ <p>
3
+ <strong><%= attribute.human_name %>:</strong>
4
+ <%%= @<%= singular_table_name %>.<%= attribute.name %> %>
5
+ </p>
6
+
7
+ <% end -%>
@@ -0,0 +1,53 @@
1
+ <% if namespaced? -%>
2
+ require_dependency '<%= namespaced_path %>/application_controller'
3
+ <% end -%>
4
+ class <%= controller_class_name %>Controller < <%= controller_class_path.map(&:camelize).join('::') %>::BaseController
5
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
6
+
7
+ def index
8
+ @<%= plural_table_name %> = <%= orm_class %>.page(params[:page])
9
+ end
10
+
11
+ def new
12
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
13
+ end
14
+
15
+ def create
16
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
17
+
18
+ if @<%= orm_instance.save %>
19
+ redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully created.'" %>
20
+ else
21
+ render :new
22
+ end
23
+ end
24
+
25
+ def show
26
+ end
27
+
28
+ def edit
29
+ end
30
+
31
+ def update
32
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
33
+ redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully updated.'" %>
34
+ else
35
+ render :edit
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ @<%= orm_instance.destroy %>
41
+ redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
42
+ end
43
+
44
+ private
45
+ def set_<%= singular_table_name %>
46
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
47
+ end
48
+
49
+ def <%= "#{singular_table_name}_params" %>
50
+ params.fetch(:<%= singular_table_name %>, {}).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
51
+ end
52
+
53
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_com
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - qinmingyuan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-05 00:00:00.000000000 Z
11
+ date: 2018-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Description of RailsCom.
41
+ description: Rails Engine with many common utils
42
42
  email:
43
43
  - mingyuan0715@foxmail.com
44
44
  executables: []
@@ -51,7 +51,11 @@ files:
51
51
  - app/assets/config/rails_com_manifest.js
52
52
  - app/assets/images/verification.jpg
53
53
  - app/assets/javascripts/rails_com/application.js
54
+ - app/assets/javascripts/rails_com/common.js
55
+ - app/assets/javascripts/rails_com/fetch_xhr_script.js
56
+ - app/assets/javascripts/rails_com/footer.js
54
57
  - app/assets/stylesheets/rails_com/application.css
58
+ - app/controllers/common_controller.rb
55
59
  - app/controllers/concerns/the_common_api.rb
56
60
  - app/controllers/the_guards_controller.rb
57
61
  - app/helpers/rails_com/active_helper.rb
@@ -67,44 +71,49 @@ files:
67
71
  - app/views/layouts/rails_com/application.html.erb
68
72
  - app/views/shared/_alert.html.erb
69
73
  - app/views/shared/_error_messages.html.erb
74
+ - app/views/shared/_locales.html.erb
70
75
  - app/views/the_guards/index.html.erb
76
+ - config/initializers/time_format.rb
71
77
  - config/locales/en.yml
72
78
  - config/locales/zh.yml
73
79
  - config/routes.rb
74
80
  - lib/assets/javascripts/input-attachment.js
75
- - lib/assets/stylesheets/semantic.css
81
+ - lib/assets/javascripts/semantic.js
76
82
  - lib/generators/doc_model_generator.rb
77
83
  - lib/generators/doc_models_generator.rb
78
84
  - lib/generators/templates/model.erb
85
+ - lib/mina/puma.rb
86
+ - lib/mina/sidekiq.rb
79
87
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.eot
80
88
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.otf
81
89
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.svg
82
90
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.ttf
83
91
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.woff
84
92
  - lib/nondigest_assets/fonts/themes/default/assets/fonts/icons.woff2
85
- - lib/nondigest_assets/images/themes/default/assets/images/flags.png
93
+ - lib/nondigest_assets/fonts/themes/default/assets/images/flags.png
86
94
  - lib/rails_com.rb
87
95
  - lib/rails_com/controller_helper.rb
96
+ - lib/rails_com/controllers.rb
88
97
  - lib/rails_com/core_ext/hash.rb
89
98
  - lib/rails_com/core_ext/nil.rb
90
99
  - lib/rails_com/engine.rb
91
100
  - lib/rails_com/helpers/jobber.rb
92
- - lib/rails_com/helpers/qiniu_helper.rb
93
101
  - lib/rails_com/helpers/uid_helper.rb
94
102
  - lib/rails_com/model_helper.rb
95
103
  - lib/rails_com/models.rb
104
+ - lib/rails_com/rails_ext/scaffold_generator.rb
105
+ - lib/rails_com/rails_ext/template_renderer.rb
96
106
  - lib/rails_com/routes.rb
97
- - lib/rails_com/sprockets.rb
98
107
  - lib/rails_com/sprockets/non_digest_assets.rb
99
- - lib/rails_com/sprockets/qiniu_exporter.rb
100
108
  - lib/rails_com/version.rb
101
109
  - lib/tasks/rails_com_tasks.rake
102
- - lib/templates/erb/scaffold/_form.html.erb
103
- - lib/templates/erb/scaffold/edit.html.erb
104
- - lib/templates/erb/scaffold/index.html.erb
105
- - lib/templates/erb/scaffold/new.html.erb
106
- - lib/templates/erb/scaffold/show.html.erb
107
- - lib/templates/rails/scaffold_controller/controller.rb
110
+ - lib/templates/erb/scaffold/_form.html.erb.tt
111
+ - lib/templates/erb/scaffold/_search_form.html.erb.tt
112
+ - lib/templates/erb/scaffold/edit.html.erb.tt
113
+ - lib/templates/erb/scaffold/index.html.erb.tt
114
+ - lib/templates/erb/scaffold/new.html.erb.tt
115
+ - lib/templates/erb/scaffold/show.html.erb.tt
116
+ - lib/templates/rails/scaffold_controller/controller.rb.tt
108
117
  homepage: https://github.com/qinmingyuan/rails_com
109
118
  licenses:
110
119
  - MIT
@@ -125,8 +134,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
134
  version: '0'
126
135
  requirements: []
127
136
  rubyforge_project:
128
- rubygems_version: 2.6.11
137
+ rubygems_version: 2.7.6
129
138
  signing_key:
130
139
  specification_version: 4
131
- summary: Summary of RailsCom.
140
+ summary: Rails Engine with many common utils
132
141
  test_files: []