kaka-ruto / carpitan

Power to the People
MIT License
79 stars 32 forks source link

Add businesses affiliated to politicians #25

Open kaka-ruto opened 1 week ago

kaka-ruto commented 1 week ago

We are already capturing businesses owned by politicians, we want to also capture those they're affiliated to, either those they co own or those their close family members own

Proposal(feel free to do it any other way)

  1. Create a new model called Affiliation that represents the relationship between a politician and a business. This model will act as a join table between the Politician and Business models.
class Affiliation < ApplicationRecord
  belongs_to :politician
  belongs_to :business

  enum relationship: { owned: 0, family_owned: 1, co_owned: 2 }
end

The relationship enum represents the different types of affiliations a politician can have with a business. The possible values can be:

  1. Update the Politician model to establish the many-to-many relationship with the Business model through the Affiliation model.
class Politician < ApplicationRecord
  has_many :affiliations
  has_many :businesses, through: :affiliations
end
  1. Update the forms of creating a business to capture the said politician, then to capture the business details and affiliation relationship enum

The last part can look something like this

 <div>
    <%= form.label :relationship %>
    <%= form.select :relationship, Affiliation.relationships.keys.map { |r| [r.humanize, r] }, prompt: 'Select Relationship' %>
  </div>
  1. Update the Business model to establish the many-to-many relationship with the Politician model through the Affiliation model.
class Business < ApplicationRecord
  has_many :affiliations
  has_many :politicians, through: :affiliations
end
  1. On each politician's page, show all businesses according to their affiliations, eg (rough example, so edit according to what we currently have, maintaining our current style, or making it better)

Politician Show View (app/views/politicians/show.html.erb):

<h1><%= @politician.name %></h1>

<h2>Owned Businesses</h2>
<ul>
  <% @politician.affiliations.where(relationship: :owned).each do |affiliation| %>
    <li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
  <% end %>
</ul>

<h2>Family Owned Businesses</h2>
<ul>
  <% @politician.affiliations.where(relationship: :family_owned).each do |affiliation| %>
    <li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
  <% end %>
</ul>

<h2>Co-owned Businesses</h2>
<ul>
  <% @politician.affiliations.where(relationship: :co_owned).each do |affiliation| %>
    <li><%= link_to affiliation.business.name, business_path(affiliation.business) %></li>
  <% end %>
</ul>
  1. In each business page as well, show the affiliations of that business

Business Show View (app/views/businesses/show.html.erb - again just a rough example, make the styling better):

<h1><%= @business.name %></h1>

<h2>Affiliated Politicians</h2>
<ul>
  <% @business.affiliations.each do |affiliation| %>
    <li>
      <%= link_to affiliation.politician.name, politician_path(affiliation.politician) %>
      (<%= affiliation.relationship %>)
    </li>
  <% end %>
</ul>
  1. Write a rake task to update all existing businesses to create a new affiliation for each, with the relationship being :owned since all current businesses are owned by said politicians