Last active
August 29, 2015 14:03
-
-
Save davidlfox/69ed06aed06975137065 to your computer and use it in GitHub Desktop.
decorate associations with draper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chore < ActiveRecord::Base | |
belongs_to :user | |
has_many :taken_chore | |
scope :available, -> { where(:available => true) } | |
scope :ordered, -> { available.order(created_at: :desc) } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChoreDecorator < Draper::Decorator | |
delegate_all | |
decorates_association :taken_chores | |
def name | |
object.user.name | |
end | |
def zip | |
object.user.zip | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- @buyer_chores.each do |chore| | |
%li | |
= link_to chore.title, chore_path(chore) | |
| |
%span= chore.description | |
%ul | |
/ this line raises undefined_method exception on 'taken_chores' | |
- chore.taken_chores.each do |taken_chore| | |
%li | |
userid: | |
= taken_chore.taker_id | |
= link_to 'approve', :controller => :chores, :action => :complete, :id => chore.id |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TakenChore < ActiveRecord::Base | |
belongs_to :chore | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TakenChoreDecorator < Draper::Decorator | |
delegate_all | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def page | |
# chores belonging to user that they're paying for | |
@buyer_chores = Chore.includes(:taken_chore) | |
.joins('LEFT JOIN taken_chores ON taken_chores.chore_id = chores.id AND taken_chores.maker_approved is null AND taken_chores.taker_approved is not null') | |
.where(:user_id => current_user.id, :direction => 'buyer') | |
.order(:created_at) | |
.decorate | |
# chores belonging to user that they're being paid for | |
@seller_chores = Chore.includes(:taken_chore) | |
.joins('LEFT JOIN taken_chores ON taken_chores.chore_id = chores.id AND taken_chores.taker_approved is null AND taken_chores.maker_approved is not null') | |
.where(:user_id => current_user.id, :direction => 'seller') | |
.order(:created_at) | |
.decorate | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment