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

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

View File

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

View File

@@ -156,6 +156,10 @@ Rails.application.routes.draw do
resource :verification, controller: "verification", only: [:show] resource :verification, controller: "verification", only: [:show]
resources :communities, only: [:show] do
resources :topics
end
scope module: :verification do scope module: :verification do
resource :residence, controller: "residence", only: [:new, :create] resource :residence, controller: "residence", only: [:new, :create]
resource :sms, controller: "sms", only: [:new, :create, :edit, :update] resource :sms, controller: "sms", only: [:new, :create, :edit, :update]
@@ -164,6 +168,7 @@ Rails.application.routes.draw do
resource :letter, controller: "letter", only: [:new, :create, :show, :edit, :update] resource :letter, controller: "letter", only: [:new, :create, :show, :edit, :update]
end end
namespace :admin do namespace :admin do
root to: "dashboard#index" root to: "dashboard#index"
resources :organizations, only: :index do resources :organizations, only: :index do

View File

@@ -0,0 +1,7 @@
class CreateCommunity < ActiveRecord::Migration
def change
create_table :communities do |t|
t.timestamps null: false
end
end
end

View File

@@ -0,0 +1,5 @@
class AddCommunityToProposal < ActiveRecord::Migration
def change
add_reference :proposals, :community, index: true, foreign_key: true
end
end

View File

@@ -0,0 +1,9 @@
class CreateTopics < ActiveRecord::Migration
def change
create_table :topics do |t|
t.string :title, null: false
t.integer :author_id
t.references :community, index: true
end
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170720092638) do ActiveRecord::Schema.define(version: 20170807082243) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -236,6 +236,11 @@ ActiveRecord::Schema.define(version: 20170720092638) do
add_index "comments", ["hidden_at"], name: "index_comments_on_hidden_at", using: :btree add_index "comments", ["hidden_at"], name: "index_comments_on_hidden_at", using: :btree
add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
create_table "communities", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "debates", force: :cascade do |t| create_table "debates", force: :cascade do |t|
t.string "title", limit: 80 t.string "title", limit: 80
t.text "description" t.text "description"
@@ -749,11 +754,13 @@ ActiveRecord::Schema.define(version: 20170720092638) do
t.datetime "retired_at" t.datetime "retired_at"
t.string "retired_reason" t.string "retired_reason"
t.text "retired_explanation" t.text "retired_explanation"
t.integer "community_id"
end end
add_index "proposals", ["author_id", "hidden_at"], name: "index_proposals_on_author_id_and_hidden_at", using: :btree add_index "proposals", ["author_id", "hidden_at"], name: "index_proposals_on_author_id_and_hidden_at", using: :btree
add_index "proposals", ["author_id"], name: "index_proposals_on_author_id", using: :btree add_index "proposals", ["author_id"], name: "index_proposals_on_author_id", using: :btree
add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree
add_index "proposals", ["community_id"], name: "index_proposals_on_community_id", using: :btree
add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree
add_index "proposals", ["geozone_id"], name: "index_proposals_on_geozone_id", using: :btree add_index "proposals", ["geozone_id"], name: "index_proposals_on_geozone_id", using: :btree
add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree
@@ -883,6 +890,14 @@ ActiveRecord::Schema.define(version: 20170720092638) do
add_index "tags", ["proposals_count"], name: "index_tags_on_proposals_count", using: :btree add_index "tags", ["proposals_count"], name: "index_tags_on_proposals_count", using: :btree
add_index "tags", ["spending_proposals_count"], name: "index_tags_on_spending_proposals_count", using: :btree add_index "tags", ["spending_proposals_count"], name: "index_tags_on_spending_proposals_count", using: :btree
create_table "topics", force: :cascade do |t|
t.string "title", null: false
t.integer "author_id"
t.integer "community_id"
end
add_index "topics", ["community_id"], name: "index_topics_on_community_id", using: :btree
create_table "users", force: :cascade do |t| create_table "users", force: :cascade do |t|
t.string "email", default: "" t.string "email", default: ""
t.string "encrypted_password", default: "", null: false t.string "encrypted_password", default: "", null: false
@@ -936,7 +951,7 @@ ActiveRecord::Schema.define(version: 20170720092638) do
t.boolean "email_digest", default: true t.boolean "email_digest", default: true
t.boolean "email_on_direct_message", default: true t.boolean "email_on_direct_message", default: true
t.boolean "official_position_badge", default: false t.boolean "official_position_badge", default: false
t.datetime "password_changed_at", default: '2017-06-22 11:21:30', null: false t.datetime "password_changed_at", default: '2017-08-07 08:24:24', null: false
t.boolean "created_from_signature", default: false t.boolean "created_from_signature", default: false
t.integer "failed_email_digests_count", default: 0 t.integer "failed_email_digests_count", default: 0
t.text "former_users_data_log", default: "" t.text "former_users_data_log", default: ""
@@ -1062,6 +1077,7 @@ ActiveRecord::Schema.define(version: 20170720092638) do
add_foreign_key "poll_voters", "polls" add_foreign_key "poll_voters", "polls"
add_foreign_key "poll_white_results", "poll_booth_assignments", column: "booth_assignment_id" add_foreign_key "poll_white_results", "poll_booth_assignments", column: "booth_assignment_id"
add_foreign_key "poll_white_results", "poll_officer_assignments", column: "officer_assignment_id" add_foreign_key "poll_white_results", "poll_officer_assignments", column: "officer_assignment_id"
add_foreign_key "proposals", "communities"
add_foreign_key "users", "geozones" add_foreign_key "users", "geozones"
add_foreign_key "valuators", "users" add_foreign_key "valuators", "users"
end end