module Theblog::Itemable

Public Instance Methods

new() click to toggle source

actions

# File lib/theblog/concerns/controllers/itemable.rb, line 18
def new
  @item = model.new
end
update() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 22
def update
  if item.update(permitted_params)
    flash[:notice] = "Item updated"
    redirect_to action: :show
  else
    render 'new'
  end
end

Private Instance Methods

index_fields() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 71
        def index_fields
  self.class::INDEX
end
item() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 46
        def item
  @item ||= if params.has_key?("#{model_params_key}_id")
              model.find(params["#{model_params_key}_id"])
            else
              model.find(params[:id])
            end
end
items() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 54
        def items
  @items ||= model.page params[:page]
end
model() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 42
        def model
  self.class::MODEL
end
model_association_param_keys() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 58
        def model_association_param_keys
  model_associations.map do |association|
    reflection = model.reflections[association.to_s]
    key = reflection.foreign_key
    key = {key.pluralize => []} unless reflection.is_a? ActiveRecord::Reflection::BelongsToReflection
    key
  end
end
model_associations() click to toggle source

helpers

# File lib/theblog/concerns/controllers/itemable.rb, line 32
        def model_associations
  self.class::ASSOCIATIONS
rescue
  []
end
model_params() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 38
        def model_params
  self.class::ATTRIBUTES
end
model_params_key() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 67
        def model_params_key
  model.to_s.underscore.match(/[\w_]+$/).to_s
end
permitted_params() click to toggle source
# File lib/theblog/concerns/controllers/itemable.rb, line 75
        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