module CollectionController

Constants

VERSION

Protected Instance Methods

build_member(attrs = nil) click to toggle source
# File lib/collection_controller.rb, line 27
def build_member(attrs = nil)
  @member = begin
    if member_is_one?
      collection.send(controller_name.singularize) || collection.send("build_#{controller_name.singularize}")
    else
      collection.respond_to?(:new) ? collection.new(attrs) : collection.build(attrs)
    end
  end
end
collection() click to toggle source
# File lib/collection_controller.rb, line 8
def collection
  @collection ||= controller_name.classify.constantize
end
collection=(new_value) click to toggle source
# File lib/collection_controller.rb, line 53
def collection=(new_value)
  @collection = new_value
end
current_page() click to toggle source
# File lib/collection_controller.rb, line 57
def current_page
  collection.page(page_number)
end
find_member(id) click to toggle source
# File lib/collection_controller.rb, line 23
def find_member(id)
  @member = collection.find(id)
end
member() click to toggle source
# File lib/collection_controller.rb, line 12
def member
  @member ||= begin
    if member_is_one?
      model_name = controller_name.singularize
      collection.send(model_name) || collection.send("build_#{model_name}")
    else
      params[:id] ? find_member(params[:id]) : build_member
    end
  end
end
member=(new_value) click to toggle source
# File lib/collection_controller.rb, line 49
def member=(new_value)
  @member = new_value
end
member_association() click to toggle source
# File lib/collection_controller.rb, line 37
def member_association
  @_member_association ||= begin
    if collection.class.respond_to?(:reflect_on_association)
      collection.class.reflect_on_association(controller_name.singularize)&.macro
    end
  end
end
member_is_one?() click to toggle source
# File lib/collection_controller.rb, line 45
def member_is_one?
  member_association == :has_one
end
page_number() click to toggle source
# File lib/collection_controller.rb, line 61
def page_number
  params[:page] ? params[:page][:number] : 1
end