Sha256: 0d69401366d48a9d67fc2a10abcb77d087f08776b1d3acf274a67b8d8b53172c

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Theblog
  class Admin::ItemsController < AdminController
    def new
      @item = model.new
    end

    def create
      if @item = model.create(permitted_params.merge({author: current_account}))
        flash[:notice] = "Item created"
        redirect_to action: :index
      else
        render 'new'
      end
    end

    def update
      if item.update(permitted_params)
        flash[:notice] = "Item updated"
        redirect_to action: :show
      else
        render 'new'
      end
    end

    private def item
      @item ||= model.find(params[:id])
    end
    helper_method :item

    private def items
      @items ||= model.all
    end
    helper_method :items

    private def model
      self.class::MODEL
    end
    helper_method :model

    private def model_params
      self.class::ATTRIBUTES
    end
    helper_method :model_params

    private def model_associations
      self.class::ASSOCIATIONS
    rescue
      []
    end
    helper_method :model_associations

    private def model_association_param_keys
      model_associations.map do |association|
        model.reflections[association.to_s].foreign_key
      end
    end

    private def permitted_params
      params.require(model_params_key).permit(*model_params.map{ |attr| attr.is_a?(Hash) ? attr.keys.first : attr }, *model_association_param_keys)
    end

    private def model_params_key
      model.to_s.underscore.match(/[\w_]+$/).to_s
    end

    private def index_fields
      self.class::INDEX
    end
    helper_method :index_fields
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
theblog-0.0.1.1 app/controllers/theblog/admin/items_controller.rb
theblog-0.0.1 app/controllers/theblog/admin/items_controller.rb