Created
October 14, 2013 15:10
-
-
Save goldnuggets24/6977197 to your computer and use it in GitHub Desktop.
This is the controller I'm working with in card_signups (card_signups_controller) I need to add functionality that creates a new 'GiftConversion' class instance for the user when they click on a button in the _social partial.
This file contains hidden or 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
class CardSignupsController < ApplicationController | |
before_filter :require_user_sign_up | |
before_filter :require_unique, :only => [:print_temp_card, :update] | |
before_filter :get_card_user, :only => [:rewards, :edit, :update, :print_coupon] | |
before_filter :get_rewards, :only => [:rewards, :print_coupon, :edit] | |
skip_before_filter :verify_authenticity_token | |
def index | |
@first_stupid = CardSignup.new | |
if params[:secret_user_id].present? | |
@referral_card_signup = User.find(params[:secret_user_id]) | |
@referral = @referral_card_signup.gift_conversions | |
@referral << GiftConversion.new | |
end | |
render | |
end | |
def new | |
if params[:user] && current_user.admin | |
@card_signup = User.find(params[:user]).build_card_signup | |
else | |
@card_signup = current_user.build_card_signup | |
end | |
end | |
def disclaimer | |
batch = CardBatch.current_batch | |
batch = CardBatch.create unless batch | |
if User.exists?(params[:user]) && current_user.admin | |
@card_signup = User.find(params[:user]).build_card_signup(params[:card_signup]) | |
else | |
@card_signup = current_user.build_card_signup(params[:card_signup]) | |
end | |
@card_signup.card_batch = batch | |
@card_signup.exp_date = 3.years.from_now | |
if @card_signup.save | |
2.times{@card_signup.user.gift_conversions << GiftConversion.new} | |
Notifier.welcome_message(@card_signup).deliver | |
@possible_converts = CardReferral.all.select{|cf|cf.email.downcase == (params[:card_signup][:email]).downcase } | |
unless @possible_converts.empty? | |
@valid_ref = @possible_converts.find(:order => "created_at").first | |
if @valid_ref.email.downcase == @card_signup.email.downcase | |
User.find(@valid_ref.user_id).conversions << @card_signup | |
Notifier.new_reward_message(User.find(@valid_ref.user_id).card_signup, @card_signup).deliver | |
end | |
end | |
respond_to do |wants| | |
wants.html {} | |
wants.json { render :json => { :html => (render_to_string :partial => 'final_form')}} | |
if params[:user] && current_user.admin | |
Notifier.card_signup_email(User.find(params[:user]).card_signup).deliver | |
User.find(params[:user]).me_topics << MeTopic.all | |
else | |
Notifier.card_signup_email(current_user.card_signup).deliver | |
current_user.me_topics << MeTopic.all | |
end | |
if @card_signup.organization_id? && Organization.exists?(@card_signup.organization_id) | |
org_name = Organization.find(@card_signup.organization_id).name | |
org_id = @card_signup.organization_id | |
email = Organization.find(@card_signup.organization_id).users.first.email | |
Notifier.referral_reward(org_name, org_id, email).deliver | |
end | |
end | |
else | |
respond_to do |wants| | |
wants.html {} | |
wants.json { render :json => { :errors => @card_signup.errors, :html => (render_to_string :partial => 'final_form') } } | |
end | |
end | |
end | |
def create | |
@card_signup ||= params[:card_signup] | |
if User.exists?(params[:user]) && current_user.admin | |
@card_signup = User.find(params[:user]).build_card_signup(params[:card_signup]) | |
else | |
@card_signup = current_user.build_card_signup(params[:card_signup]) | |
end | |
if @card_signup.valid? | |
end | |
respond_to do |wants| | |
wants.json { render :json => {:card_signup => @card_signup, :html => (render_to_string :partial => 'social') } } | |
end | |
else | |
respond_to do |wants| | |
wants.json { render :json => {:errors => @card_signup.errors} } | |
end | |
end | |
end | |
def edit | |
respond_to do |wants| | |
wants.json { render :json => { :html => (render_to_string :partial => 'form') } } | |
wants.html { render :layout => 'account' } | |
end | |
end | |
def update | |
params[:card_signup][:dob] = @card_signup.dob | |
params[:card_signup][:how_did_you_hear] = @card_signup.how_did_you_hear | |
if @card_signup.update_attributes(params[:card_signup]) | |
respond_to do |wants| | |
wants.html { redirect_to edit_card_signup_path(@card_signup) } | |
wants.json { render :json => { :html => (render_to_string :partial => 'printing_status') } } | |
end | |
else | |
respond_to do |wants| | |
wants.json { logger.info @card_signup.errors.inspect } | |
wants.html {render :action => 'edit', :layout => 'account'} | |
end | |
end | |
end | |
def destroy | |
@card_signup = CardSignup.find(params[:id]) | |
if @card_signup.organization_id != nil | |
@organization = Organization.find(@card_signup.organization_id) | |
@organization.update_credits | |
end | |
@card_signup.destroy | |
respond_to do |wants| | |
wants.js { render :nothing => true } | |
end | |
end | |
def skip_to_end | |
@card_signup ||= CardSignup.new(params[:card_signup]) | |
respond_to do |wants| | |
wants.html { redirect_to new_card_signup_path } | |
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/new_form') } } | |
end | |
end | |
def go_to_disclaimer | |
@card_signup ||= CardSignup.new(params[:card_signup]) | |
respond_to do |wants| | |
wants.html { redirect_to new_card_signup_path } | |
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/disclaimer') } } | |
end | |
end | |
def go_to_final_form | |
@card_signup ||= CardSignup.new(params[:card_signup]) | |
2.times{@current_user.gift_conversions << GiftConversion.new } | |
respond_to do |wants| | |
wants.html { redirect_to new_card_signup_path } | |
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/final_form') } } | |
end | |
end | |
def skip_to_social | |
@card_signup ||= CardSignup.new(params[:card_signup]) | |
2.times{@card_signup.user.gift_conversions << GiftConversion.new} | |
respond_to do |wants| | |
wants.html { redirect_to new_card_signup_path } | |
wants.json { render :json => { :html => (render_to_string :partial => '/card_signups/social') } } | |
end | |
end | |
def print_temp_card | |
options = {:page_layout=>:landscape} | |
["left","right","top","bottom"].each do |side| | |
options[(side + '_margin').to_sym] = 36 | |
end | |
prawnto :inline=>true, :prawn=>options, :filename => "temp_card.pdf" | |
end | |
def rewards | |
render :layout => 'account' | |
end | |
end | |
def print_coupon | |
@used_reward = (@gifts + @conversions).first | |
@org = Organization.find(params[:org]) | |
if @unused_orgs.include?(@org) && (@conversions.count + @gifts.count) > 0 | |
@used_reward.update_attribute("referred_organization_id", params[:org]) | |
@used_reward.update_attribute("used_referral_reward_at", DateTime.now) | |
Notifier.org_reward_used_message(@org, @user).deliver | |
options = {:page_layout=>:landscape, :page_size => [400, 700]} | |
["left","right","top","bottom"].each do |side| | |
options[(side + '_margin').to_sym] = 36 | |
end | |
prawnto :inline=> true, :prawn=> options, :filename => "HQCard_Gift.pdf" | |
else | |
redirect_to root_url | |
end | |
end | |
def redeem_facebook | |
@card_signup = current_user.card_signup | |
@card_signup.redeem_facebook! | |
end | |
private | |
def get_rewards | |
@conversions = @card_signup.user.conversions.find(:all, :conditions => ["used_referral_reward_at IS ? OR used_referral_reward_at < ?", nil, (Date.today - 1.years)]) | |
@gifts = @card_signup.user.gift_conversions.find(:all, :conditions => ["used_referral_reward_at IS ? OR used_referral_reward_at < ?", nil, (Date.today - 1.years)]) | |
@ref_orgs = Organization.find(:all, :conditions => ["offering_referral = ?", true]) | |
@soiled_converts = @card_signup.user.conversions.find(:all, :conditions => ["used_referral_reward_at > ?", (Date.today - 1.years)]) | |
@soiled_gifts = @card_signup.user.gift_conversions.find(:all, :conditions => ["used_referral_reward_at > ?", (Date.today - 1.years)]) | |
@unused_orgs = (@ref_orgs - @soiled_converts.collect{|e|Organization.find(e.referred_organization_id)} - @soiled_gifts.collect{|sg|Organization.find(sg.referred_organization_id)}).sort{|a,b| b.offering_referral_timestamp <=> a.offering_referral_timestamp} | |
end | |
def require_unique | |
begin | |
@unique_user = current_user.card_signup.id == params[:id].to_i | |
@unique_user = CardSignup.find(params[:id]) if @unique_user.blank? && current_user.admin | |
rescue | |
@unique_user = CardSignup.find(params[:id]) if current_user.admin | |
end | |
redirect_to root_path unless @unique_user | |
end | |
def get_card_user | |
if params[:org] | |
@user = User.find(params[:id]) | |
@card_signup = @user.card_signup | |
elsif current_user.admin | |
@user = CardSignup.find(params[:id]).user | |
@card_signup = CardSignup.find(params[:id]) | |
else | |
@user = current_user | |
@card_signup = current_user.card_signup | |
end | |
redirect_to login_url unless @card_signup | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment