Skip to content

Instantly share code, notes, and snippets.

@chipiga
Created June 30, 2011 10:44
Show Gist options
  • Save chipiga/1055994 to your computer and use it in GitHub Desktop.
Save chipiga/1055994 to your computer and use it in GitHub Desktop.
SmartSourcing.ru controllers
class PostsController < InheritedResources::Base
respond_to :html
respond_to :rss, :only => :index
before_filter :authenticate_user!, :except => [:index, :show, :search]
load_resource :except => [:index, :search]
authorize_resource
has_scope :by_blog, :only => :index, :as => :blog_id
has_scope :by_group, :only => :index, :as => :group_id
has_scope :tag
before_filter lambda { resource.thumb = nil if params[:thumb_delete] }, :only => :update
before_filter lambda {
if params[:blog_id]
@blog = Blog.by_domain(Domain.current_domain).find(params[:blog_id])
redirect_to @blog, :status => :moved_permanently unless @blog.friendly_id_status.best?
end
@group = Group.by_domain(Domain.current_domain).find(params[:group_id]) if params[:group_id]
@tag = ActsAsTaggableOn::Tag.find(params[:tag]) if params[:tag]
}, :only => :index
# Redirect legacy URLs
before_filter lambda {
unless resource.blog.cached_slug == params[:blog]
redirect_to resource, :status => :moved_permanently
end
}, :only => :show
before_filter lambda {
publish_on_main = params[:post].delete(:published_on_main)
unless publish_on_main.nil?
authorize! :publish_on_main, resource
resource.published_on_main = publish_on_main
end
recommendations = params[:post].delete(:recommendations)
unless recommendations.nil?
authorize! :recommend, resource
resource.recommendations = recommendations
end
}, :only => [:update, :create]
after_filter lambda { cookies[:new_post] = 'true' }, :only => :create
helper_method :current_ability
caches_page :show, :unless => lambda { |c| c.user_signed_in? }
cache_sweeper :post_sweeper, :only => [:create, :update, :destroy]
cache_sweeper :posts_feed_sweeper, :only => [:update, :destroy]
include Platform::Controllers::Paternity
include Platform::Controllers::Paginate
include Platform::Controllers::Domain
include Platform::Controllers::EagerLoading
def search
respond_with(collection)
end
def subscribe
current_user.subscribes.create(:post_id => params[:id])
redirect_to :back
end
def unsubscribe
current_user.subscribes.where(:post_id => params[:id]).first.delete
redirect_to :back
end
protected
def collection
@posts ||= if params[:query]
Post.search params[:query],
:with => {:blog_id => Blog.by_domain(Domain.current_domain).map(&:id)},
:include => [:blog, :user, :tags],
:page => params[:page],
:per_page => Platform.per_page
else
collection_with_paginate
end
end
end
class UsersController < InheritedResources::Base
respond_to :html
respond_to :csv, :only => :index
has_scope :confirmed
has_scope :employee, :type => :boolean
has_scope :by_country
has_scope :by_location
has_scope :with_communities, :default => lambda{Domain.current_domain}, :only => :index
before_filter :authenticate_user!
load_resource :except => [:index, :invite, :recalc_ratings, :join_community, :leave_community]
authorize_resource
include Platform::Controllers::CollectionRedirect
include Platform::Controllers::Paginate
include Platform::Controllers::Invitation
before_filter lambda{
resource.roles = params[:user][:roles] if params[:user][:roles]
(resource.admin_communities = params[:user][:admin_communities]) if current_user.super_admin?
resource.rating_participate = params[:user][:rating_participate] if params[:user][:rating_participate]
}, :only => :update
before_filter lambda{ @result ||= []; params[:email].each{|i, e|
if user = User.find_by_email(e) and user.confirmed? and user.communities?(Domain.current_community)
@result << "#{e} - уже зарегистрирован"
params[:email].delete i
end
} }, :only => :invite
def index
index! do |format|
format.csv { render :text => collection.map(&:to_csv).join("\n") }
end
end
def edit_skills
render "edit_skills", :layout => 'popup'
end
def update_skills
if resource.update_skills(params[:user][:skills])
render :text => 'location.reload();'
else
render "users/_edit_skills", :layout => 'popup'
end
end
def recalc_ratings
if params[:target] == 'user'
User.delay.recalc_ratings!(Domain.current_domain)
flash[:notice] = 'Пересчет рейтингов пользователей добавлен в очередь на обработку'
elsif params[:target] == 'company'
Company.delay.recalc_ratings!
flash[:notice] = 'Пересчет рейтингов компаний добавлен в очередь на обработку'
end
ThinkingSphinx::Configuration.instance.controller.delay.index # reindex
redirect_to :back
end
def join_community
current_user.join_community(Domain.current_domain)
Mailer.community_joining(current_user).deliver if current_user.email?
redirect_to :back rescue redirect_to root_path
end
def leave_community
current_user.leave_community(Domain.current_domain)
Mailer.community_leaving(current_user).deliver if current_user.email?
redirect_to :back rescue redirect_to root_path
end
protected
def collection
@users ||= (request.format.to_s == 'text/csv' ? collection_without_paginate : collection_with_paginate)
end
end
class CompaniesController < InheritedResources::Base
respond_to :html
theme 'exchange' # include Platform::Controllers::Theme
before_filter lambda { params[:company][:location_ids] = params[:company][:location_ids].first.split(',') if params[:company][:location_ids].try(:size) == 1 }, :only => [:create, :update] # not working for create if after auth
before_filter :registration_by_invitation, :only => :accept # create confirmed user from invitation params
before_filter :login_by_invitation, :only => :accept # autologin/redirect by invitation token
before_filter lambda { redirect_to new_company_path(:role => params[:id], :step => 2) if Company::ROLES.include? params[:id] }, :only => :update_roles # should be before auth
before_filter :authenticate_user!, :except => [:index, :show, :new]
before_filter :authenticate_user!, :only => :new, :unless => lambda {|c| c.session[:pending_user]}
load_resource :except => [:index, :accept]
authorize_resource
before_filter lambda {
if @invitation ||= ::Invitation.find_by_token(params[:token]) and @invitation.available?
@invitation.invitable.user ||= current_user; @invitation.invitable.save(:validate => false) # activate company
end
}, :only => :accept
before_filter lambda { resource.logo = nil if params[:logo_delete] }, :only => :update
before_filter lambda { params.delete(:per_page) if params[:per_page] && params[:per_page].empty? # Normalize per_page
params[:location_ids] = params[:location_ids].first.split(',') if params[:location_ids].try(:size) == 1
params[:service_ids] ||= Service.children_of(params[:service_id]).opened.map{|s| s.id.to_s} if params[:service_id].present? }, :only => :index
before_filter lambda { resource.roles = params[:role].to_a; resource.name = session[:pending_user].try(:fetch, :company_name) }, :only => :new
has_scope :confirmed, :default => 'true' #, :only => :show
caches_page :show, :unless => lambda {|c| c.user_signed_in?}
cache_sweeper :company_sweeper, :only => [:create, :update, :destroy]
cache_sweeper :company_user_sweeper, :only => :accept
include Platform::Controllers::Paternity
include Platform::Controllers::Paginate
include Platform::Controllers::Invitation
def create
if user_signed_in?
create! do |success, failure|
success.html {
resource.users << current_user
session[:company_id] = resource.id if return_to = stored_location_for('user')
redirect_to return_to || smart_resource_url
}
end
elsif session[:user_return_to]
create! do |success, failure|
success.html { session[:company_id] = resource.id; redirect_to stored_location_for('user') }
end
else
create!(:notice => 'Вам отправлена ссылка для подтверждения регистрации') do |success, failure|
success.html {
Invitation.create_for_resource(session[:pending_user], resource); session.delete :pending_user
redirect_to collection_path
}
end
end
end
def edit_technologies; end
def edit; edit!; end # TODO inherited_resources development bug?
def destroy
destroy!{ user_path(resource.user) }
end
def update_roles
resource.roles << params[:role]; resource.save :validate => false
flash[:notice] = 'Компания выбрана успешно!'
session[:company_id] = resource.id if return_to = stored_location_for('user')
redirect_to return_to || resource
end
protected
def collection
with_hash = {}.tap do |w|
w.merge!(:location_ids => params[:location_ids]) if params[:location_ids].present?
w.merge!(:company_service_ids => params[:service_ids]) if params[:service_ids].present?
w.merge!(:company_service_is_for_people_service_ids => Service.opened.map(&:id)) if params[:is_for_people].present?
w.merge!(:company_service_is_for_companies_service_ids => Service.opened.map(&:id)) if params[:is_for_companies].present?
end
@companies ||= Company.search(params[:query],
:with => with_hash,
:include => [:locations, :services],
:sort_mode => :extended,
:order => "rating_with_factor DESC, name ASC",
:page => params[:page],
:per_page => params.fetch(:per_page, Platform.per_page))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment