Class: Tellimus::SubscriptionsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ApplicationController
- Tellimus::SubscriptionsController
- Defined in:
- app/controllers/tellimus/subscriptions_controller.rb
Instance Method Summary collapse
- #cancel ⇒ Object
- #create ⇒ Object
- #current_owned_through_or_by ⇒ Object
-
#current_owner ⇒ Object
the following three methods allow us to show the pricing table before someone has an account.
- #edit ⇒ Object
- #index ⇒ Object
- #load_owner ⇒ Object
- #load_plans ⇒ Object
- #load_subscription ⇒ Object
- #new ⇒ Object
- #no_owner? ⇒ Boolean
- #redirect_to_sign_up ⇒ Object
- #show ⇒ Object
- #show_existing_subscription ⇒ Object
- #unauthorized ⇒ Object
- #update ⇒ Object
Instance Method Details
#cancel ⇒ Object
142 143 144 145 146 147 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 142 def cancel flash[:notice] = I18n.t('tellimus.confirmations.subscription_cancelled') @subscription.plan_id = nil @subscription.save redirect_to owner_subscription_path(@owner, @subscription) end |
#create ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 124 def create @subscription = ::Subscription.new(subscription_params) @subscription.payment_method_nonce = params[:payment_method_nonce] @subscription.subscription_owner = @owner if @subscription.save flash[:notice] = redirect_to after_new_subscription_path else flash[:error] = I18n.t('tellimus.failure.problem_processing_transaction') render :new end end |
#current_owned_through_or_by ⇒ Object
67 68 69 70 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 67 def current_owned_through_or_by # e.g. "self.current_user" send "current_#{Tellimus.subscriptions_owned_through_or_by}" end |
#current_owner ⇒ Object
the following three methods allow us to show the pricing table before someone has an account. by default these support devise, but they can be overriden to support others.
62 63 64 65 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 62 def current_owner # e.g. "self.current_user" send "current_#{Tellimus.subscriptions_owned_by}" end |
#edit ⇒ Object
149 150 151 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 149 def edit @braintree_client_token = Tellimus.gateway.client_token.generate end |
#index ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 79 def index # don't bother showing the index if they've already got a subscription. if current_owner and current_owner.subscription.present? redirect_to tellimus.edit_owner_subscription_path(current_owner, current_owner.subscription) end # Don't prep a subscription unless a user is authenticated. unless no_owner? # we should also set the owner of the subscription here. @subscription = ::Subscription.new({Tellimus.owner_id_sym => @owner.id}) @subscription.subscription_owner = @owner end end |
#load_owner ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 17 def load_owner unless params[:owner_id].nil? if current_owner.present? # we need to try and look this owner up via the find method so that we're # taking advantage of any override of the find method that would be provided # by older versions of friendly_id. (support for newer versions default behavior # below.) searched_owner = current_owner.class.find(params[:owner_id]) rescue nil # if we couldn't find them that way, check whether there is a new version of # friendly_id in place that we can use to look them up by their slug. # in christoph's words, "why?!" in my words, "warum?!!!" # (we debugged this together on skype.) if searched_owner.nil? && current_owner.class.respond_to?(:friendly) searched_owner = current_owner.class.friendly.find(params[:owner_id]) rescue nil end if current_owner.try(:id) == searched_owner.try(:id) @owner = current_owner else return end else return end end end |
#load_plans ⇒ Object
8 9 10 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 8 def load_plans @plans = ::Plan.order(:display_order) end |
#load_subscription ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 51 def load_subscription ownership_attribute = :"#{Tellimus.subscriptions_owned_by}_id" @subscription = ::Subscription.where(ownership_attribute => current_owner.id).find_by_id(params[:id]) # also, if cancan methods are available, we should use that to authorize. return @subscription.present? ? @subscription : end |
#new ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 95 def new if no_owner? if defined?(Devise) # by default these methods support devise. if current_owner redirect_to new_owner_subscription_path(current_owner, plan: params[:plan]) else redirect_to_sign_up end else raise I18n.t('tellimus.failure.feature_depends_on_devise') end @braintree_client_token = Tellimus.gateway.client_token.generate else @subscription = ::Subscription.new @subscription.plan = ::Plan.find(params[:plan]) @braintree_client_token = Tellimus.gateway.client_token.generate end end |
#no_owner? ⇒ Boolean
47 48 49 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 47 def no_owner? @owner.nil? end |
#redirect_to_sign_up ⇒ Object
72 73 74 75 76 77 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 72 def redirect_to_sign_up # this is a Devise default variable and thus should not change its name # when we change subscription owners from :user to :company session["#{Tellimus.subscriptions_owned_through_or_by}_return_to"] = new_subscription_path(plan: params[:plan]) redirect_to new_registration_path(Tellimus.subscriptions_owned_through_or_by.to_s) end |
#show ⇒ Object
139 140 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 139 def show end |
#show_existing_subscription ⇒ Object
118 119 120 121 122 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 118 def show_existing_subscription if @owner.subscription.present? redirect_to owner_subscription_path(@owner, @owner.subscription) end end |
#unauthorized ⇒ Object
12 13 14 15 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 12 def render status: 401, template: "tellimus/subscriptions/unauthorized" false end |
#update ⇒ Object
153 154 155 156 157 158 159 160 161 162 |
# File 'app/controllers/tellimus/subscriptions_controller.rb', line 153 def update @subscription.payment_method_nonce = params[:payment_method_nonce] if @subscription.update(subscription_params) flash[:notice] = I18n.t('tellimus.confirmations.subscription_updated') redirect_to owner_subscription_path(@owner, @subscription) else flash[:error] = I18n.t('tellimus.failure.problem_processing_transaction') render :edit end end |