patch_utils 1.0.0 → 1.0.1
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 +4 -4
- data/lib/patch_utils/actions.rb +82 -0
- data/lib/patch_utils/active_support_encoding.rb +17 -0
- data/lib/patch_utils/hash.rb +26 -0
- data/lib/patch_utils/string.rb +34 -0
- data/lib/patch_utils/time_with_zone.rb +8 -0
- data/lib/patch_utils.rb +4 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6612a4b31eecf9678c515beda31029563112a41
|
4
|
+
data.tar.gz: e997d0767bd912bc0cd60479935c49887fb9f704
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc5f2b338b344a9af3876b09727c6ec104a28913224ad4917474c653eff205acb06bf487398909e3fcce3f265130631de345e7b381cc103723a665695f6470f0
|
7
|
+
data.tar.gz: b6571de749f6ed59e307572342318cbca852c7093bf6bd690afcd8fb2671620c3fe97f3cf3ec9cf836a647dda05e5aeb7fcb6d4ff66f6ad0d0baf74449a01acf
|
@@ -0,0 +1,82 @@
|
|
1
|
+
if defined?(ActionController::Caching::Actions)
|
2
|
+
module ActionController #:nodoc:
|
3
|
+
module Caching
|
4
|
+
# With this monkey patch, you can also use action cache as the original one. For more informations, refer to
|
5
|
+
# actionpack/lib/action_controller/caching/actions.rb in rails3.
|
6
|
+
# In rails4, action cache is removed from core, refer to https://github.com/rails/actionpack-page_caching,
|
7
|
+
# and it's original file is
|
8
|
+
# actionpack-action_caching/lib/action_controller/caching/actions.rb in rails4
|
9
|
+
#
|
10
|
+
# The original action cache uses request path without query string as cache key, so "/users/show/1" and "/users/show/2"
|
11
|
+
# can perform as expected. But "/users/query?name=jody&gender=male" and "/users/query?name=angel&gender=female" will use the same
|
12
|
+
# cache key, so the latter request will read cache and return the same response.
|
13
|
+
# So, this new feature is, you can use params as a part of the cache key, by configure them with params.
|
14
|
+
#
|
15
|
+
# class UsersController < ApplicationController
|
16
|
+
# caches_action :query, :cache_path => {:with_params => [:name, :gender]}
|
17
|
+
# def query
|
18
|
+
# @user = User.where(:id => params[:id]).first
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# def update
|
22
|
+
# user = User.where(:id => params[:id]).first
|
23
|
+
# expire_action(:controller => "users", :action => "query", :with_params => {:name => user.name, :gender => user.gender})
|
24
|
+
# user.update_attributes(params)
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
module Actions
|
28
|
+
class ActionCachePath
|
29
|
+
attr_reader :path, :extension
|
30
|
+
|
31
|
+
# If +infer_extension+ is true, the cache path extension is looked up from the request's
|
32
|
+
# path and format. This is desirable when reading and writing the cache, but not when
|
33
|
+
# expiring the cache - expire_action should expire the same files regardless of the
|
34
|
+
# request format.
|
35
|
+
def initialize(controller, options = {}, infer_extension = true)
|
36
|
+
with_params = options.extract!(:with_params)
|
37
|
+
if infer_extension
|
38
|
+
@extension = controller.params[:format]
|
39
|
+
options.reverse_merge!(:format => @extension) if options.is_a?(Hash)
|
40
|
+
end
|
41
|
+
|
42
|
+
path = controller.url_for(options).split(%r{://}).last
|
43
|
+
@path = normalize!(path)
|
44
|
+
@path = append_query_string!(@path, controller, with_params[:with_params]) if with_params.present?
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def append_query_string!(path, controller, with_params)
|
49
|
+
case with_params
|
50
|
+
when Array
|
51
|
+
if controller.env["QUERY_STRING"].blank?
|
52
|
+
return path
|
53
|
+
end
|
54
|
+
_params = Rack::Utils.parse_nested_query(controller.env["QUERY_STRING"]).slice(*with_params.map(&:to_s).sort)
|
55
|
+
_query_string = Rack::Utils.build_nested_query(_params)
|
56
|
+
"#{path}?#{URI.parser.unescape(_query_string)}"
|
57
|
+
when Hash
|
58
|
+
# Rack::Utils.build_nested_query({:a=>1}) # => "a"
|
59
|
+
# Rack::Utils.build_nested_query({:a=>'1'}) # => "a=1"
|
60
|
+
# Rack::Utils.build_nested_query(Rack::Utils.parse_nested_query({:a=>1}.to_param)) # => "a=1"
|
61
|
+
h = Rack::Utils.parse_nested_query(with_params.to_param)
|
62
|
+
_query_string = Rack::Utils.build_nested_query(h.slice(*h.keys.sort))
|
63
|
+
"#{path}?#{URI.parser.unescape(_query_string)}"
|
64
|
+
when String
|
65
|
+
if with_params.blank?
|
66
|
+
return path
|
67
|
+
end
|
68
|
+
_ps = Rack::Utils.parse_nested_query(with_params)
|
69
|
+
_params = _ps.slice(*_ps.keys.sort)
|
70
|
+
_query_string = Rack::Utils.build_nested_query(_params)
|
71
|
+
"#{path}?#{URI.parser.unescape(_query_string)}"
|
72
|
+
else
|
73
|
+
path
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
else
|
81
|
+
# If you are using rails4, you must add actionpack-action_caching to your gem list to use this patch.
|
82
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
# Enable emoji in rails json with utf8mb4 charset. Remember to change charset in your db.
|
3
|
+
if defined? ActiveSupport::JSON
|
4
|
+
module ActiveSupport::JSON::Encoding
|
5
|
+
class << self
|
6
|
+
def escape(string)
|
7
|
+
if string.respond_to?(:force_encoding)
|
8
|
+
string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY)
|
9
|
+
end
|
10
|
+
json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
|
11
|
+
json = %("#{json}")
|
12
|
+
json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
|
13
|
+
json
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
# When a http request is sent by ajax, if params contains a json array, eg: {arr: [{a: 1}, {b: 2}]},
|
3
|
+
# ruby backend will receive a hash like this: Parameters: {"arr"=>{"0"=>{"a"=>"1"},"1"=>{"b"=>"2"}}}.
|
4
|
+
# Use params[:arr] = params[:arr].to_params_array to make sure params[:arr] is an array.
|
5
|
+
class Hash
|
6
|
+
def to_params_array
|
7
|
+
return self if self['0'].nil?
|
8
|
+
arr = []
|
9
|
+
self.each do |k,v|
|
10
|
+
arr[k.to_i] = v.to_params_array
|
11
|
+
end
|
12
|
+
arr
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Array
|
17
|
+
def to_params_array
|
18
|
+
self
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class NilClass
|
23
|
+
def to_params_array
|
24
|
+
self
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding:utf-8
|
2
|
+
class String
|
3
|
+
#
|
4
|
+
#SINGLE_BYTE_REGEX = /\A[\u0000-\u00ff]+\z/
|
5
|
+
#def is_single_byte_char?
|
6
|
+
# !!(SINGLE_BYTE_REGEX =~ self.strip)
|
7
|
+
#end
|
8
|
+
#
|
9
|
+
## Notice: In mysql db with utf8, varchar(4) means 4 chinese characters or 4 numbers or 4 english letters.
|
10
|
+
## So this method is NOT used for valid whether the string can be stored to mysql db with utf8.
|
11
|
+
## return char size of the string, with 2 for each chinese characters and 1 for others.
|
12
|
+
##
|
13
|
+
#MULTI_BYTE_REGEX = /[^\u0000-\u00ff]/
|
14
|
+
#def char_size
|
15
|
+
# self.gsub(MULTI_BYTE_REGEX, '12').length
|
16
|
+
#end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
EMAIL_ADDR_REGEX = /\A([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}\z/
|
21
|
+
def is_email?
|
22
|
+
!!(EMAIL_ADDR_REGEX =~ self.strip)
|
23
|
+
end
|
24
|
+
|
25
|
+
CHINESE_CHARACTER_REGEX = /\A[\u4e00-\u9fa5]+\z/
|
26
|
+
def is_chinese_character?
|
27
|
+
!!(EMAIL_ADDR_REGEX =~ self.strip)
|
28
|
+
end
|
29
|
+
|
30
|
+
CELLPHONE_NUM_REGEX = /\A1\d{10}\z/
|
31
|
+
def is_cellphone_num?
|
32
|
+
!!(CELLPHONE_NUM_REGEX =~ self.strip)
|
33
|
+
end
|
34
|
+
end
|
data/lib/patch_utils.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: patch_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zzqwdss
|
@@ -15,7 +15,13 @@ email: zzqwdss@163.com
|
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
|
-
files:
|
18
|
+
files:
|
19
|
+
- lib/patch_utils.rb
|
20
|
+
- lib/patch_utils/actions.rb
|
21
|
+
- lib/patch_utils/active_support_encoding.rb
|
22
|
+
- lib/patch_utils/hash.rb
|
23
|
+
- lib/patch_utils/string.rb
|
24
|
+
- lib/patch_utils/time_with_zone.rb
|
19
25
|
homepage: http://rubygems.org/gems/patch_utils
|
20
26
|
licenses: []
|
21
27
|
metadata: {}
|