Add communities and topics

This commit is contained in:
taitus
2017-08-07 12:51:49 +02:00
parent ee3b3f2c85
commit 43c17c3fc7
20 changed files with 210 additions and 10 deletions

View File

@@ -0,0 +1,19 @@
class CommunitiesController < ApplicationController
before_action :set_community, :load_topics, only: :show
skip_authorization_check
def show
end
private
def set_community
@community = Community.find(params[:id])
end
def load_topics
@topics = @community.topics
end
end

View File

@@ -0,0 +1,47 @@
class TopicsController < ApplicationController
before_action :set_community
skip_authorization_check
def new
@topic = Topic.new
end
def create
@topic = Topic.new(topic_params.merge(author: current_user, community_id: params[:community_id]))
if @topic.save
redirect_to community_path(@community), notice: I18n.t('flash.actions.create.topic')
else
render :new
end
end
def show
@topic = Topic.find(params[:id])
end
def edit
@topic = Topic.find(params[:id])
end
def update
@topic = Topic.find(params[:id])
if @topic.update(topic_params)
redirect_to community_path(@community), notice: t('topic.update.notice')
else
render :edit
end
end
private
def topic_params
params.require(:topic).permit(:title, :community_id)
end
def set_community
@community = Community.find(params[:community_id])
end
end

6
app/models/community.rb Normal file
View File

@@ -0,0 +1,6 @@
class Community < ActiveRecord::Base
has_one :proposal
has_one :investment
has_many :topics
end

View File

@@ -23,6 +23,7 @@ class Proposal < ActiveRecord::Base
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
belongs_to :geozone
belongs_to :community
has_many :comments, as: :commentable
has_many :proposal_notifications
@@ -43,6 +44,7 @@ class Proposal < ActiveRecord::Base
before_validation :set_responsible_name
before_save :calculate_hot_score, :calculate_confidence_score
before_create :associate_community
scope :for_render, -> { includes(:tags) }
scope :sort_by_hot_score, -> { reorder(hot_score: :desc) }
@@ -180,6 +182,11 @@ class Proposal < ActiveRecord::Base
(voters + followers).uniq
end
def associate_community
community = Community.create
self.community_id = community.id
end
protected
def set_responsible_name

4
app/models/topic.rb Normal file
View File

@@ -0,0 +1,4 @@
class Topic < ActiveRecord::Base
belongs_to :community
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
end

View File

@@ -41,4 +41,3 @@
<div id="investments">
<%= render '/admin/budget_investments/investments' %>
</div>

View File

@@ -41,4 +41,4 @@
</tbody>
</table>
<%= paginate @budgets %>
<%= paginate @budgets %>

View File

@@ -0,0 +1,17 @@
Comunidad de usuarios
<br>
<%= @community.proposal.title %>
<br>
Participa en la comunidad de esta propuesta
<div class="small-12 medium-9 column">
<div id="topics">
<%= render "topics/topics", topics: @community.topics %>
</div>
</div>
<div class="small-12 medium-3 column">
Participa
<%= link_to t("topic.new"), new_community_topic_path(@community.id), class: 'button expanded' %>
</div>

View File

@@ -89,12 +89,12 @@
<aside class="margin-bottom">
<%= link_to t("proposals.index.start_proposal"), new_proposal_path, class: 'button expanded' %>
<% if params[:retired].blank? %>
<%= render 'categories' %>
<%= render "shared/tag_cloud", taggable: 'proposal' %>
<%= render 'geozones' %>
<%= render 'popular' %>
<% end %>
<%= render 'retired' %>
<%= render 'categories' %>
<%= render "shared/tag_cloud", taggable: 'proposal' %>
<%= render 'geozones' %>
<%= render 'popular' %>
<% end %>
<%= render 'retired' %>
</aside>
</div>

View File

@@ -158,6 +158,7 @@
<% if current_user %>
<%= render 'follows/follow_button', follow: find_or_build_follow(current_user, @proposal) %>
<% end %>
<%= link_to t("community.show.access"), community_path(@proposal.community_id), class: 'button expanded' %>
</aside>
</div>

View File

@@ -0,0 +1,15 @@
<%= form_for([@community, @topic]) do |f| %>
<%= render 'shared/errors', resource: @topic %>
<div class="row">
<div class="small-12 column">
<%= f.label :title, t("topic.form.topic_title") %>
<%= f.text_field :title %>
</div>
<div class="actions small-12 column">
<%= f.submit(class: "button", value: t("topic.#{action_name}.form.submit_button")) %>
</div>
</div>
<% end %>

View File

@@ -0,0 +1,16 @@
<div class="order">
Ordenar por:
</div>
<br>
<% if topics.any? %>
<% topics.each do |topic| %>
<div id="<%= dom_id(topic) %>">
<%= link_to topic.title, community_topic_path(@community, topic) %>
<% if topic.author == current_user %>
<%= link_to t("topic.edit"), edit_community_topic_path(@community.id, topic), class: 'button expanded' %>
<% end %>
</div>
<% end %>
<% else %>
Crea el primer tema de la comunidad!!!
<% end %>

View File

@@ -0,0 +1,10 @@
<div class="topic-form row">
<div class="small-12 column">
<%= render "shared/back_link" %>
<h1><%= t("topic.edit.editing") %></h1>
<%= render "form" %>
</div>
</div>

View File

@@ -0,0 +1,12 @@
<div class="topic-new row">
<div class="small-12 medium-9 column">
<h1><%= t("topic.create") %></h1>
<%= render '/topics/form' %>
<%#= render '/topics/form', form_url: budget_investments_path(@budget) %>
</div>
<div class="small-12 medium-9 column">
Recomendaciones para crear un tema
</div>
</div>

View File

@@ -0,0 +1,5 @@
<%= render "shared/back_link" %>
<br>
Comunidad: <%= @community.proposal.title %>
<br>
<%= @topic.title %>