Last active
December 23, 2015 09:40
-
-
Save ozgun/1016bd283b735769826e to your computer and use it in GitHub Desktop.
Nested conrtollers example
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
class Admin::AppsController < Admin::BaseController | |
before_filter :load_app | |
def index | |
@apps = App.all | |
end | |
def show | |
load_app | |
end | |
def edit | |
load_app | |
end | |
protected | |
def load_app | |
@app = App.find(params[:id]) | |
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
class Admin::BaseController < ::ApplicationController | |
layout "admin" | |
before_filter :authenticate_user! | |
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
class Admin::Apps::BaseController < Admin::BaseController | |
before_filter :find_app | |
helper_method :selected_app | |
protected | |
def find_app | |
@selected_app = App.find(params[:app_id]) | |
end | |
def selected_app | |
@selected_app | |
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
class Admin::FreeSubscriptionsController < Admin::Apps::BaseController | |
before_filter :load_products_and_users | |
def new | |
@subscription = build_subscription | |
end | |
def create | |
product = load_product | |
user = load_user | |
service = Payments::CreateFreeSubscription.new(user, selected_app, product) | |
result = service.call | |
if result.success? | |
redirect_to admin_subscriptions_path | |
else | |
flash.now[:alert] = result.error | |
@subscription = result.error_data || build_subscription(subscription_params) | |
render 'new' | |
end | |
end | |
protected | |
def load_products_and_users | |
load_products | |
load_users | |
end | |
def load_products | |
@products = product_scope.pluck(:name, :id) | |
end | |
def load_product | |
product_scope.find(params[:subscription][:product_id]) | |
end | |
def product_scope | |
Product.free.web | |
end | |
def load_users | |
@users = app_managers.map{ |u| [u.email, u.id] } | |
end | |
def load_user | |
user = app_managers.detect{|u| u.id == params[:subscription][:user_id].to_i } | |
raise ActiveRecord::RecordNotFound unless user | |
user | |
end | |
def app_managers | |
selected_app.app_managers_with_active_email | |
end | |
def build_subscription(attrs = {}) | |
Subscription.new(attrs) | |
end | |
def subscription_params | |
params.require(:subscription).permit(:product_id, :user_id) | |
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
<h2><%= t('subscription.create_free_subscription') %></h2> | |
<h3><%= link_to @app.name, admin_app_path(selected_app) %></h3> | |
<%= simple_form_for @subscription, url: admin_app_free_subscriptions_path(selected_app) do |f| %> | |
<%= f.input :user_id, collection: @users %> | |
<%= f.input :product_id, collection: @products %> | |
<%= f.submit %> | |
<% 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
class Admin::PagesController < Admin::Apps::BaseController | |
def index | |
@pages = page_scope | |
end | |
def show | |
@page = page_scope.find(params[:id]) | |
end | |
protected | |
def page_scope | |
selected_app.pages | |
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
class Admin::Apps::PaymentStatusController < Admin::Apps::BaseController | |
# url: /apps/1/payment_status | |
# Controller file: app/controllers/apps/payment_status_controller.rb | |
# View file: app/views/apps/payment_status/show.html.erb | |
def show | |
@payment_status = @app.payment_status | |
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
Rails.application.routes.draw do | |
namespace :admin, constraints: { host: Rails.application.secrets.domain } do | |
resources :apps do | |
resources :free_subscriptions, only: [:new, :create, :index] | |
resources :pages | |
resource :payment_status, controller: 'payment_status', module: 'apps' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dosya/klasor yapisi da su sekilde: