module NippoCore class GroupsController < ApplicationController before_action :initialize_group, only: [:new, :create] before_action :find_group, only: [:show, :request_join] def index @groups = NippoCore::Group.order(created_at: :desc).page(params[:page]).per(10) end def new end def show @reports = @group.reports.order(reported_at: :desc).limit(5) @requests = @group.unaccepted_requests.limit(5) @members = @group.members end def create if @group.save current_user.add_member(current_user, @group) redirect_to root_path else render 'new', status: 400 end end def request_join result = current_user.send_request(@group) render layout: nil end private def initialize_group @group = NippoCore::Group.new(group_params.merge(creator: current_user)) end def group_params params.fetch(:group, {}).permit(:name, :description) end end end