Skip to content

Instantly share code, notes, and snippets.

@ozgun

ozgun/apps_controller.rb

Last active Dec 23, 2015
Embed
What would you like to do?
Nested conrtollers example
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
class Admin::BaseController < ::ApplicationController
layout "admin"
before_filter :authenticate_user!
end
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
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
<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 %>
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
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
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
@ozgun

This comment has been minimized.

Copy link
Owner Author

@ozgun ozgun commented Dec 22, 2015

Dosya/klasor yapisi da su sekilde:

apps/controllers/admin/apps/base_controller.rb
apps/controllers/admin/free_subscriptions_controller.rb

apps/views/admin/free_subscriptions/new.html.erb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment