javy_tool 0.0.3 → 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cd41262da792826ff19b19ac3f8ba03d2ed3074
4
+ data.tar.gz: 0fb375d009db7e7090e5ddcc61e1f02953e14e11
5
+ SHA512:
6
+ metadata.gz: cccf9e365db4d4de826c5efb458e93fb0db93ec713124a4882715f2e682e73419a44bd2d61eabb6d3a30f5a9dfad3865e7e1c51c31ca8489cfc16f4042050a04
7
+ data.tar.gz: 8c9376ae07121fee8c9ff21562597c16a7a86699114bd8bc3d82b0734843d5a138c4f48f1ffc80739c17d5c24dfe6d427b30f0467888824e1287b302c3c39cd8
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.swp
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}
@@ -13,7 +13,6 @@ module JavyTool
13
13
 
14
14
  def drop_breadcrumb(title=nil, url=nil)
15
15
  title ||= @page_title
16
- #url ||= url_for
17
16
  if title
18
17
  if url
19
18
  @breadcrumbs.push("<a href=\"#{url}\">#{title}</a>".html_safe)
@@ -48,26 +47,17 @@ module JavyTool
48
47
  def render_body_tag
49
48
  class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
50
49
  id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""
51
-
52
- raw(%Q|<!--[if lt IE 7 ]>
53
- <body class="#{class_attribute} lt-ie9 lt-ie8 lt-ie7"><![endif]-->
54
- <!--[if IE 7 ]>
55
- <body class="#{class_attribute} lt-ie9 lt-ie8"><![endif]-->
56
- <!--[if IE 8 ]>
57
- <body class="#{class_attribute} lt-ie9"><![endif]-->
58
- <!--[if !IE]>-->
59
- <body#{id_attribute} class="#{class_attribute}">
60
- <!--<![endif]-->|)
50
+ raw(%Q[ <body#{id_attribute} class="#{class_attribute}"> ])
61
51
  end
62
52
 
63
53
 
64
- # display the flash messages
54
+ # display the flash messages using foundation
65
55
  def notice_message
66
56
  flash_messages = []
67
57
  flash.each do |type, message|
68
- type = :success if type == :notice
58
+ type = :info if type == :notice
69
59
  type = :alert if type == :error
70
- text = content_tag(:div, message, :class => "alert-box #{type}")
60
+ text = content_tag(:div, message.html_safe, class: "alert-box #{type}")
71
61
  flash_messages << text if message
72
62
  end
73
63
  flash_messages.join("\n").html_safe
@@ -80,20 +70,15 @@ module JavyTool
80
70
  def render_breadcrumb
81
71
  return "" if @breadcrumbs.nil? || @breadcrumbs.size <= 0
82
72
  prefix = "".html_safe
83
- crumb = "".html_safe
73
+ crumb = []#.html_safe
84
74
 
85
75
  @breadcrumbs.each_with_index do |c, i|
86
76
  breadcrumb_class = []
87
- breadcrumb_class << "active" if i == (@breadcrumbs.length - 1)
88
- if i == (@breadcrumbs.length - 1)
89
- breadcrumb_content = c
90
- else
91
- breadcrumb_content = c + content_tag(:span, ">", :class => "divider")
92
- end
77
+ breadcrumb_class << "current" if i == (@breadcrumbs.length - 1)
93
78
 
94
- crumb += content_tag(:li, breadcrumb_content ,:class => breadcrumb_class )
79
+ crumb.push content_tag(:li, c ,:class => breadcrumb_class )
95
80
  end
96
- return prefix + content_tag(:ul, crumb, :class => "breadcrumb")
81
+ return prefix + content_tag(:ul, crumb.join("").html_safe, :class => "breadcrumbs")
97
82
  end
98
83
 
99
84
  # add nested object to the parent form by ajax
@@ -131,12 +116,44 @@ module JavyTool
131
116
  # }
132
117
  #
133
118
  # deprecated in rails4 need to fix the link_to_founction
119
+ # Fixed:
120
+ ### Need change the association_fields like following:
121
+ #
122
+ # <div class="fields" rel="need_upload">
123
+ # <p>
124
+ # <%= f.label :img_src %><br />
125
+ # <%= f.hidden_field :img_src,:class=>"web_img_src" %>
126
+ # <%= f.hidden_field :item_type %>
127
+ # <%= f.hidden_field :position %>
128
+ # <%= f.hidden_field :_destroy %>
129
+ # <%= link_to "remove_fields", "#" %>
130
+ # </p>
131
+ # </div>
132
+ #
133
+ ### Need add code to application.js like following:
134
+ #
135
+ # $(document).bind("click",".add_fields",function(e){
136
+ # e.preventDefault();
137
+ # var _this = $(this);
138
+ # var new_id = new Date().getTime();
139
+ # var regexp = new RegExp("new_" + _this.data("association"), "g");
140
+ # var con = _this.data("content").replace(regexp, new_id);
141
+ # $(this).parent().before(con);
142
+ # }).bind("click",".remove_fields",function(e){
143
+ # e.preventDefault();
144
+ # var _this = $(this);
145
+ # _this.prev("input[type=hidden]").val("1");
146
+ # _this.closest(".fields").hide();
147
+ #
148
+ # });
149
+ #
150
+ #
134
151
  def link_to_add_fields(name, f, association,new_object=nil)
135
152
  new_object ||= f.object.class.reflect_on_association(association).klass.new()
136
- fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
153
+ fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
137
154
  render(association.to_s.singularize + "_fields", :f => builder)
138
155
  end
139
- link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields.html_safe)}\")")
156
+ link_to(name,"#",class: "add_fields",data: {association: association,content: escape_javascript(fields.html_safe)})
140
157
  end
141
158
 
142
159
 
@@ -166,10 +183,10 @@ module JavyTool
166
183
  end
167
184
 
168
185
  def self.included(receiver)
169
- receiver.extend ClassMethods
186
+ #receiver.extend ClassMethods
170
187
  receiver.send :include, InstanceMethods
171
188
  receiver.send :helper, Helpers
172
- receiver.send :before_filter, :set_breadcrumbs
189
+ receiver.send (Rails::VERSION::MAJOR > 3 ? :before_action : :before_filter), :set_breadcrumbs
173
190
  end
174
191
  end
175
192
  end
@@ -0,0 +1,46 @@
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: [],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
+ con_hash = params[param].select{|_,value|value.present?}
19
+ if con_hash.present?
20
+ _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?
21
+
22
+ if gt.present?
23
+ gt.collect!{|item|item.to_s}
24
+ gt = (gt + gt.map{|item| "gt_#{item}"}) & con_hash.keys
25
+ _gt_con=con_hash.extract!(*gt).tap{|t|Rails.logger.info(t.inspect)}.map{|k,v| ["#{k.sub(/^gt_/,'')} >= ?",v] }
26
+ end
27
+ if lt.present?
28
+ lt.collect!{|item|item.to_s}
29
+ lt = (lt + lt.map{|item| "lt_#{item}"}) & con_hash.keys
30
+ _lt_con= con_hash.extract!(*lt).tap{|t|Rails.logger.info(t.inspect)}.map{|k,v| ["#{k.sub(/^lt_/,'')} <= ?",v] }
31
+ end
32
+
33
+ all_ary_con = ((_like_con || [])+(_gt_con||[])+(_lt_con||[])).transpose
34
+ all_ary_con = [all_ary_con.first.join(" and "),all_ary_con.last].flatten if all_ary_con.present?
35
+ #适用于查询字段为空的情况
36
+ con_hash.each{|k,v|con_hash[k] = nil if v == 'null'}
37
+
38
+ #Rails.logger.info(con_hash.inspect)
39
+ end
40
+ end
41
+ [con_hash.presence || nil,all_ary_con]
42
+ end
43
+
44
+
45
+ end
46
+ 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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module JavyTool
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/javy_tool.rb CHANGED
@@ -3,6 +3,8 @@ 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"
6
8
  mattr_accessor :tool_config
7
9
  mattr_accessor :upload_path
8
10
  @@upload_path = "/tmp"
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: javy_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.1.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - javy_liu
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-04-11 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: pack some used methods
15
14
  email:
@@ -18,7 +17,7 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
- - .gitignore
20
+ - ".gitignore"
22
21
  - Gemfile
23
22
  - LICENSE.md
24
23
  - README.md
@@ -26,31 +25,33 @@ files:
26
25
  - javy_tool.gemspec
27
26
  - lib/javy_tool.rb
28
27
  - lib/javy_tool/breadcrumb.rb
28
+ - lib/javy_tool/construct_query.rb
29
+ - lib/javy_tool/csv.rb
29
30
  - lib/javy_tool/utils.rb
30
31
  - lib/javy_tool/version.rb
31
32
  homepage: https://github.com/javyliu/javy_tool
32
- licenses: []
33
+ licenses:
34
+ - MIT
35
+ metadata: {}
33
36
  post_install_message:
34
37
  rdoc_options: []
35
38
  require_paths:
36
39
  - lib
37
40
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
41
  requirements:
40
- - - ! '>='
42
+ - - ">="
41
43
  - !ruby/object:Gem::Version
42
44
  version: '0'
43
45
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
46
  requirements:
46
- - - ! '>='
47
+ - - ">="
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
50
  requirements: []
50
51
  rubyforge_project:
51
- rubygems_version: 1.8.24
52
+ rubygems_version: 2.4.6
52
53
  signing_key:
53
- specification_version: 3
54
+ specification_version: 4
54
55
  summary: some methods ofen used in my projects
55
56
  test_files: []
56
57
  has_rdoc: