Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < ActiveRecord::Base
- has_many :participations, foreign_key: :participant_id
- has_many :owned_tasks, class_name: "Task", foreign_key: :owner_id
- end
- class Participation < ActiveRecord::Base
- enum status: [:interested, :selected]
- belongs_to :task
- belongs_to :participant, class_name: "User"
- end
- class Task < ActiveRecord::Base
- enum status: [:open, :in_progress, :complete]
- has_many :participations
- has_many :participants, through: :participations, source: :participant
- # Dynamically generates relations such as 'selected_participants'
- Participation.statuses.keys.each do |status|
- has_many "#{status}_participants".to_sym,
- -> { where(participations: { status: status.to_sym }) },
- through: :participations,
- source: :participant
- end
- belongs_to :owner, class_name: "User"
- end
- user = User.first
- task = Task.first
- user.owned_tasks << task
- user_2 = User.find(2)
- task.participants << user_2
- <% unless current_user == @task.owner %>
- <div class="volunteer-form">
- <% if current_user.is_participating? %>
- <%= render 'cancel' %>
- <% else %>
- <%= render 'volunteer' %>
- <% end %>
- </div>
- <% end %>
- <%= form_for(current_user.participations.build(participant_id: current_user), remote: true) do |f| %>
- <div><%= f.hidden_field :participant_id %></div>
- <%= f.submit "Volunteer" %>
- <% end %>
- <%= form_for(current_user.participations.find_by(participant_id: current_user), html: { method: :delete }, remote: true) do |f| %>
- <%= f.submit "Cancel" %>
- <% end %>
- // create.js.erb
- $(".volunteer-form").html("<%= escape_javascript(render('tasks/volunteer')) %>");
- // destroy.js.erb
- $(".volunteer-form").html("<%= escape_javascript(render('tasks/cancel')) %>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement