Skip to content

Instantly share code, notes, and snippets.

<% cache(:action=>"me_action",:action_prefix=>"some_prefix") do %>
<% cache("name") do %>
do some view stuff here
<% end %>
class WidgetsController < ApplicationController
#bad formatting here b/c of wordpress (remove space before 'only' if you copy this next line of code)
cache_sweeper :widget_sweeper, : only => [:create,:update,:destroy]
caches_action :show, :if => Proc.new { |x| x.cacheable? }
def show
#...
end
end
class WidgetSweeper < ActionController::Caching::Sweeper
observe Widget
def after_update(widget)
expire_cache_for(widget)
end
def after_destroy(widget)
expire_cache_for(widget)
end
class ApplicationController < ActionController::Base
before_filter :init
def cacheable?
@user.blank? ? true : false
end
def init
@user = nil
@user = User.find(session[:user_id]) if session[:user_id]
class WidgetsController < ApplicationController
caches_action :show
def show
#...
end
end
require 'bcrypt'
module Lock
class CreatePasswordFileGenerator < Rails::Generators::Base
argument :password, :type => :string
source_root File.expand_path('../templates', __FILE__)
def create_password_file
password_salt = BCrypt::Engine.generate_salt
password_hash = BCrypt::Engine.hash_secret(password, password_salt)
require "lock"
require "rails"
module Lock
class Engine < Rails::Engine
initializer "lock.extend_application_controller" do
ActiveSupport.on_load(:action_controller) do
include LockApplicationController::InstanceMethods
extend LockApplicationController::ClassMethods
end
module LockApplicationController
module ClassMethods
def lock(opts={})
before_filter { |c| c.lock_filter opts[:actions] }
end
end
module InstanceMethods
def lock_filter(actions=nil)
if locked_action?(actions) and session[:lock_opened]!=true
LOCK_PATH = File.dirname(__FILE__) + "/lock"
require "#{LOCK_PATH}/engine.rb"
require "bcrypt"
module Lock
def self.passwords_match?(password)
begin
hashed_combo = IO.read("#{Rails.root}/config/lock_password")
rescue
return false