javy_tool 0.0.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/javy_tool.gemspec +1 -0
- data/lib/javy_tool/breadcrumb.rb +50 -29
- data/lib/javy_tool/construct_query.rb +49 -0
- data/lib/javy_tool/csv.rb +36 -0
- data/lib/javy_tool/custom_error.rb +22 -0
- data/lib/javy_tool/utils.rb +33 -3
- data/lib/javy_tool/version.rb +1 -1
- data/lib/javy_tool.rb +3 -0
- metadata +25 -45
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c318bf216be32e4276137323bcc268089b284baaaaeb7cc29db8ea607a110232
|
4
|
+
data.tar.gz: 878fb92a64d8ae00fd26fe63b44f7964dc987992e048c8c9a8e8c61dd1219826
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f06b8d932d1a0a2b150a421575e4a5339198fcac195e6c0c6b0d2ee1bd11ccb91d709d6aed843c058db1d61f71a338788ce064bed992f39c1650f6a9a4b97fe1
|
7
|
+
data.tar.gz: 32dad23465b2ffc60f1bb60a6ff76f3bfa530d20259750aaf5162eaaa75a5efab5fdff47a0cd9b72e1c49cd0c759c428ffa62524a3fedf7920bb6e3c25f8ab16
|
data/.gitignore
CHANGED
data/javy_tool.gemspec
CHANGED
@@ -7,6 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.name = "javy_tool"
|
8
8
|
gem.version = JavyTool::VERSION
|
9
9
|
gem.authors = ["javy_liu"]
|
10
|
+
gem.licenses = ["MIT"]
|
10
11
|
gem.email = ["javy_liu@163.com"]
|
11
12
|
gem.description = %q{pack some used methods}
|
12
13
|
gem.summary = %q{some methods ofen used in my projects}
|
data/lib/javy_tool/breadcrumb.rb
CHANGED
@@ -8,12 +8,12 @@ module JavyTool
|
|
8
8
|
protected
|
9
9
|
|
10
10
|
def set_breadcrumbs
|
11
|
-
|
11
|
+
#Rails.logger.info I18n.locale
|
12
|
+
@breadcrumbs = ["#{I18n.t('common.index_icon')}#{view_context.link_to(I18n.t('common.home'), root_path)}".html_safe]
|
12
13
|
end
|
13
14
|
|
14
15
|
def drop_breadcrumb(title=nil, url=nil)
|
15
16
|
title ||= @page_title
|
16
|
-
#url ||= url_for
|
17
17
|
if title
|
18
18
|
if url
|
19
19
|
@breadcrumbs.push("<a href=\"#{url}\">#{title}</a>".html_safe)
|
@@ -48,25 +48,18 @@ module JavyTool
|
|
48
48
|
def render_body_tag
|
49
49
|
class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
|
50
50
|
id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
|
51
|
-
|
52
|
-
raw(%Q|<!--[if lt IE 7 ]>
|
53
|
-
<body class="#{class_attribute} ie6"><![endif]-->
|
54
|
-
<!--[if gte IE 7 ]>
|
55
|
-
<body class="#{class_attribute} ie"><![endif]-->
|
56
|
-
<!--[if !IE]>-->
|
57
|
-
<body#{id_attribute} class="#{class_attribute}">
|
58
|
-
<!--<![endif]-->|)
|
59
|
-
|
51
|
+
raw(%Q[ <body#{id_attribute} class="#{class_attribute}"> ])
|
60
52
|
end
|
61
53
|
|
62
54
|
|
63
|
-
# display the flash messages
|
64
|
-
def notice_message
|
55
|
+
# display the flash messages using foundation
|
56
|
+
def notice_message(cls: 'alert-box')
|
65
57
|
flash_messages = []
|
66
58
|
flash.each do |type, message|
|
67
|
-
|
59
|
+
next if message.nil?
|
60
|
+
type = :info if type == :notice
|
68
61
|
type = :alert if type == :error
|
69
|
-
text = content_tag(:div, message, :
|
62
|
+
text = content_tag(:div, message.html_safe, class: "#{cls} #{type}")
|
70
63
|
flash_messages << text if message
|
71
64
|
end
|
72
65
|
flash_messages.join("\n").html_safe
|
@@ -79,20 +72,15 @@ module JavyTool
|
|
79
72
|
def render_breadcrumb
|
80
73
|
return "" if @breadcrumbs.nil? || @breadcrumbs.size <= 0
|
81
74
|
prefix = "".html_safe
|
82
|
-
crumb =
|
75
|
+
crumb = []#.html_safe
|
83
76
|
|
84
77
|
@breadcrumbs.each_with_index do |c, i|
|
85
78
|
breadcrumb_class = []
|
86
|
-
breadcrumb_class << "
|
87
|
-
if i == (@breadcrumbs.length - 1)
|
88
|
-
breadcrumb_content = c
|
89
|
-
else
|
90
|
-
breadcrumb_content = c + content_tag(:span, ">", :class => "divider")
|
91
|
-
end
|
79
|
+
breadcrumb_class << "current" if i == (@breadcrumbs.length - 1)
|
92
80
|
|
93
|
-
crumb
|
81
|
+
crumb.push content_tag(:li, c ,:class => breadcrumb_class )
|
94
82
|
end
|
95
|
-
return prefix + content_tag(:ul, crumb, :class => "
|
83
|
+
return prefix + content_tag(:ul, crumb.join("").html_safe, :class => "breadcrumbs")
|
96
84
|
end
|
97
85
|
|
98
86
|
# add nested object to the parent form by ajax
|
@@ -130,12 +118,45 @@ module JavyTool
|
|
130
118
|
# }
|
131
119
|
#
|
132
120
|
# deprecated in rails4 need to fix the link_to_founction
|
133
|
-
|
121
|
+
# Fixed:
|
122
|
+
### Need change the association_fields like following:
|
123
|
+
#
|
124
|
+
# <div class="fields" rel="need_upload">
|
125
|
+
# <p>
|
126
|
+
# <%= f.label :img_src %><br />
|
127
|
+
# <%= f.hidden_field :img_src,:class=>"web_img_src" %>
|
128
|
+
# <%= f.hidden_field :item_type %>
|
129
|
+
# <%= f.hidden_field :position %>
|
130
|
+
# <%= f.hidden_field :_destroy %>
|
131
|
+
# <%= link_to "remove_fields", "#" %>
|
132
|
+
# </p>
|
133
|
+
# </div>
|
134
|
+
#
|
135
|
+
### Need add code to application.js like following:
|
136
|
+
#
|
137
|
+
# $(document).bind("click",".add_fields",function(e){
|
138
|
+
# e.preventDefault();
|
139
|
+
# var _this = $(this);
|
140
|
+
# var new_id = new Date().getTime();
|
141
|
+
# var regexp = new RegExp("new_" + _this.data("association"), "g");
|
142
|
+
# var con = _this.data("content").replace(regexp, new_id);
|
143
|
+
# $(this).parent().before(con);
|
144
|
+
# }).bind("click",".remove_fields",function(e){
|
145
|
+
# e.preventDefault();
|
146
|
+
# var _this = $(this);
|
147
|
+
# _this.prev("input[type=hidden]").val("1");
|
148
|
+
# _this.closest(".fields").hide();
|
149
|
+
#
|
150
|
+
# });
|
151
|
+
#
|
152
|
+
#
|
153
|
+
def link_to_add_fields(name, f, association,new_object=nil,options={})
|
134
154
|
new_object ||= f.object.class.reflect_on_association(association).klass.new()
|
135
|
-
fields = f.fields_for(association, new_object, :
|
155
|
+
fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
|
136
156
|
render(association.to_s.singularize + "_fields", :f => builder)
|
137
157
|
end
|
138
|
-
|
158
|
+
options.merge!(data: {association: association,content: escape_javascript(fields.html_safe)},class: "add_fields")
|
159
|
+
link_to(name,"#",options)
|
139
160
|
end
|
140
161
|
|
141
162
|
|
@@ -165,10 +186,10 @@ module JavyTool
|
|
165
186
|
end
|
166
187
|
|
167
188
|
def self.included(receiver)
|
168
|
-
receiver.extend ClassMethods
|
189
|
+
#receiver.extend ClassMethods
|
169
190
|
receiver.send :include, InstanceMethods
|
170
191
|
receiver.send :helper, Helpers
|
171
|
-
receiver.send :before_filter, :set_breadcrumbs
|
192
|
+
receiver.send (Rails::VERSION::MAJOR > 3 ? :before_action : :before_filter), :set_breadcrumbs
|
172
193
|
end
|
173
194
|
end
|
174
195
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module JavyTool
|
2
|
+
module ConstructQuery
|
3
|
+
protected
|
4
|
+
|
5
|
+
#like_ary:需要作like查询的字段,数组类型
|
6
|
+
#model_class: 查询的类名,字符串类型
|
7
|
+
#param: 查询参数,为空的话默认用model_class参数的underscore版本
|
8
|
+
#返回数组 [普通条件,like条件]
|
9
|
+
def construct_condition(model_class,like_ary: [],left_like:[],right_like:[],param: nil,gt:[],lt:[])
|
10
|
+
model_class = model_class.to_s
|
11
|
+
_class = model_class.classify.constantize
|
12
|
+
param = param || model_class.underscore
|
13
|
+
#instance variable need to be remove
|
14
|
+
#_obj = _class.send(:new,params[param])
|
15
|
+
#self.instance_variable_set("@#{param}", _obj)
|
16
|
+
|
17
|
+
if params[param]
|
18
|
+
|
19
|
+
con_hash = params[param].permit!.to_hash.select{|_,value|value.present?}
|
20
|
+
if con_hash.present?
|
21
|
+
_like_con = con_hash.extract!(*(like_ary.collect{|item| item.to_s} & con_hash.keys)).map{|k,v| ["#{k} like ?","%#{v}%"] } if like_ary.present?
|
22
|
+
_left_like_con = con_hash.extract!(*(left_like.collect{|item| item.to_s} & con_hash.keys)).map{|k,v| ["#{k} like ?","#{v}%"] } if left_like.present?
|
23
|
+
_right_like_con = con_hash.extract!(*(right_like.collect{|item| item.to_s} & con_hash.keys)).map{|k,v| ["#{k} like ?","#{v}%"] } if right_like.present?
|
24
|
+
|
25
|
+
if gt.present?
|
26
|
+
gt.collect!(&:to_s)
|
27
|
+
gt = (gt + gt.map{|item| "gt_#{item}"}) & con_hash.keys
|
28
|
+
_gt_con=con_hash.extract!(*gt).map{|k,v| ["#{k.sub(/^gt_/,'')} >= ?",v] }
|
29
|
+
end
|
30
|
+
if lt.present?
|
31
|
+
lt.collect!(&:to_s)
|
32
|
+
lt = (lt + lt.map{|item| "lt_#{item}"}) & con_hash.keys
|
33
|
+
_lt_con= con_hash.extract!(*lt).map{|k,v| ["#{k.sub(/^lt_/,'')} <= ?",v] }
|
34
|
+
end
|
35
|
+
|
36
|
+
all_ary_con = ((_left_like_con || [])+(_right_like_con || [])+(_like_con || [])+(_gt_con||[])+(_lt_con||[])).transpose
|
37
|
+
all_ary_con = [all_ary_con.first.join(" and "),all_ary_con.last].flatten if all_ary_con.present?
|
38
|
+
#适用于查询字段为空的情况
|
39
|
+
con_hash.each{|k,v|con_hash[k] = nil if v == 'null'}
|
40
|
+
|
41
|
+
#Rails.logger.info(con_hash.inspect)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
[con_hash.presence || nil,all_ary_con]
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module JavyTool
|
2
|
+
module Csv
|
3
|
+
module ClassMethods
|
4
|
+
require 'csv'
|
5
|
+
#used to export xls or csv file
|
6
|
+
#need to extend in model
|
7
|
+
#use like following:
|
8
|
+
# format.xls {send_data @checkinouts.to_csv(col_sep: "\t")}
|
9
|
+
# or
|
10
|
+
# select_columns = "id,name,user_id"
|
11
|
+
# format.xls {send_data @checkinouts.select(_select_columns).to_csv(select: _select_columns)}
|
12
|
+
def to_csv(options = {},&block)
|
13
|
+
select_values = options.delete(:select)
|
14
|
+
objs = options.delete(:objs) # model instances list
|
15
|
+
select_values = select_values.split(",").collect{|e| e.split(/\s+|\./).last} if select_values.kind_of?(String)
|
16
|
+
options[:col_sep] ||= "\t"
|
17
|
+
CSV.generate(options) do |csv|
|
18
|
+
cols = select_values.presence || column_names
|
19
|
+
csv << cols
|
20
|
+
(objs || all).each do |item|
|
21
|
+
if block_given?
|
22
|
+
csv << yield(item,cols)
|
23
|
+
else
|
24
|
+
csv << item.attributes.values_at(*cols)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def self.included(receiver)
|
33
|
+
receiver.extend ClassMethods
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module JavyTool
|
2
|
+
module CustomError
|
3
|
+
class Error < StandardError; end
|
4
|
+
|
5
|
+
class AccessDenied < Error
|
6
|
+
attr_reader :action, :subject
|
7
|
+
attr_writer :default_message
|
8
|
+
|
9
|
+
def initialize(message = nil, action = nil, subject = nil)
|
10
|
+
@message = message
|
11
|
+
@action = action
|
12
|
+
@subject = subject
|
13
|
+
@default_message = I18n.t(:"custom_error.default", :default => "Access this page error.")
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
@message || @default_message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
data/lib/javy_tool/utils.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module JavyTool
|
2
2
|
module Utils
|
3
3
|
require "ostruct"
|
4
|
+
require "active_support"
|
4
5
|
module_function
|
5
6
|
|
6
7
|
#get user agent
|
@@ -71,6 +72,38 @@ module JavyTool
|
|
71
72
|
Iconv.conv("utf8","gb2312",text)
|
72
73
|
end
|
73
74
|
|
75
|
+
|
76
|
+
#用于抽奖
|
77
|
+
#传入代表中奖概率的数组如 1,10,20,30,40
|
78
|
+
#返回数组的索引
|
79
|
+
# 真实场景
|
80
|
+
# 奖项数组
|
81
|
+
# prize_arr =[
|
82
|
+
# ['id'=>1,'prize'=>'平板电脑','v'=>1],
|
83
|
+
# ['id'=>2,'prize'=>'数码相机','v'=>5],
|
84
|
+
# ['id'=>3,'prize'=>'音箱设备','v'=>10],
|
85
|
+
# ['id'=>4,'prize'=>'4G优盘','v'=>12],
|
86
|
+
# ['id'=>5,'prize'=>'10Q币','v'=>22],
|
87
|
+
# ['id'=>6,'prize'=>'下次没准就能中哦','v'=>50],
|
88
|
+
# ]
|
89
|
+
#
|
90
|
+
# 传给loggery的数组应为 1,5,10,12,22,50
|
91
|
+
# 返回的索引值即代表奖品的索引值
|
92
|
+
|
93
|
+
def lottery(*args)
|
94
|
+
args.extract_options!
|
95
|
+
total = args.inject{|sum,item|sum+=item}
|
96
|
+
|
97
|
+
args.flatten.each_with_index do |item,index|
|
98
|
+
rand_num = rand(1..total)#.tap{|it|puts ".#{item}.#{index}..#{it}.#{total}-------"}
|
99
|
+
if rand_num <= item
|
100
|
+
return index
|
101
|
+
else
|
102
|
+
total -= item
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
74
107
|
# upload file,default to /tmp
|
75
108
|
# return filename
|
76
109
|
def upload_file(file,path=nil)
|
@@ -80,9 +113,6 @@ module JavyTool
|
|
80
113
|
yield file.original_filename
|
81
114
|
else
|
82
115
|
Time.now.strftime("%Y%m%d%H%M%S") + rand(10000).to_s + File.extname(file.original_filename)
|
83
|
-
# if File.extname(file.original_filename).downcase == ".apk"
|
84
|
-
# file.original_filename.gsub(/[^\w]/,'') #
|
85
|
-
# end
|
86
116
|
end
|
87
117
|
File.open(path+filename, "wb") { |f| f.write(file.read) }
|
88
118
|
filename
|
data/lib/javy_tool/version.rb
CHANGED
data/lib/javy_tool.rb
CHANGED
@@ -3,6 +3,9 @@ require "javy_tool/version"
|
|
3
3
|
module JavyTool
|
4
4
|
autoload :Utils, "javy_tool/utils"
|
5
5
|
autoload :Breadcrumb, "javy_tool/breadcrumb"
|
6
|
+
autoload :Csv, "javy_tool/csv"
|
7
|
+
autoload :ConstructQuery, "javy_tool/construct_query"
|
8
|
+
autoload :CustomError, "javy_tool/custom_error"
|
6
9
|
mattr_accessor :tool_config
|
7
10
|
mattr_accessor :upload_path
|
8
11
|
@@upload_path = "/tmp"
|
metadata
CHANGED
@@ -1,34 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: javy_tool
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 2
|
10
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- javy_liu
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
date: 2014-01-02 00:00:00 Z
|
11
|
+
date: 2021-10-13 00:00:00.000000000 Z
|
19
12
|
dependencies: []
|
20
|
-
|
21
13
|
description: pack some used methods
|
22
|
-
email:
|
14
|
+
email:
|
23
15
|
- javy_liu@163.com
|
24
16
|
executables: []
|
25
|
-
|
26
17
|
extensions: []
|
27
|
-
|
28
18
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
|
31
|
-
- .gitignore
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
32
21
|
- Gemfile
|
33
22
|
- LICENSE.md
|
34
23
|
- README.md
|
@@ -36,41 +25,32 @@ files:
|
|
36
25
|
- javy_tool.gemspec
|
37
26
|
- lib/javy_tool.rb
|
38
27
|
- lib/javy_tool/breadcrumb.rb
|
28
|
+
- lib/javy_tool/construct_query.rb
|
29
|
+
- lib/javy_tool/csv.rb
|
30
|
+
- lib/javy_tool/custom_error.rb
|
39
31
|
- lib/javy_tool/utils.rb
|
40
32
|
- lib/javy_tool/version.rb
|
41
33
|
homepage: https://github.com/javyliu/javy_tool
|
42
|
-
licenses:
|
43
|
-
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
44
37
|
post_install_message:
|
45
38
|
rdoc_options: []
|
46
|
-
|
47
|
-
require_paths:
|
39
|
+
require_paths:
|
48
40
|
- lib
|
49
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
-
|
51
|
-
requirements:
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
52
43
|
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
version: "0"
|
58
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
-
none: false
|
60
|
-
requirements:
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
61
48
|
- - ">="
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
|
64
|
-
segments:
|
65
|
-
- 0
|
66
|
-
version: "0"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
67
51
|
requirements: []
|
68
|
-
|
69
|
-
rubyforge_project:
|
70
|
-
rubygems_version: 1.8.24
|
52
|
+
rubygems_version: 3.0.6
|
71
53
|
signing_key:
|
72
|
-
specification_version:
|
54
|
+
specification_version: 4
|
73
55
|
summary: some methods ofen used in my projects
|
74
56
|
test_files: []
|
75
|
-
|
76
|
-
has_rdoc:
|