forked from huacnlee/pasite
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
370 additions
and
11 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
= acts_as_views_count | ||
|
||
This plugin can help you easy to total views_count with Models, it can delay in cache. | ||
一个 Model 查看次数统计插件,支持统计数延迟如缓存(Rails.cache)里面,到了一定数量后在存入数据库,以提高效率 | ||
|
||
== Usage | ||
|
||
=== DB | ||
you need add views_count field to you table. | ||
|
||
=== Model | ||
|
||
class Post < ActiveRecord::Base | ||
acts_as_views_count | ||
end | ||
|
||
class Topic < ActiveRecord::Base | ||
# set delay save to db with 30 | ||
acts_as_views_count :delay => 30 | ||
end | ||
|
||
=== Controller | ||
|
||
class PostsController < ApplicationController | ||
def show | ||
@post = Post.find(params[:id]) | ||
# update views_count | ||
@post.update_views_count | ||
end | ||
end | ||
|
||
=== View | ||
|
||
<p> | ||
view:<%= @post.views_count_s %> comment: <%= @post.comments_count %> | ||
</p> | ||
|
||
== Credits | ||
|
||
Jason Lee <huacnlee@gmail.com> - http://huacnlee.com | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ActiveRecord::Base.extend ::ActsAsViewsCount |
43 changes: 43 additions & 0 deletions
43
vendor/plugins/acts_as_views_count/lib/acts_as_views_count.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# version: 0.1 | ||
module ActsAsViewsCount | ||
|
||
def self.included(base) | ||
base.extend ClassMethods | ||
end | ||
|
||
module ClassMethods | ||
# == Configuration options | ||
# :delay | ||
def acts_as_views_count(options = {}) | ||
cattr_accessor :views_count_delay,:views_count_delay_cache_key | ||
self.views_count_delay = options[:delay] || 20 | ||
self.views_count_delay_cache_key = "models/#{self.to_s.tableize}/acts_as_views_count/" | ||
include ActsAsViewsCount::InstanceMethods | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def update_views_count | ||
# puts "============== #{views_count_delay_cache_key}#{self.id}" | ||
views_count = Rails.cache.read("#{views_count_delay_cache_key}#{self.id}") | ||
if !views_count | ||
views_count = 0 | ||
end | ||
views_count += 1 | ||
|
||
if views_count >= views_count_delay | ||
self.views_count = (self.views_count || 0) + views_count | ||
self.save | ||
views_count = 0 | ||
end | ||
Rails.cache.write("#{views_count_delay_cache_key}#{self.id}",views_count) | ||
end | ||
|
||
def views_count_s | ||
views_count = Rails.cache.read("#{views_count_delay_cache_key}#{self.id}") || 0 | ||
return (self.views_count || 0) + views_count.to_i | ||
end | ||
end | ||
end | ||
|
||
ActiveRecord::Base.send(:include, ActsAsViewsCount) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
= Captcha | ||
|
||
This captcha generator is based on the very fine work of Eric Methot | ||
(http://blogs.ericmethot.com/) all credit and thanks should go to him. | ||
|
||
This fork allows you to specify a file type for captcha image generation (png or gif). | ||
|
||
Using Captcha is a five step process | ||
|
||
1. Install RMagick | ||
2. Install the plugin | ||
3. Generate a bunch of images off-line | ||
4. Use the helper method(s) | ||
5. Validate user input | ||
|
||
The plugin has been tested on OS X 10.5 using ImageMagick @6.4.1-8_0+q16 (via MacPorts) and | ||
the rmagick 2.5.2 gem. | ||
|
||
== Demo | ||
|
||
http://pasite.org/login | ||
|
||
== Install RMagic | ||
|
||
Follow the instructions on their site: | ||
http://rmagick.rubyforge.org/install-faq.html | ||
|
||
|
||
== Install the plugin | ||
|
||
Installing the plugin as a submodule is as simple as issuing these commands from the | ||
root of your rails application directory: | ||
|
||
To add as a submodule | ||
|
||
git submodule add git://github.com/zendesk/captcha.git | ||
git submodule init | ||
git submodule update | ||
|
||
To update the submodule | ||
|
||
git pull git://github.com/zendesk/captcha.git master | ||
|
||
Or clone and make your own changes | ||
|
||
git clone git@github.com:zendesk/captcha.git | ||
|
||
Update: Using submodules is a headache to the extent where they're not really worth while. | ||
Go for script/plugin install instead for increased happiness. | ||
|
||
== Generate a bunch of images off-line | ||
|
||
The reason why you don't need to generate images on the fly is that | ||
they have been generated in advance and all you do is pick one at | ||
random. | ||
|
||
First, define a salt for your application, do this by setting the global variable | ||
CAPTCHA_SALT to a random string in e.g. your environment.rb | ||
|
||
CAPTCHA_SALT = 'Something really random here' | ||
|
||
Next, generate the images by running the following rake task, this takes a while: | ||
|
||
rake captcha:generate COUNT=250 | ||
|
||
This will create the '/public/system/captcha' directory if it doesn't | ||
already exist and put 250 randomly generated captcha images. | ||
|
||
You can specify the following parameters when running the rake task: | ||
|
||
COUNT - the number of images to generate, default 3 | ||
IMAGE_HEIGHT - the height of the captcha image in pixels, default 50 | ||
IMAGE_WIDTH - the width of the captcha image in pixels, default 260 | ||
CAPTCHA_LENGTH - the number of characters in the captcha, default 5 | ||
FILE_FORMAT - the file type of the captcha image (png or gif) | ||
|
||
== Use the helper methods | ||
|
||
In your forms all you need to do is: | ||
|
||
<%= captcha_block %> | ||
|
||
And add a little bit of CSS styling to get a nice looking captcha validation text field | ||
and image. If you don't like the way it's setup then use the other helper methods | ||
(see captcha_helper.rb), which are more granular. | ||
|
||
|
||
== Validate user input | ||
|
||
In your controller, you will need to do the following: | ||
|
||
PostController < ApplicationController | ||
|
||
validates_captcha | ||
|
||
def create | ||
... | ||
if captcha_validated? | ||
... | ||
else | ||
... | ||
end | ||
end | ||
end | ||
|
||
That's it. | ||
|
||
Copyright (c) 2008 Zendesk, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
$:.unshift "#{File.dirname(__FILE__)}/lib" | ||
|
||
require 'captcha_util' | ||
require 'captcha_helper' | ||
require 'validates_captcha' | ||
|
||
ActionController::Base.class_eval do | ||
include ValidatesCaptcha | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
puts "Adding CAPTCHA_SALT global variable to environment.rb" | ||
environment = File.open("#{Rails.root}/config/environment.rb", 'a') | ||
environment.puts("CAPTCHA_SALT = '#{CaptchaUtil.encrypt_string(rand(1000000000))}'") | ||
environment.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module CaptchaHelper | ||
|
||
def captcha_image(options = {}) | ||
@captcha_image ||= CaptchaUtil::random_image | ||
image_tag('/system/captcha/' + @captcha_image, options) | ||
end | ||
|
||
def captcha_input_text(label, options = {}) | ||
@captcha_image ||= CaptchaUtil::random_image | ||
content_tag('label', label, :for => 'captcha') + text_field_tag(:captcha, nil, options) | ||
end | ||
|
||
def captcha_hidden_text | ||
@captcha_image ||= CaptchaUtil::random_image | ||
hidden_field_tag(:captcha_validation, @captcha_image.gsub(/\..+$/,'')) | ||
end | ||
|
||
def captcha_block(label = 'Please type the characters in the image below') | ||
content_tag('div', captcha_hidden_text + captcha_input_text(label) + captcha_image, { :class => 'captcha' }) | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
begin | ||
require 'RMagick' | ||
rescue Exception => e | ||
puts "Warning: RMagick not installed, you cannot generate captcha images on this machine" | ||
end | ||
|
||
require 'captcha_util' | ||
|
||
module CaptchaImageGenerator | ||
|
||
@@eligible_chars = (2..9).to_a + %w(A B C D E F G H J K L M N P Q R S T U V X Y Z) | ||
@@default_colors = ['#D02F01','#36AA09','#25689C','#383E51','#8D5D39'] | ||
@@default_bg_colors = ['#F8F8F8','#EDF7FF','#F7FEEC','#FFF2EA'] | ||
@@default_fonts = ['Arial','Verdana,Tahoma','MS Serif','Lucida Sans','Georgia'] | ||
@@default_parameters = { | ||
:image_width => 130, | ||
:image_height => 50, | ||
:captcha_length => 4 | ||
} | ||
|
||
def self.generate_captcha_image(params = {}) | ||
|
||
params.reverse_merge!(@@default_parameters) | ||
|
||
file_format = '.gif' | ||
|
||
bg_color = @@default_bg_colors[rand(@@default_bg_colors.size)] | ||
fore_color = @@default_colors[rand(@@default_colors.size)] | ||
font = @@default_fonts[rand(@@default_fonts.size)] | ||
|
||
|
||
black_img = Magick::Image.new(params[:image_width].to_i, params[:image_height].to_i) do | ||
self.background_color = bg_color | ||
self.quality = 80 | ||
end | ||
|
||
# Generate a 5 character random string | ||
random_string = (1..params[:captcha_length].to_i).collect { @@eligible_chars[rand(@@eligible_chars.size)] }.join('') | ||
|
||
|
||
# Gerenate the filename based on the string where we have removed the spaces | ||
filename = CaptchaUtil::encrypt_string(random_string.gsub(' ', '').downcase) + file_format | ||
|
||
# Render the text in the image | ||
black_img.annotate(Magick::Draw.new, 0,0,0,0, random_string) { | ||
self.gravity = Magick::CenterGravity | ||
self.font_family = font | ||
# self.text_antialias = true | ||
self.font_weight = Magick::BoldWeight | ||
self.fill = fore_color | ||
self.stroke = fore_color | ||
self.stroke_width = 0.5 | ||
self.kerning = -3.5 | ||
self.pointsize = 35 | ||
} | ||
|
||
# Apply a little blur and fuzzing | ||
# black_img = black_img.gaussian_blur(0.8, 0.8) | ||
# black_img.dissolve(black_img.sketch(0, 10, 135), 0.75, 0.25) | ||
# black_img = black_img.spread(0.5) | ||
wave_ranges = (-8..8).collect | ||
wave_direct = wave_ranges[rand(wave_ranges.size)] | ||
black_img = black_img.wave(wave_direct, 100) | ||
black_img = black_img.resize_to_fill(params[:image_width],params[:image_height]) | ||
# Write the file to disk | ||
puts 'Writing image file ' + filename | ||
|
||
black_img.write(filename) | ||
|
||
# Collect rmagick | ||
GC.start | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'digest/sha1' | ||
|
||
module CaptchaUtil | ||
|
||
def self.random_image | ||
@@captcha_files ||= Dir.glob("#{Rails.root}/public/system/captcha/*.gif").map {|f| File.basename(f)} | ||
@@captcha_files[rand(@@captcha_files.size)] | ||
end | ||
|
||
def self.encrypt_string(string) | ||
salt = 'This really should be random' | ||
|
||
if defined?(CAPTCHA_SALT) | ||
salt = CAPTCHA_SALT | ||
else | ||
Rails.logger.warn("No salt defined, please add CAPTHCA_SALT = 'Something really random' to environment.rb") | ||
end | ||
|
||
Digest::SHA1.hexdigest("#{salt}#{string}") | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ValidatesCaptcha | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def validates_captcha | ||
helper CaptchaHelper | ||
include ValidatesCaptcha::InstanceMethods | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
def captcha_validated? | ||
CaptchaUtil::encrypt_string(params[:captcha].to_s.gsub(' ', '').downcase) == params[:captcha_validation] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
$:.unshift "#{File.dirname(__FILE__)}/../lib" | ||
|
||
require 'captcha_image_generator' | ||
|
||
namespace :captcha do | ||
|
||
desc 'Generate a set of captcha images off-line' | ||
task :generate => :environment do | ||
FileUtils.mkdir_p('public/system/captcha') | ||
Dir.chdir('public/system/captcha') do | ||
image_count = (ENV['COUNT'] || 3).to_i | ||
image_params = resolve_params | ||
puts "Generating #{image_count} captcha images off-line #{'with params '+image_params.inspect unless image_params.empty?}" | ||
for i in 1..image_count | ||
CaptchaImageGenerator::generate_captcha_image(image_params) | ||
end | ||
end | ||
end | ||
|
||
def resolve_params | ||
params = {} | ||
['IMAGE_WIDTH', 'IMAGE_HEIGHT', 'CAPTCHA_LENGTH', 'FILE_FORMAT'].each do |param| | ||
params[param.downcase.to_sym] = ENV[param] if ENV[param] | ||
end | ||
params | ||
end | ||
end |