Sha256: 57d6d7c65922ee248cb0e4a7eec4bac04821acbd9e08a6a6117de1091b298362

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

class PostsController < ApplicationController
  prepend_before_filter :load_resource

  embedded_association :comments => {:user => :account}
  embedded_association :user => [:account]
  embedded_association [:tags]
  embedded_association :category

  attr_accessor :resource

  def create
    params = post_params
    handle_embedded_associations(resource, params)
    resource.update_attributes(params)
    render json: resource
  end

  def update
    params = post_params
    handle_embedded_associations(resource, params)
    resource.update_attributes(params)
    render json: resource
  end

  def destroy
    resource.destroy
    render nothing: true
  end

  protected

  def load_resource
    self.resource = @post = if params['action'] == 'create'
      Post.new
    else
      Post.find(params[:id])
    end
  end

  def resource_name
    'post'
  end

  def post_params
    params.require(:post).permit(
      :title,
      comments: [:content, user: [:name, :email, account: [:note] ]],
      user: [:type, :name, :email, account: [:note] ],
      tags: [:name],
      category: [:name]
    )
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
embedded_associations-4.1.1 spec/support/app/app/controllers/posts_controller.rb