rails_com 1.0.0

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +28 -0
  4. data/Rakefile +37 -0
  5. data/app/assets/config/qiniu.js +23 -0
  6. data/app/assets/config/qiniu1.js +25 -0
  7. data/app/assets/config/rails_com_manifest.js +3 -0
  8. data/app/assets/javascripts/rails_com/application.js +0 -0
  9. data/app/helpers/rails_com/active_helper.rb +92 -0
  10. data/app/helpers/rails_com_helper.rb +20 -0
  11. data/app/models/state_machine.rb +26 -0
  12. data/app/views/kaminari/_first_page.html.erb +3 -0
  13. data/app/views/kaminari/_gap.html.erb +3 -0
  14. data/app/views/kaminari/_last_page.html.erb +3 -0
  15. data/app/views/kaminari/_next_page.html.erb +3 -0
  16. data/app/views/kaminari/_page.html.erb +1 -0
  17. data/app/views/kaminari/_paginator.html.erb +15 -0
  18. data/app/views/kaminari/_prev_page.html.erb +3 -0
  19. data/app/views/shared/_alert.html.erb +12 -0
  20. data/app/views/shared/_error_messages.html.erb +17 -0
  21. data/config/locales/en.yml +17 -0
  22. data/config/locales/zh.yml +17 -0
  23. data/config/routes.rb +2 -0
  24. data/lib/generators/doc_model_generator.rb +37 -0
  25. data/lib/generators/doc_models_generator.rb +11 -0
  26. data/lib/generators/templates/model.erb +6 -0
  27. data/lib/rails_com/core_ext/hash.rb +37 -0
  28. data/lib/rails_com/engine.rb +9 -0
  29. data/lib/rails_com/helpers/jobber.rb +70 -0
  30. data/lib/rails_com/helpers/qiniu_helper.rb +113 -0
  31. data/lib/rails_com/helpers/uid_helper.rb +31 -0
  32. data/lib/rails_com/model_helper.rb +54 -0
  33. data/lib/rails_com/models.rb +20 -0
  34. data/lib/rails_com/routes.rb +35 -0
  35. data/lib/rails_com/version.rb +3 -0
  36. data/lib/rails_com.rb +14 -0
  37. data/lib/tasks/rails_com_tasks.rake +4 -0
  38. metadata +94 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9884f6394c3e3e9d704e28b52b564eab818287d3
4
+ data.tar.gz: c0b5eb3790e0a67868a44715f2e9c36760ce33c7
5
+ SHA512:
6
+ metadata.gz: b45dd59723a7532cb40a8001928de34edd568d4d94b0e953012da16d4026ed89671eb6a07a4180366e98b77b101abd0ac427e751780fc81a3b79b385a57f7ce7
7
+ data.tar.gz: f58cc13c5ac95755231adc26f9c867e6a3dd24bf291ac4db390eaf52caa1178b3b0e620508b90b051e2c552bef02f75a875a9df44b2a8ebff9b92c40ab50fa00
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 qinmingyuan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RailsCom
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rails_com'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install rails_com
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RailsCom'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,23 @@
1
+ var uploader = Qiniu.uploader({
2
+ runtimes: 'html5,html4',
3
+ browse_button: 'avatar_image_key_button',
4
+ uptoken_url: '/uptoken',
5
+ unique_names: true,
6
+ save_key: false,
7
+ domain: '<%= Settings.qiniu.public.domain %>',
8
+ get_new_uptoken: false,
9
+ container: 'container',
10
+ max_file_size: '100mb',
11
+ max_retries: 3,
12
+ dragdrop: true,
13
+ drop_element: 'container',
14
+ chunk_size: '4mb',
15
+ auto_start: true,
16
+ init: {
17
+ 'FileUploaded': function(up, file, info) {
18
+ var res = $.parseJSON(info);
19
+ $('#avatar_image_key_id').val(res.key);
20
+ },
21
+ 'Error': function(up, err, errTip) {}
22
+ }
23
+ });
@@ -0,0 +1,25 @@
1
+ var Qiniu1 = new QiniuJsSDK;
2
+
3
+ var uploader1 = Qiniu1.uploader({
4
+ runtimes: 'html5,html4',
5
+ browse_button: 'certificate_image_key_button',
6
+ uptoken_url: '/private_uptoken',
7
+ unique_names: true,
8
+ save_key: false,
9
+ domain: '<%= Settings.qiniu.private.domain %>',
10
+ get_new_uptoken: false,
11
+ container: 'container',
12
+ max_file_size: '100mb',
13
+ max_retries: 3,
14
+ dragdrop: true,
15
+ drop_element: 'container',
16
+ chunk_size: '4mb',
17
+ auto_start: true,
18
+ init: {
19
+ 'FileUploaded': function(up, file, info) {
20
+ var res = $.parseJSON(info);
21
+ $('#certificate_image_key_id').val(res.key);
22
+ },
23
+ 'Error': function(up, err, errTip) {}
24
+ }
25
+ });
@@ -0,0 +1,3 @@
1
+ //# link rails_com/application.css
2
+ //= link rails_com/application.js
3
+ //= link_tree ../javascripts/controllers .js
File without changes
@@ -0,0 +1,92 @@
1
+ module RailsCom::ActiveHelper
2
+
3
+ # active_assert('notice' == 'notice', expected: 'ui info message', unexpected: 'ui negative message')
4
+ def active_assert(assert, expected: 'item active', unexpected: 'item')
5
+ if assert
6
+ expected
7
+ else
8
+ unexpected
9
+ end
10
+ end
11
+
12
+ def active_helper(controller: [], path: [], active_class: 'item active', item_class: 'item')
13
+ if path.present?
14
+ return active_class if path.include?(request.path)
15
+ end
16
+
17
+ if controller.present?
18
+ controller.include?(controller_name) ? active_class : item_class
19
+ else
20
+ item_class
21
+ end
22
+ end
23
+
24
+ def active_action(active_class: 'item active', item_class: 'item', **options)
25
+ ok = false
26
+ options.each do |key, value|
27
+ if controller_name == key.to_s
28
+ ok = true if value.include?(action_name)
29
+ end
30
+ end
31
+
32
+ if ok
33
+ active_class
34
+ else
35
+ item_class
36
+ end
37
+ end
38
+
39
+ # active_page controller: 'users', action: 'show', id: 371
40
+ def active_page(options)
41
+ active_class = options.delete(:active_class) || 'item active'
42
+ item_class = options.delete(:item_class) || 'item'
43
+
44
+ options.select { |_, v| v.blank? }.each do |k, v|
45
+ if params[k].to_s == v.to_s
46
+ return active_class
47
+ else
48
+ return item_class
49
+ end
50
+ end
51
+
52
+ current_page?(options) ? active_class : item_class
53
+ end
54
+
55
+ def active_params(options)
56
+ active_class = options.delete(:active_class) || 'item active'
57
+ item_class = options.delete(:item_class) || 'item'
58
+
59
+ options.select { |_, v| v.present? }.each do |k, v|
60
+ if params[k].to_s == v.to_s
61
+ return active_class
62
+ end
63
+ end
64
+
65
+ item_class
66
+ end
67
+
68
+ def filter_params(options = {})
69
+ except = options.delete(:except)
70
+ only = options.delete(:only)
71
+ query = request.GET.dup
72
+
73
+ if only.present?
74
+ query.slice!(*only)
75
+ else
76
+ excepts = []
77
+ if except.is_a? Array
78
+ excepts += except
79
+ else
80
+ excepts << except
81
+ end
82
+ excepts += ['commit', 'utf8', 'page']
83
+
84
+ query.except!(*excepts)
85
+ end
86
+
87
+ query.merge!(options)
88
+ query.reject! { |_, value| value.blank? }
89
+ query
90
+ end
91
+
92
+ end
@@ -0,0 +1,20 @@
1
+ module RailsComHelper
2
+
3
+ def js_load(filename = nil, root: Rails.root, **options)
4
+ filename ||= "controllers/#{controller_path}/#{action_name}"
5
+ path = root + 'app/assets/javascripts' + filename.to_s
6
+ if File.exist?(path.to_s + '.js') || File.exist?(path.to_s + '.js.erb')
7
+ javascript_include_tag filename, options
8
+ end
9
+ end
10
+
11
+ def simple_format_hash(hash_text, options = {})
12
+ wrapper_tag = options.fetch(:wrapper_tag, :p)
13
+
14
+ hash_text.map do |k, v|
15
+ text = k.to_s + ': ' + v.to_s
16
+ content_tag(wrapper_tag, text)
17
+ end.join("\n\n").html_safe
18
+ end
19
+
20
+ end
@@ -0,0 +1,26 @@
1
+ module StateMachine
2
+
3
+ # obj = Model.new
4
+ # obj.process_to state: 'xxx'
5
+ def process_to(options = {})
6
+ if options.size > 1
7
+ raise 'Only support one column'
8
+ end
9
+
10
+ options.each do |k, v|
11
+ states = k.to_s.pluralize
12
+ states = self.class.send(states).keys
13
+
14
+ i = states.find_index self.send(k)
15
+ n = states[i+1]
16
+
17
+ if n == v.to_s
18
+ update!(k => states[v])
19
+ else
20
+ errors.add :state, 'Next state is wrong'
21
+ raise ActiveRecord::Rollback, 'Next state is wrong'
22
+ end
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ <%= link_to url, class: 'item', title: t('views.pagination.first'), remote: remote do %>
2
+ <i class="fast backward icon"></i>
3
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <div class="item">
2
+ <%= t('views.pagination.truncate').html_safe %>
3
+ </div>
@@ -0,0 +1,3 @@
1
+ <%= link_to url, class: 'item', title: t('views.pagination.last'), remote: remote do %>
2
+ <i class="fast forward icon"></i>
3
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%= link_to url, class: 'item', title: t('views.pagination.next'), remote: remote do %>
2
+ <i class="right arrow icon"></i>
3
+ <% end %>
@@ -0,0 +1 @@
1
+ <%= link_to page, url, remote: remote, class: active_assert(page.current?) %>
@@ -0,0 +1,15 @@
1
+ <%= paginator.render do %>
2
+ <nav class="ui pagination menu">
3
+ <%= first_page_tag unless current_page.first? %>
4
+ <%= prev_page_tag unless current_page.first? %>
5
+ <% each_page do |page| %>
6
+ <% if page.display_tag? %>
7
+ <%= page_tag page %>
8
+ <% elsif !page.was_truncated? %>
9
+ <%= gap_tag %>
10
+ <% end %>
11
+ <% end %>
12
+ <%= next_page_tag unless current_page.last? %>
13
+ <%= last_page_tag unless current_page.last? %>
14
+ </nav>
15
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <%= link_to url, class: 'item', title: t('views.pagination.previous'), remote: remote do %>
2
+ <i class="left arrow icon"></i>
3
+ <% end %>
@@ -0,0 +1,12 @@
1
+ <% flash.each do |type, msg| %>
2
+ <div class="<%= active_assert(type == 'notice', expected: 'ui info message', unexpected: 'ui negative message') %>">
3
+ <i class="close icon"></i>
4
+ <p><%= msg %></p>
5
+ </div>
6
+ <% end %>
7
+
8
+ <script>
9
+ $('.message .close').on('click', function() {
10
+ $(this).closest('.message').fadeOut();
11
+ });
12
+ </script>
@@ -0,0 +1,17 @@
1
+ <% if target.errors.any? %>
2
+ <div class="ui negative message">
3
+ <i class="close icon"></i>
4
+ <div class="header">有 <%= target.errors.count %> 处问题导至无法提交:</div>
5
+ <ul class="list">
6
+ <% target.errors.full_messages.each do |msg| %>
7
+ <li><%= msg %></li>
8
+ <% end %>
9
+ </ul>
10
+ </div>
11
+ <% end %>
12
+
13
+ <script>
14
+ $('.message .close').on('click', function() {
15
+ $(this).closest('.message').fadeOut();
16
+ });
17
+ </script>
@@ -0,0 +1,17 @@
1
+ en:
2
+ views:
3
+ pagination:
4
+ first: First
5
+ last: Last
6
+ previous: Prev
7
+ next: Next
8
+ truncate: ...
9
+ helpers:
10
+ page_entries_info:
11
+ one_page:
12
+ display_entries:
13
+ zero: "No %{entry_name} found"
14
+ one: "Displaying <b>1</b> %{entry_name}"
15
+ other: "Displaying <b>all %{count}</b> %{entry_name}"
16
+ more_pages:
17
+ display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
@@ -0,0 +1,17 @@
1
+ zh:
2
+ views:
3
+ pagination:
4
+ first: First
5
+ last: Last
6
+ previous: Prev
7
+ next: Next
8
+ truncate: ...
9
+ helpers:
10
+ page_entries_info:
11
+ one_page:
12
+ display_entries:
13
+ zero: "No %{entry_name} found"
14
+ one: "Displaying <b>1</b> %{entry_name}"
15
+ other: "Displaying <b>all %{count}</b> %{entry_name}"
16
+ more_pages:
17
+ display_entries: "Displaying %{entry_name} <b>%{first}&nbsp;-&nbsp;%{last}</b> of <b>%{total}</b> in total"
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,37 @@
1
+ # 生成模型
2
+ #require 'rails/generators'
3
+ module Doc
4
+ class DocModelGenerator < Rails::Generators::NamedBase
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def create_controller_files
8
+ check_model_exist?
9
+
10
+ template 'model.erb', File.join('doc/api', "#{file_name}_model.md")
11
+ end
12
+
13
+ def check_model_exist?
14
+ Rails.application.eager_load! # 主动require 模型的定义
15
+ unless self.class.const_defined? model_name
16
+ abort "模型:#{model_name}没有定义"
17
+ end
18
+ end
19
+
20
+ def model_name
21
+ @model_name = file_name.classify
22
+ end
23
+
24
+ def model_class
25
+ @model_class = model_name.constantize
26
+ end
27
+
28
+
29
+ protected
30
+
31
+
32
+ def assign_model_name!(name)
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ # 生成模型
2
+ require_relative 'doc_model_generator'
3
+ module Doc
4
+ class DocModelsGenerator < Rails::Generators::Base
5
+
6
+ def create_controller_files
7
+ create_file 'doc/test.md', 'ddd'
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ ## 模型名称:<%= model_name %>
2
+ | 字段 | 类型 | 描述 | 示例 | server端需特殊处理 | update |
3
+ |---|---|---|---|---|---:|
4
+ <% model_class.columns.each do |column| -%>
5
+ | <%= column.name %> | <%= column.type.to_s %> |
6
+ <% end %>
@@ -0,0 +1,37 @@
1
+ class Hash
2
+
3
+ def deep_transform_values(&block)
4
+ _deep_transform_values_in_object(self, &block)
5
+ end
6
+
7
+ def deep_transform_values!(&block)
8
+ _deep_transform_values_in_object!(self, &block)
9
+ end
10
+
11
+ private
12
+ def _deep_transform_values_in_object(object, &block)
13
+ case object
14
+ when Hash
15
+ object.each_with_object({}) do |(key, value), result|
16
+ result[key] = _deep_transform_values_in_object(value, &block)
17
+ end
18
+ when Array
19
+ object.map { |e| _deep_transform_values_in_object(e, &block) }
20
+ else
21
+ yield(object)
22
+ end
23
+ end
24
+
25
+ def _deep_transform_values_in_object!(object, &block)
26
+ case object
27
+ when Hash
28
+ object.each do |key, value|
29
+ object[key] = _deep_transform_values_in_object!(value, &block)
30
+ end
31
+ when Array
32
+ object.map! { |e| _deep_transform_values_in_object!(e, &block) }
33
+ else
34
+ yield(object)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module RailsCom
2
+ class Engine < ::Rails::Engine
3
+
4
+ initializer 'rails_com.assets.precompile' do |app|
5
+ app.config.assets.precompile += ['rails_com_manifest.js']
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,70 @@
1
+ module Jobber
2
+ extend self
3
+
4
+ def deliver(job, *arguments)
5
+ msg = job_data(job, arguments)
6
+
7
+ r = redis_pool.with do |conn|
8
+ conn.pipelined do
9
+ conn.lpush(queue_key, JSON.dump(msg))
10
+ conn.lrange(queue_key, 0, 0)
11
+ end
12
+ end
13
+ JSON.load(r[1].first)
14
+ end
15
+
16
+ def job_data(job, args, at: nil)
17
+ {
18
+ class: 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
19
+ wrapped: job,
20
+ queue: config['queue'],
21
+ args: [ {
22
+ job_class: job,
23
+ job_id: SecureRandom.uuid,
24
+ queue_name: config['queue'],
25
+ priority: nil,
26
+ arguments: args,
27
+ locale: 'en'
28
+ } ],
29
+ retry: true,
30
+ jid: SecureRandom.hex(12),
31
+ created_at: Time.now.to_f,
32
+ enqueued_at: Time.now.to_f
33
+ }
34
+ end
35
+
36
+ def rjobs(size = 1)
37
+ result = redis_pool.with do |conn|
38
+ conn.lrange(queue_key, -size, -1)
39
+ end
40
+ result.map do |r|
41
+ JSON.load(r)
42
+ end
43
+ end
44
+
45
+ def ljobs(size = 1)
46
+ result = redis_pool.with do |conn|
47
+ conn.lrange(queue_key, 0, size - 1)
48
+ end
49
+ result.map do |r|
50
+ JSON.load(r)
51
+ end
52
+ end
53
+
54
+ def queue_key
55
+ @queue_key ||= "queue:#{config['queue']}"
56
+ end
57
+
58
+ def redis_pool
59
+ @redis_pool ||= ConnectionPool.new(size: 5, timeout: 5) { Redis.new(url: config['url']) }
60
+ end
61
+
62
+ def redis
63
+ Redis.new(url: config['url'])
64
+ end
65
+
66
+ def config
67
+ @config ||= Rails.application.config_for('jobber')
68
+ end
69
+
70
+ end
@@ -0,0 +1,113 @@
1
+ module QiniuHelper
2
+ extend self
3
+
4
+ def download_url(key)
5
+ Qiniu::Auth.authorize_download_url_2(config['host'], key)
6
+ end
7
+
8
+ def qiniu_url(key)
9
+ config['host'] << '/' unless config['host'].end_with? '/'
10
+ config['host'] + key.to_s
11
+ end
12
+
13
+ def generate_uptoken(key = nil)
14
+ put_policy = Qiniu::Auth::PutPolicy.new(config['bucket'], key)
15
+ @uptoken = Qiniu::Auth.generate_uptoken(put_policy)
16
+ end
17
+
18
+ def upload(local_file, key = nil)
19
+ code, result, response_headers = Qiniu::Storage.upload_with_token_2(
20
+ generate_uptoken(key),
21
+ local_file,
22
+ key,
23
+ nil,
24
+ bucket: config['bucket']
25
+ )
26
+ result['key']
27
+ end
28
+
29
+ def list(prefix = '')
30
+ list_policy = Qiniu::Storage::ListPolicy.new(config['bucket'], 10, prefix, '/')
31
+ code, result, response_headers, s, d = Qiniu::Storage.list(list_policy)
32
+ result['items']
33
+ end
34
+
35
+ def bsearch_last
36
+ ary = (0..9).to_a.reverse
37
+ search = 'chem_0'
38
+ begin_index = 0
39
+ end_index = ary.length - 1
40
+ result = nil
41
+
42
+ while true do
43
+ if end_index - begin_index > 1
44
+ index = ((begin_index + end_index) / 2.0).floor
45
+ end_flag = false
46
+ else
47
+ index = end_index
48
+ end_flag = true
49
+ end
50
+
51
+ search.sub! /\d$/, ary[index].to_s
52
+ list = self.list(search)
53
+
54
+ puts 'index: ' + index.to_s
55
+ puts 'search: ' + search
56
+ puts 'count: ' + list.size.to_s
57
+ puts '-------------'
58
+
59
+ if list.blank?
60
+ begin_index = index
61
+ elsif list.size >= 1
62
+ end_index = index
63
+
64
+ if end_flag
65
+ search << '9'
66
+ begin_index = 0
67
+ end_index = ary.length - 1
68
+
69
+ if list.size == 1
70
+ break result = list[0]['key']
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ result
77
+ end
78
+
79
+ def last
80
+ ary = (0..9).to_a.reverse
81
+ search = 'chem_0'
82
+ result = nil
83
+
84
+ while true do
85
+ break result if result
86
+ ary.each_with_index do |value, index|
87
+ search.sub! /\d$/, value.to_s
88
+ list = self.list(search)
89
+
90
+ puts 'index: ' + index.to_s
91
+ puts 'search: ' + search
92
+ puts 'count: ' + list.size.to_s
93
+ puts '-------------'
94
+
95
+ if list.blank?
96
+ next
97
+ elsif list.size > 1
98
+ search << '9'
99
+ break
100
+ elsif list.size == 1
101
+ break result = list[0]['key']
102
+ end
103
+ end
104
+ end
105
+
106
+ result
107
+ end
108
+
109
+ def config
110
+ @config ||= Rails.application.config_for('qiniu')
111
+ end
112
+
113
+ end
@@ -0,0 +1,31 @@
1
+ module UidHelper
2
+ extend self
3
+
4
+ def uuid(int, prefix = '', suffix = '')
5
+ str = int.to_s(36)
6
+ str = str + suffix
7
+ str = str.upcase.scan(/.{1,4}/).join('-')
8
+
9
+ if prefix.present?
10
+ str = prefix + '-' + str
11
+ end
12
+
13
+ str
14
+ end
15
+
16
+ def nsec_uuid(prefix = '')
17
+ time = Time.now
18
+ str = time.to_i.to_s + time.nsec.to_s
19
+ uuid str.to_i, prefix, rand_string
20
+ end
21
+
22
+ def sec_uuid(prefix = '')
23
+ time = Time.now
24
+ uuid time.to_i, prefix, rand_string(2)
25
+ end
26
+
27
+ def rand_string(len = 4)
28
+ len.times.map { ((0..9).to_a + ('A'..'Z').to_a).sample }.join
29
+ end
30
+
31
+ end
@@ -0,0 +1,54 @@
1
+ module RailsCom::ModelHelper
2
+
3
+ def to_factory_girl
4
+ require 'rails/generators'
5
+ require 'generators/factory_girl/model/model_generator'
6
+
7
+ args = [
8
+ self.name.underscore
9
+ ]
10
+ cols = columns.map { |col| "#{col.name}:#{col.type}" }
11
+
12
+ generator = FactoryGirl::Generators::ModelGenerator.new(args + cols, destination_root: Rails.root)
13
+ generator.invoke_all
14
+ end
15
+
16
+
17
+ def column_attributes
18
+ columns.map do |column|
19
+ [
20
+ column.name.to_sym,
21
+ column.default,
22
+ column.type,
23
+ column.sql_type,
24
+ column.null,
25
+ column.default_function
26
+ ]
27
+ end
28
+ end
29
+
30
+
31
+ def print_table(with_column: false)
32
+ columns.each do |column|
33
+ info = with_column ? '# t.column' : '# '
34
+ info << " :#{column.name.to_sym}, :#{column.type}"
35
+ info << ", precision: #{column.precision}" if column.precision
36
+ info << ", scale: #{column.scale}" if column.scale
37
+ info << ", limit: #{column.limit}" if column.limit
38
+ if column.default && column.type == :string
39
+ info << ", default: '#{column.default}'"
40
+ elsif column.default
41
+ info << ", default: #{column.default}"
42
+ end
43
+ info << ", null: false" unless column.null
44
+ info << ", comment: '#{column.comment} type: #{column.sql_type}'" if column.comment
45
+ puts info
46
+ end
47
+
48
+ nil
49
+ end
50
+
51
+ end
52
+
53
+
54
+ ActiveRecord::Base.extend RailsCom::ModelHelper
@@ -0,0 +1,20 @@
1
+ module RailsCom::Models
2
+ extend self
3
+
4
+ def models
5
+ models = ActiveRecord::Base.connection.tables.map do |table|
6
+ begin
7
+ klass = table.classify.constantize
8
+ klass.ancestors.include?(ActiveRecord::Base) ? klass : nil
9
+ rescue
10
+ nil
11
+ end
12
+ end
13
+ models.compact
14
+ end
15
+
16
+ def model_names
17
+ models.map { |klass| klass.to_s }
18
+ end
19
+
20
+ end
@@ -0,0 +1,35 @@
1
+ module RailsCom::Routes
2
+ extend self
3
+
4
+ def routes_wrapper
5
+ ary = []
6
+
7
+ routes.each do |route|
8
+ ary << detail(route)
9
+ end
10
+
11
+ ary
12
+ end
13
+
14
+ def actions(controller)
15
+ routes_wrapper.select { |i| i[:controller] == controller.to_s }.map { |i| i[:action] }.uniq
16
+ end
17
+
18
+ private
19
+ def detail(route)
20
+ wrap = ActionDispatch::Routing::RouteWrapper.new(route)
21
+ info = route.defaults
22
+
23
+ {
24
+ verb: route.verb,
25
+ path: wrap.path,
26
+ controller: info[:controller],
27
+ action: info[:action]
28
+ }
29
+ end
30
+
31
+ def routes
32
+ @routes ||= Rails.application.routes.routes
33
+ end
34
+
35
+ end
@@ -0,0 +1,3 @@
1
+ module RailsCom
2
+ VERSION = '1.0.0'
3
+ end
data/lib/rails_com.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rails_com/engine'
2
+ require 'rails_com/routes'
3
+ require 'rails_com/models'
4
+ require 'rails_com/model_helper'
5
+
6
+ require 'rails_com/helpers/uid_helper'
7
+ require 'rails_com/helpers/jobber'
8
+ require 'rails_com/helpers/qiniu_helper'
9
+
10
+ require 'rails_com/core_ext/hash'
11
+
12
+ module RailsCom
13
+ # Your code goes here...
14
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_com do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_com
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - qinmingyuan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.1
27
+ description: Description of RailsCom.
28
+ email:
29
+ - mingyuan0715@foxmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/qiniu.js
38
+ - app/assets/config/qiniu1.js
39
+ - app/assets/config/rails_com_manifest.js
40
+ - app/assets/javascripts/rails_com/application.js
41
+ - app/helpers/rails_com/active_helper.rb
42
+ - app/helpers/rails_com_helper.rb
43
+ - app/models/state_machine.rb
44
+ - app/views/kaminari/_first_page.html.erb
45
+ - app/views/kaminari/_gap.html.erb
46
+ - app/views/kaminari/_last_page.html.erb
47
+ - app/views/kaminari/_next_page.html.erb
48
+ - app/views/kaminari/_page.html.erb
49
+ - app/views/kaminari/_paginator.html.erb
50
+ - app/views/kaminari/_prev_page.html.erb
51
+ - app/views/shared/_alert.html.erb
52
+ - app/views/shared/_error_messages.html.erb
53
+ - config/locales/en.yml
54
+ - config/locales/zh.yml
55
+ - config/routes.rb
56
+ - lib/generators/doc_model_generator.rb
57
+ - lib/generators/doc_models_generator.rb
58
+ - lib/generators/templates/model.erb
59
+ - lib/rails_com.rb
60
+ - lib/rails_com/core_ext/hash.rb
61
+ - lib/rails_com/engine.rb
62
+ - lib/rails_com/helpers/jobber.rb
63
+ - lib/rails_com/helpers/qiniu_helper.rb
64
+ - lib/rails_com/helpers/uid_helper.rb
65
+ - lib/rails_com/model_helper.rb
66
+ - lib/rails_com/models.rb
67
+ - lib/rails_com/routes.rb
68
+ - lib/rails_com/version.rb
69
+ - lib/tasks/rails_com_tasks.rake
70
+ homepage: https://github.com/qinmingyuan/rails_com
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.6.8
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Summary of RailsCom.
94
+ test_files: []