tiny_support 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/app/ables/tiny_support/configable.rb +20 -1
- data/app/ables/tiny_support/formable.rb +26 -3
- data/app/views/tiny_captcha/_tiny_captcha.erb +33 -0
- data/config/locales/en/tiny_support.en.yml +2 -0
- data/config/locales/zh-CN/tiny_support.zh-CN.yml +2 -0
- data/db/migrate/20131129224440_create_tiny_captcha_data.rb +16 -0
- data/lib/paperclip/storage/gridfs.rb +82 -0
- data/lib/tiny_support/action_controller/request_init.rb +60 -0
- data/lib/tiny_support/action_controller.rb +12 -0
- data/lib/tiny_support/active_record/display_able.rb +42 -0
- data/lib/tiny_support/active_record/display_base.rb +12 -0
- data/lib/tiny_support/active_record/has_avatar_able.rb +21 -0
- data/lib/tiny_support/active_record/nested_set.rb +820 -0
- data/lib/tiny_support/config.rb +56 -0
- data/lib/tiny_support/const.rb +7 -0
- data/lib/tiny_support/engine.rb +4 -0
- data/lib/tiny_support/gridfs_client.rb +48 -0
- data/lib/tiny_support/middlewares/gridfs.rb +83 -0
- data/lib/tiny_support/version.rb +1 -1
- data/lib/tiny_support.rb +40 -0
- metadata +185 -70
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8e09694741fa8267fe679927f667b648c57ba270
|
4
|
+
data.tar.gz: 5ff8f18d6ac559638b3e38200250f696705459ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 03028333c2899cb8c037827b5cd069f449a1558b796e2e93ea496463f5be4bbd0b9abae7c8251f95e4c333415fe78661aedd41262538d82952bffa572e785073
|
7
|
+
data.tar.gz: 7c99eb97b044bf845f76ea25bd71733873fa90d100f9e704d0b9426e1348d910646140dcaba19d972402bd3aec64acdefd71f24e4fb761f95df7cd88b05a6596
|
@@ -8,12 +8,30 @@ module TinySupport
|
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
+
|
12
|
+
#
|
13
|
+
# field_name: 字段名称
|
14
|
+
# configs: 请传入一个数组
|
15
|
+
#
|
16
|
+
# ---
|
17
|
+
# 会生成方法:
|
18
|
+
# - 实例方法: # 以 field_name = status 为例
|
19
|
+
# * status_key
|
20
|
+
# * status_key=
|
21
|
+
# * status_key?(a_key)
|
22
|
+
# 类方法:
|
23
|
+
# * status_config
|
24
|
+
#
|
11
25
|
def tiny_support_config field_name, configs=[]
|
12
26
|
_class_config_name = :"#{field_name}_config".to_sym
|
13
27
|
cattr_accessor _class_config_name, :instance_reader => false
|
14
|
-
_configs = TinyUtil::Config.new(configs)
|
28
|
+
_configs = ::TinyUtil::Config.new(configs)
|
15
29
|
self.class_variable_set("@@#{_class_config_name}", _configs)
|
16
30
|
|
31
|
+
scope :status_of, ->(key) do
|
32
|
+
where(:status => _configs.find_by_key(_key).value)
|
33
|
+
end
|
34
|
+
|
17
35
|
define_method :"#{field_name}" do
|
18
36
|
_configs.find_by_value(send(:"#{field_name}_value"))
|
19
37
|
end
|
@@ -34,3 +52,4 @@ module TinySupport
|
|
34
52
|
end
|
35
53
|
end
|
36
54
|
end
|
55
|
+
# *
|
@@ -4,15 +4,32 @@ module TinySupport
|
|
4
4
|
extend ::ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
include ActiveModel::Model
|
8
|
-
include ActiveModel::Conversion
|
9
|
-
include ActiveModel::Validations
|
7
|
+
include ::ActiveModel::Model
|
8
|
+
include ::ActiveModel::Conversion
|
9
|
+
include ::ActiveModel::Validations
|
10
|
+
include ::ActiveModel::Validations::Callbacks
|
11
|
+
|
10
12
|
|
11
13
|
include ::TinySupport::Configable
|
12
14
|
end
|
13
15
|
|
16
|
+
module ClassMethods
|
17
|
+
def support_strip_values! *fields
|
18
|
+
before_validation do |r|
|
19
|
+
fields.each do |_field|
|
20
|
+
r.send(:"#{_field}").to_s.strip!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
14
26
|
public
|
15
27
|
|
28
|
+
def initialize *args
|
29
|
+
attrs = args.extract_options!
|
30
|
+
assign_attrs attrs
|
31
|
+
end
|
32
|
+
|
16
33
|
def success?
|
17
34
|
self.errors.empty?
|
18
35
|
end
|
@@ -21,6 +38,12 @@ module TinySupport
|
|
21
38
|
!(success?)
|
22
39
|
end
|
23
40
|
|
41
|
+
def assign_attrs attrs={}
|
42
|
+
attrs.each do |field, value|
|
43
|
+
self.send(:"#{field}=", value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
24
47
|
private
|
25
48
|
|
26
49
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
<style type="text/CSS">
|
2
|
+
.tiny_captcha{padding: 5px !important;}
|
3
|
+
.tiny_captcha,
|
4
|
+
.tiny_captcha div{display: table;}
|
5
|
+
.tiny_captcha .tiny_captcha_field,
|
6
|
+
.tiny_captcha .tiny_captcha_image{
|
7
|
+
margin: 0px 0px 2px 0px !important;
|
8
|
+
padding: 0px !important;
|
9
|
+
}
|
10
|
+
.tiny_captcha .tiny_captcha_image img{
|
11
|
+
margin: 0px !important;
|
12
|
+
padding: 0px !important;
|
13
|
+
width: 110px !important;
|
14
|
+
}
|
15
|
+
.tiny_captcha .tiny_captcha_label{font-size: 12px;}
|
16
|
+
.tiny_captcha .tiny_captcha_field input{
|
17
|
+
width: 150px !important;
|
18
|
+
}
|
19
|
+
</style>
|
20
|
+
|
21
|
+
<div class='tiny_captcha'>
|
22
|
+
<div class='tiny_captcha_image'>
|
23
|
+
<%= tiny_captcha_options[:image] %>
|
24
|
+
</div>
|
25
|
+
|
26
|
+
<div class='tiny_captcha_field'>
|
27
|
+
<%= tiny_captcha_options[:field] %>
|
28
|
+
</div>
|
29
|
+
|
30
|
+
<div class='tiny_captcha_label'>
|
31
|
+
<%= tiny_captcha_options[:label] %>
|
32
|
+
</div>
|
33
|
+
</div>
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class CreateTinyCaptchaData < ActiveRecord::Migration
|
3
|
+
def self.up
|
4
|
+
create_table :tiny_captcha_data do |t|
|
5
|
+
t.string :key, :limit => 40
|
6
|
+
t.string :value, :limit => 6
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :tiny_captcha_data, :key
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :tiny_captcha_data
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Paperclip
|
3
|
+
module Storage
|
4
|
+
# MongoDB's GridFS storage system (http://www.mongodb.org/display/DOCS/GridFS) uses
|
5
|
+
# a chunking strategy to store files in a mongodb database.
|
6
|
+
# Specific options for GridFS:
|
7
|
+
# * +gridfs_credentials+: Similar to +s3_credentials+, this can be a path, a File, or
|
8
|
+
# a Hash. Keys are as follows:
|
9
|
+
# * +database+: the name of the MongoDB database to connect to. This can also be
|
10
|
+
# a +Mongo::DB+ object, in which case that connection will be used, and other
|
11
|
+
# credentials will be ignored.
|
12
|
+
# * +host+: defaults to +localhost+
|
13
|
+
# * +username+ and +password+: optional authentication for the database server.
|
14
|
+
#
|
15
|
+
# Note that, because files stored using the +:gridfs+ storage module are stored
|
16
|
+
# within the database rather than the file system, you'll need to work out a method
|
17
|
+
# to extract the file data to serve it over HTTP. This is pretty trivial using
|
18
|
+
# Rails Metal.
|
19
|
+
|
20
|
+
module Gridfs
|
21
|
+
def self.extended base
|
22
|
+
begin
|
23
|
+
require 'mongo'
|
24
|
+
rescue LoadError => e
|
25
|
+
e.message << " (You may need to install the mongo gem)"
|
26
|
+
raise e
|
27
|
+
end
|
28
|
+
|
29
|
+
base.instance_eval do
|
30
|
+
@gridfs = ::TinySupport::GridFSClient.new(@options[:gridfs]).gridfs
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def exists? style = default_style
|
35
|
+
if original_filename
|
36
|
+
!!@gridfs.exist?(:filename => _url_of_file_saved(style))
|
37
|
+
else
|
38
|
+
false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a binary representation of the data of the file assigned to the given style
|
43
|
+
def to_file style = default_style
|
44
|
+
@queued_for_write[style] || (@gridfs.open(_url_of_file_saved(style), 'r') if exists?(style))
|
45
|
+
end
|
46
|
+
|
47
|
+
def flush_writes #:nodoc:
|
48
|
+
@queued_for_write.each do |style, file|
|
49
|
+
_url = _url_of_file_saved(style)
|
50
|
+
log("saving #{_url}")
|
51
|
+
@gridfs.open(_url, 'w', :content_type => content_type) do |f|
|
52
|
+
f.write file.read
|
53
|
+
end
|
54
|
+
end
|
55
|
+
after_flush_writes # allows attachment to clean up temp files
|
56
|
+
@queued_for_write = {}
|
57
|
+
end
|
58
|
+
|
59
|
+
def flush_deletes #:nodoc:
|
60
|
+
@queued_for_delete.each do |url|
|
61
|
+
log("deleting #{url}")
|
62
|
+
@gridfs.delete(url)
|
63
|
+
end
|
64
|
+
@queued_for_delete = []
|
65
|
+
end
|
66
|
+
|
67
|
+
def after_flush_writes
|
68
|
+
@queued_for_write.each do |style, file|
|
69
|
+
file.close unless file.closed?
|
70
|
+
file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def _url_of_file_saved style=default_style
|
77
|
+
url(style).to_s.split('?').first
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinySupport
|
3
|
+
module ActionController
|
4
|
+
module RequestInit
|
5
|
+
def self.included base
|
6
|
+
base.class_eval do
|
7
|
+
include ControllerMethods
|
8
|
+
helper HelperMethods
|
9
|
+
|
10
|
+
before_action do |controller|
|
11
|
+
request = controller.send(:request)
|
12
|
+
|
13
|
+
# logger
|
14
|
+
if defined? Rails
|
15
|
+
Rails.logger.debug " - request logger start"
|
16
|
+
Rails.logger.debug " remote_ip: #{request.remote_ip}"
|
17
|
+
Rails.logger.debug " locale: #{I18n.locale}"
|
18
|
+
Rails.logger.debug " session:"
|
19
|
+
session.to_hash.each do |key, value|
|
20
|
+
Rails.logger.debug " - #{key}: #{value}"
|
21
|
+
end
|
22
|
+
|
23
|
+
# set locale
|
24
|
+
params_locale = params[:locale] || cookies[:locale]
|
25
|
+
|
26
|
+
if params_locale.present?
|
27
|
+
params_locale = params_locale.to_sym
|
28
|
+
|
29
|
+
if I18n.available_locales.include?(params_locale)
|
30
|
+
I18n.locale = params_locale
|
31
|
+
set_cookies('locale', :value => params_locale)
|
32
|
+
else
|
33
|
+
I18n.locale = I18n.default_locale
|
34
|
+
end
|
35
|
+
else
|
36
|
+
I18n.locale = I18n.default_locale
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module ControllerMethods
|
44
|
+
# 设置cookies
|
45
|
+
def set_cookies cookie_name, options={}
|
46
|
+
cookies[cookie_name] = {
|
47
|
+
:expires => ::Time.now + 10.years.to_i,
|
48
|
+
:path => '/',
|
49
|
+
:domain => ::DomainName.new(request.host).domain
|
50
|
+
}.merge(options)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# helper 方法
|
55
|
+
module HelperMethods
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "tiny_support/action_controller/request_init"
|
3
|
+
|
4
|
+
module TinySupport
|
5
|
+
module ActionController
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
if defined?(ActionController::Base)
|
10
|
+
::ActionController::Base.send :include, ::TinySupport::ActionController::RequestInit
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinySupport
|
3
|
+
module ActiveRecord
|
4
|
+
module DisplayAble
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def tiny_support_display *args
|
9
|
+
options = args.extract_options!
|
10
|
+
|
11
|
+
cattr_accessor :tiny_support_display_klass, :instance_reader => false
|
12
|
+
|
13
|
+
tiny_support_display_klass_name = "#{self.name}Display"
|
14
|
+
|
15
|
+
begin
|
16
|
+
if (tiny_support_display_klass=(tiny_support_display_klass_name.constantize)).is_a?(Class)
|
17
|
+
self.class_variable_set("@@tiny_support_display_klass", tiny_support_display_klass)
|
18
|
+
|
19
|
+
args.each do |klass|
|
20
|
+
if klass.is_a?(Module)
|
21
|
+
tiny_support_display_klass.send :include, klass
|
22
|
+
else
|
23
|
+
raise "#{klass} is not a Module"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
rescue
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method :tiny_support_display do
|
32
|
+
@tiny_support_display ||= self.class.class_variable_get('@@tiny_support_display_klass').new(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method :display do
|
36
|
+
self.tiny_support_display
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module TinySupport
|
3
|
+
module ActiveRecord
|
4
|
+
module HasAvatarAble
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def has_upload_avatar_able *args
|
9
|
+
options = args.extract_options!
|
10
|
+
_filed_name = args.first || :avatar
|
11
|
+
|
12
|
+
_options = ::TinySupport.config.upload_attachment_options.merge(options)
|
13
|
+
_options[:url] ||= "/#{TinySupport.config.default_upload_prefix}/:class/:attachment/:id_partition/:style/:filename"
|
14
|
+
|
15
|
+
self.has_attached_file _filed_name, _options
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|