Skip to content

Instantly share code, notes, and snippets.

@VictorTpo
Last active May 15, 2017 18:05
Show Gist options
  • Save VictorTpo/d9e948dfcdad84418826d9905a5a6bb5 to your computer and use it in GitHub Desktop.
Save VictorTpo/d9e948dfcdad84418826d9905a5a6bb5 to your computer and use it in GitHub Desktop.

Projects Controllers

Client side

2 kinds of controllers :

  • basic (for project with only two fields [name and descritpion]
  • classic (with specific fields)

In both sides showis the same method, so we code it in a common module.
But new, create, edit, update are not the same ('cause they don't have the same fields). So we create 2 different modules.

# basic controller
# app/controllers/projects/uncategorizeds_controller.rb
module Projects
  class UncategorizedsController < ApplicationController
    include Projects::Actions::Common
    include Projects::Actions::BasicUpdatable
  end
end
# classic controller
# app/controllers/projects/event_sponsors_controller.rb
module Projects
  class EventSponsorsController < ApplicationController
    include Projects::Actions::Common
    include Projects::Actions::Updatable
  end
end

Concerns:

# app/controllers/concerns/projects/actions/common.rb
module Projects
  module Actions
    module Common
    extend ActiveSupport::Concern
		
    def show
      @project = find_project
    end
  end
end
# app/controllers/concerns/projects/actions/basic_updatable.rb
module Projects
  module Actions
    module BasicUpdatable
      extend ActiveSupport::Concern
		
      def new
        new_project
      end
    end
  end
end
# app/controllers/concerns/projects/actions/updatable.rb
module Projects
  module Actions
    module Updatable
      extend ActiveSupport::Concern
		
      def new
        new_project
        form_data
      end
    end
  end
end

Admin side

aka ciacia Active Admin

It's almost the same !

For the moment, all the project forms are managed by AA, but we want to go away from it ! A lot of actions are in AA controller (app/admin/projects/event_sponsors.rb).

For basic projects all the logic are in AA controller :

# app/admin/projects/uncategorizeds.rb
ActiveAdmin.register_page 'Project_Uncategorizeds' do
  include Admin::Projects::Buttons
  include Admin::Projects::Actions::Common
  include Admin::Projects::Actions::BasicUpdatable
end
module Admin
  module Projects
    module Actions
      module BasicUpdatable
        extend ActiveSupport::Concern

        def self.included(base)
          base.instance_eval do
            controller do
              layout 'active_admin'

              def new
                new_project
              end
            end
          end
        end
      end
    end
  end
end

But for classic projects actions can be in AA controllers or classic controllers :

  • project form are in classic controllers
  • everything else in AA controllers
But all action link to the form are now in classic controller !
# app/admin/projects/event_sponsors.rb
ActiveAdmin.register_page 'Project_Event_Sponsors' do
  include Admin::Projects::Buttons
  include Admin::Projects::Actions::Common

  controller do
    def edit
      redirect_to edit_admin_projects_event_sponsor_path
    end
  end
end
# app/controllers/admin/projects/event_sponsors_controller.rb
module Admin
  module Projects
    class EventSponsorsController < ApplicationController
      include Projects::Actions::Updatable
    end
  end
end
# app/controllers/concerns/admin/projects/actions/updatable.rb
module Admin
  module Projects
    module Actions
      module Updatable
        extend ActiveSupport::Concern

        def new
          new_project
          form_data
        end
      end
    end
  end
end

To resume :

# here, not in alpahbetical order !
|-app
  |-admin
    |-projects
  |-controllers
    |-projects
    |-admin
      |-projects
    |-concerns
      |-projects
      |-admin
        |-projects
    	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment