Sha256: 0643fc5b0865da0ec0841cf73bd12be8fcd97e594eacaef6b3c8827b661d17d3

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 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: [:name, :email, account: [:note] ],
      tags: [:name],
      category: [:name]
    )
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

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