Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2. has_many :participations, foreign_key: :participant_id
  3. has_many :owned_tasks, class_name: "Task", foreign_key: :owner_id
  4. end
  5.  
  6. class Participation < ActiveRecord::Base
  7. enum status: [:interested, :selected]
  8.  
  9. belongs_to :task
  10. belongs_to :participant, class_name: "User"
  11. end
  12.  
  13. class Task < ActiveRecord::Base
  14. enum status: [:open, :in_progress, :complete]
  15.  
  16. has_many :participations
  17. has_many :participants, through: :participations, source: :participant
  18. # Dynamically generates relations such as 'selected_participants'
  19. Participation.statuses.keys.each do |status|
  20. has_many "#{status}_participants".to_sym,
  21. -> { where(participations: { status: status.to_sym }) },
  22. through: :participations,
  23. source: :participant
  24. end
  25.  
  26. belongs_to :owner, class_name: "User"
  27. end
  28.  
  29. user = User.first
  30. task = Task.first
  31. user.owned_tasks << task
  32.  
  33. user_2 = User.find(2)
  34. task.participants << user_2
  35.  
  36. <% unless current_user == @task.owner %>
  37. <div class="volunteer-form">
  38. <% if current_user.is_participating? %>
  39. <%= render 'cancel' %>
  40. <% else %>
  41. <%= render 'volunteer' %>
  42. <% end %>
  43. </div>
  44. <% end %>
  45.  
  46. <%= form_for(current_user.participations.build(participant_id: current_user), remote: true) do |f| %>
  47. <div><%= f.hidden_field :participant_id %></div>
  48. <%= f.submit "Volunteer" %>
  49. <% end %>
  50.  
  51. <%= form_for(current_user.participations.find_by(participant_id: current_user), html: { method: :delete }, remote: true) do |f| %>
  52. <%= f.submit "Cancel" %>
  53. <% end %>
  54.  
  55. // create.js.erb
  56. $(".volunteer-form").html("<%= escape_javascript(render('tasks/volunteer')) %>");
  57.  
  58. // destroy.js.erb
  59. $(".volunteer-form").html("<%= escape_javascript(render('tasks/cancel')) %>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement