From 43c17c3fc76372972b9f513cbe16a0796cf09b66 Mon Sep 17 00:00:00 2001 From: taitus Date: Mon, 7 Aug 2017 12:51:49 +0200 Subject: [PATCH] Add communities and topics --- app/controllers/communities_controller.rb | 19 ++++++++ app/controllers/topics_controller.rb | 47 +++++++++++++++++++ app/models/community.rb | 6 +++ app/models/proposal.rb | 7 +++ app/models/topic.rb | 4 ++ .../admin/budget_investments/index.html.erb | 1 - app/views/admin/budgets/index.html.erb | 2 +- app/views/communities/show.html.erb | 17 +++++++ app/views/proposals/index.html.erb | 12 ++--- app/views/proposals/show.html.erb | 1 + app/views/topics/_form.html.erb | 15 ++++++ app/views/topics/_topics.html.erb | 16 +++++++ app/views/topics/edit.html.erb | 10 ++++ app/views/topics/new.html.erb | 12 +++++ app/views/topics/show.html.erb | 5 ++ config/routes.rb | 5 ++ db/migrate/20170804170049_create_community.rb | 7 +++ ...0170804171325_add_community_to_proposal.rb | 5 ++ db/migrate/20170807082243_create_topics.rb | 9 ++++ db/schema.rb | 20 +++++++- 20 files changed, 210 insertions(+), 10 deletions(-) create mode 100644 app/controllers/communities_controller.rb create mode 100644 app/controllers/topics_controller.rb create mode 100644 app/models/community.rb create mode 100644 app/models/topic.rb create mode 100644 app/views/communities/show.html.erb create mode 100644 app/views/topics/_form.html.erb create mode 100644 app/views/topics/_topics.html.erb create mode 100644 app/views/topics/edit.html.erb create mode 100644 app/views/topics/new.html.erb create mode 100644 app/views/topics/show.html.erb create mode 100644 db/migrate/20170804170049_create_community.rb create mode 100644 db/migrate/20170804171325_add_community_to_proposal.rb create mode 100644 db/migrate/20170807082243_create_topics.rb diff --git a/app/controllers/communities_controller.rb b/app/controllers/communities_controller.rb new file mode 100644 index 000000000..e4421281f --- /dev/null +++ b/app/controllers/communities_controller.rb @@ -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 diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb new file mode 100644 index 000000000..b2d698e41 --- /dev/null +++ b/app/controllers/topics_controller.rb @@ -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 diff --git a/app/models/community.rb b/app/models/community.rb new file mode 100644 index 000000000..5d8d82b34 --- /dev/null +++ b/app/models/community.rb @@ -0,0 +1,6 @@ +class Community < ActiveRecord::Base + has_one :proposal + has_one :investment + has_many :topics + +end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 000c2d42d..ae4ed9682 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -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 diff --git a/app/models/topic.rb b/app/models/topic.rb new file mode 100644 index 000000000..04411ac91 --- /dev/null +++ b/app/models/topic.rb @@ -0,0 +1,4 @@ +class Topic < ActiveRecord::Base + belongs_to :community + belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' +end diff --git a/app/views/admin/budget_investments/index.html.erb b/app/views/admin/budget_investments/index.html.erb index beb5eb856..e86402fd5 100644 --- a/app/views/admin/budget_investments/index.html.erb +++ b/app/views/admin/budget_investments/index.html.erb @@ -41,4 +41,3 @@
<%= render '/admin/budget_investments/investments' %>
- diff --git a/app/views/admin/budgets/index.html.erb b/app/views/admin/budgets/index.html.erb index 87aaa8c66..6d9c4aee7 100644 --- a/app/views/admin/budgets/index.html.erb +++ b/app/views/admin/budgets/index.html.erb @@ -41,4 +41,4 @@ -<%= paginate @budgets %> \ No newline at end of file +<%= paginate @budgets %> diff --git a/app/views/communities/show.html.erb b/app/views/communities/show.html.erb new file mode 100644 index 000000000..cb3f2334f --- /dev/null +++ b/app/views/communities/show.html.erb @@ -0,0 +1,17 @@ +Comunidad de usuarios +
+<%= @community.proposal.title %> +
+Participa en la comunidad de esta propuesta + + +
+
+ <%= render "topics/topics", topics: @community.topics %> +
+
+ +
+ Participa + <%= link_to t("topic.new"), new_community_topic_path(@community.id), class: 'button expanded' %> +
diff --git a/app/views/proposals/index.html.erb b/app/views/proposals/index.html.erb index 072d88ccb..69a8df4a3 100644 --- a/app/views/proposals/index.html.erb +++ b/app/views/proposals/index.html.erb @@ -89,12 +89,12 @@ diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index 523c0961d..05ef91fca 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -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' %> diff --git a/app/views/topics/_form.html.erb b/app/views/topics/_form.html.erb new file mode 100644 index 000000000..e0172219b --- /dev/null +++ b/app/views/topics/_form.html.erb @@ -0,0 +1,15 @@ +<%= form_for([@community, @topic]) do |f| %> + + <%= render 'shared/errors', resource: @topic %> + +
+
+ <%= f.label :title, t("topic.form.topic_title") %> + <%= f.text_field :title %> +
+ +
+ <%= f.submit(class: "button", value: t("topic.#{action_name}.form.submit_button")) %> +
+
+<% end %> diff --git a/app/views/topics/_topics.html.erb b/app/views/topics/_topics.html.erb new file mode 100644 index 000000000..689178143 --- /dev/null +++ b/app/views/topics/_topics.html.erb @@ -0,0 +1,16 @@ +
+ Ordenar por: +
+
+<% if topics.any? %> + <% topics.each do |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 %> +
+ <% end %> +<% else %> + Crea el primer tema de la comunidad!!! +<% end %> diff --git a/app/views/topics/edit.html.erb b/app/views/topics/edit.html.erb new file mode 100644 index 000000000..7d6254258 --- /dev/null +++ b/app/views/topics/edit.html.erb @@ -0,0 +1,10 @@ +
+ +
+ <%= render "shared/back_link" %> + +

<%= t("topic.edit.editing") %>

+ + <%= render "form" %> +
+
diff --git a/app/views/topics/new.html.erb b/app/views/topics/new.html.erb new file mode 100644 index 000000000..c6620a396 --- /dev/null +++ b/app/views/topics/new.html.erb @@ -0,0 +1,12 @@ +
+
+

<%= t("topic.create") %>

+ + <%= render '/topics/form' %> + <%#= render '/topics/form', form_url: budget_investments_path(@budget) %> +
+
+ Recomendaciones para crear un tema +
+ +
diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb new file mode 100644 index 000000000..333b7b543 --- /dev/null +++ b/app/views/topics/show.html.erb @@ -0,0 +1,5 @@ +<%= render "shared/back_link" %> +
+Comunidad: <%= @community.proposal.title %> +
+<%= @topic.title %> diff --git a/config/routes.rb b/config/routes.rb index 46751fd48..00985071a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -156,6 +156,10 @@ Rails.application.routes.draw do resource :verification, controller: "verification", only: [:show] + resources :communities, only: [:show] do + resources :topics + end + scope module: :verification do resource :residence, controller: "residence", only: [:new, :create] 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] end + namespace :admin do root to: "dashboard#index" resources :organizations, only: :index do diff --git a/db/migrate/20170804170049_create_community.rb b/db/migrate/20170804170049_create_community.rb new file mode 100644 index 000000000..938b75efb --- /dev/null +++ b/db/migrate/20170804170049_create_community.rb @@ -0,0 +1,7 @@ +class CreateCommunity < ActiveRecord::Migration + def change + create_table :communities do |t| + t.timestamps null: false + end + end +end diff --git a/db/migrate/20170804171325_add_community_to_proposal.rb b/db/migrate/20170804171325_add_community_to_proposal.rb new file mode 100644 index 000000000..e18cd53c9 --- /dev/null +++ b/db/migrate/20170804171325_add_community_to_proposal.rb @@ -0,0 +1,5 @@ +class AddCommunityToProposal < ActiveRecord::Migration + def change + add_reference :proposals, :community, index: true, foreign_key: true + end +end diff --git a/db/migrate/20170807082243_create_topics.rb b/db/migrate/20170807082243_create_topics.rb new file mode 100644 index 000000000..3af567279 --- /dev/null +++ b/db/migrate/20170807082243_create_topics.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 60ae69a0c..086818b57 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # 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 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", ["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| t.string "title", limit: 80 t.text "description" @@ -749,11 +754,13 @@ ActiveRecord::Schema.define(version: 20170720092638) do t.datetime "retired_at" t.string "retired_reason" t.text "retired_explanation" + t.integer "community_id" end 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", ["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", ["geozone_id"], name: "index_proposals_on_geozone_id", 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", ["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| t.string "email", default: "" 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_on_direct_message", default: true 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.integer "failed_email_digests_count", default: 0 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_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 "proposals", "communities" add_foreign_key "users", "geozones" add_foreign_key "valuators", "users" end