-
+

diff --git a/app/views/welcome/highlights.html.erb b/app/views/welcome/highlights.html.erb index f0b4e446e..66d335448 100644 --- a/app/views/welcome/highlights.html.erb +++ b/app/views/welcome/highlights.html.erb @@ -4,19 +4,16 @@
-
- -
-

<%= t('welcome.highlights') %>

-    -

- <%= t('welcome.signed_in_home_title') %> -

-
- - <%= render @list %> - <%= paginate @paginator %> +
+

<%= t('welcome.highlights') %>

+    +

+ <%= t('welcome.signed_in_home_title') %> +

+ + <%= render @list %> + <%= paginate @paginator %>
diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 561ad3347..9941114f8 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -109,6 +109,7 @@ ignore_unused: - 'admin.comments.index.filter*' - 'admin.debates.index.filter*' - 'admin.proposals.index.filter*' + - 'admin.spending_proposals.index.filter*' - 'admin.organizations.index.filter*' - 'admin.users.index.filter*' - 'admin.activity.show.filter*' @@ -127,6 +128,8 @@ ignore_unused: - 'proposals.index.select_order' - 'proposals.index.orders.*' - 'proposals.index.search_form.*' + - 'notifications.index.comments_on*' + - 'notifications.index.replies_to*' - 'helpers.page_entries_info.*' # kaminari - 'views.pagination.*' # kaminari # - '{devise,kaminari,will_paginate}.*' diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 4e9cd5931..42c29fed8 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -37,6 +37,9 @@ en: proposal: one: "Citizen proposal" other: "Citizen proposals" + spending_proposal: + one: "Spending proposal" + other: "Spending proposals" attributes: comment: body: "Comment" diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 998bc4fb6..780595562 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -37,6 +37,9 @@ es: proposal: one: "Propuesta ciudadana" other: "Propuestas ciudadanas" + spending_proposal: + one: "Propuesta de gasto" + other: "Propuestas de gasto" attributes: comment: body: "Comentario" diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 22bd6d97b..e511d8db1 100755 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -16,6 +16,7 @@ en: hidden_debates: "Hidden debates" hidden_comments: "Hidden comments" hidden_users: "Hidden users" + spending_proposals: "Spending proposals" incomplete_verifications: "Incomplete verifications" organizations: "Organisations" officials: "Officials" @@ -95,6 +96,20 @@ en: all: "All" with_confirmed_hide: "Confirmed" without_confirmed_hide: "Pending" + spending_proposals: + actions: + accept: Accept + reject: Reject + index: + title: "Spending proposals for participatory budgeting" + filter: "Filter" + filters: + unresolved: "Unresolved" + accepted: "Accepted" + rejected: "Rejected" + show: + geozone: "Scope" + by: "Sent by" users: index: title: "Hidden users" diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index a1b9395a1..9c7ed27a1 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -16,6 +16,7 @@ es: hidden_debates: "Debates ocultos" hidden_comments: "Comentarios ocultos" hidden_users: "Usuarios bloqueados" + spending_proposals: "Propuestas de gasto" incomplete_verifications: "Verificaciones incompletas" organizations: "Organizaciones" officials: "Cargos públicos" @@ -95,6 +96,20 @@ es: all: "Todas" with_confirmed_hide: "Confirmadas" without_confirmed_hide: "Pendientes" + spending_proposals: + actions: + accept: Aceptar + reject: Rechazar + index: + title: "Propuestas de gasto para presupuestos participativos" + filter: "Filtro" + filters: + unresolved: "Sin resolver" + accepted: "Aceptadas" + rejected: "Rechazadas" + show: + geozone: "Ámbito" + by: "Enviada por" users: index: title: "Usuarios bloqueados" diff --git a/config/locales/en.yml b/config/locales/en.yml index 60c22b775..7b2361b4e 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -29,6 +29,11 @@ en: more_information: "More information" debates: "Debates" proposals: "Proposals" + spending_proposals: "Spending proposals" + new_notifications: + one: "You have a new notification" + other: "You have %{count} new notifications" + no_notifications: "You don't have new notifications" footer: description: "This portal uses the %{consul} which is %{open_source}. From Madrid out into the world." open_source: "open-source software" @@ -71,6 +76,7 @@ en: user: "Account" debate: "Debate" proposal: "Proposal" + spending_proposal: "Spending proposal" verification::sms: "Telephone" verification::letter: "the verification" application: @@ -79,7 +85,7 @@ en: debates: index: title: "Debates" - featured_debates_html: "Featured
debates" + featured_debates: "Featured" start_debate: "Start a debate" select_order: "Order by" orders: @@ -156,7 +162,7 @@ en: proposals: index: title: "Proposals" - featured_proposals_html: "Featured
proposals" + featured_proposals: "Featured" start_proposal: "Create a proposal" select_order_long: "You are viewing proposals according to:" select_order: "Order by" @@ -244,7 +250,34 @@ en: update: form: submit_button: "Save changes" + spending_proposals: + index: + title: "Participatory budgeting" + text: "Here you can send spending proposals to be considered in the frame of the annual participatory budgeting." + create_link: "Create spending proposal" + verified_only: "Only verified users can create spending proposals, %{verify_account}." + verify_account: "verify your account" + new: + back_link: Back + start_new: "Create spending proposal" + more_info: "How do participatory budgeting works?" + recommendations_title: "Recommendations for creating a spending proposal" + recommendation_one: "It's mandatory that the proposal makes reference to a budgetable action." + recommendation_two: "Any proposal or comment suggesting illegal action will be deleted." + recommendation_three: "Try to go into details when describing your spending proposal so the reviewing team undertands your points." + form: + title: "Spending proposal title" + description: "Description" + external_url: "Link to additional documentation" + geozone: "Scope of operation" + submit_buttons: + new: Create + create: Create + geozones: + none: "All city" comments: + show: + return_to_commentable: "Go back to " select_order: "Sort by" orders: most_voted: "Most voted" @@ -311,6 +344,17 @@ en: user_permission_votes: "Participate on final voting" user_permission_verify: "To perform all the actions verify your account." user_permission_verify_info: "* Only for users on Madrid City Census." + notifications: + index: + title: "Notifications" + mark_all_as_read: "Mark all as read" + empty_notifications: "You don't have new notifications." + comments_on: + one: "Someone commented on" + other: "There are %{count} new comments on" + replies_to: + one: "Someone replied to your comment on" + other: "There are %{count} new replies to your comment on" simple_captcha: placeholder: "Enter the text from the image" label: "Enter the text from the image in the box below" @@ -319,6 +363,7 @@ en: user: "the secret code does not match the image" debate: "the secret code does not match the image" proposal: "the secret code does not match the image" + spendingproposal: "the secret code does not match the image" shared: author_info: author_deleted: "User deleted" diff --git a/config/locales/es.yml b/config/locales/es.yml index 246221c04..efd750781 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -29,6 +29,11 @@ es: more_information: "Más información" debates: "Debates" proposals: "Propuestas" + spending_proposals: "Presupuestos ciudadanos" + new_notifications: + one: "Tienes una nueva notificación" + other: "Tienes %{count} notificaciones nuevas" + no_notifications: "No tienes notificaciones nuevas" footer: description: "Este portal usa la %{consul} que es %{open_source}. De Madrid, para el mundo entero." open_source: "software libre" @@ -71,6 +76,7 @@ es: user: "la cuenta" debate: "el debate" proposal: "la propuesta" + spending_proposal: "la propuesta de gasto" verification::sms: "el teléfono" verification::letter: "la verificación" application: @@ -79,7 +85,7 @@ es: debates: index: title: "Debates" - featured_debates_html: "Debates
destacados" + featured_debates: "Destacados" start_debate: "Empieza un debate" select_order: "Ordenar por" orders: @@ -156,7 +162,7 @@ es: proposals: index: title: "Propuestas ciudadanas" - featured_proposals_html: "Propuestas
destacadas" + featured_proposals: "Destacadas" start_proposal: "Crea una propuesta" select_order_long: "Estas viendo las propuestas" select_order: "Ordenar por" @@ -205,7 +211,7 @@ es: proposal_responsible_name: "Nombre y apellidos de la persona que hace esta propuesta" proposal_responsible_name_note: "(individualmente o como representante de un colectivo; no se mostrará públicamente)" tags_label: "Temas" - tags_instructions: "Etiqueta esta propuesta. Puedes elegir entre Categorias y Distritos propuestos o introducir las que desees." + tags_instructions: "Etiqueta esta propuesta. Puedes elegir entre nuestras sugerencias o introducir las que desees." tags_placeholder: "Escribe las etiquetas que desees separadas por una coma (',')" tag_category_label: "Categorías" tag_district_label: "Distritos" @@ -244,7 +250,34 @@ es: update: form: submit_button: "Guardar cambios" + spending_proposals: + index: + title: "Presupuestos participativos" + text: "Desde esta sección podrás sugerir propuestas de gasto que irán asociadas a las partidas de presupuestos ciudadanos. El requisito principal es que sean propuestas presupuestables." + create_link: "Enviar propuesta de gasto" + verified_only: "Sólo los usuarios verificados pueden crear propuestas de gasto, %{verify_account}." + verify_account: "verifica tu cuenta" + new: + back_link: Volver + start_new: "Crear una propuesta de gasto" + more_info: "¿Cómo funcionan los presupuestos participativos?" + recommendations_title: "Recomendaciones para crear una propuesta de gasto" + recommendation_one: "Es fundamental que haga referencia a una actuación presupuestable." + recommendation_two: "Cualquier propuesta o comentario que implique acciones ilegales será eliminada." + recommendation_three: "Intenta detallar lo máximo posible la propuesta para que el equipo de gobierno encargado de estudiarla tenga las menor dudas posibles." + form: + title: "Título de la propuesta de gasto" + description: "Descripción detallada" + external_url: "Enlace a documentación adicional" + geozone: "Ámbito de actuación" + submit_buttons: + new: Crear + create: Crear + geozones: + none: "Toda la ciudad" comments: + show: + return_to_commentable: "Volver a " select_order: "Ordenar por" orders: most_voted: "Más votados" @@ -311,6 +344,17 @@ es: user_permission_votes: "Participar en las votaciones finales*" user_permission_verify: "Para poder realizar todas las acciones verifica tu cuenta." user_permission_verify_info: "* Sólo usuarios empadronados en el municipio de Madrid." + notifications: + index: + title: "Notificaciones" + mark_all_as_read: "Marcar todas como leídas" + empty_notifications: "No tienes notificaciones nuevas." + comments_on: + one: "Hay un nuevo comentario en" + other: "Hay %{count} comentarios nuevos en" + replies_to: + one: "Hay una respuesta nueva a tu comentario en" + other: "Hay %{count} nuevas respuestas a tu comentario en" simple_captcha: placeholder: "Introduce el texto de la imagen" label: "Introduce el texto de la imagen en la siguiente caja" @@ -319,6 +363,7 @@ es: user: "el código secreto no coincide con la imagen" debate: "el código secreto no coincide con la imagen" proposal: "el código secreto no coincide con la imagen" + spendingproposal: "el código secreto no coincide con la imagen" shared: author_info: author_deleted: Usuario eliminado diff --git a/config/locales/pages.en.yml b/config/locales/pages.en.yml index b69694d38..f34e7250a 100755 --- a/config/locales/pages.en.yml +++ b/config/locales/pages.en.yml @@ -35,6 +35,7 @@ en: how_to_use: "Use it in your local government" participation: "Madrid Participation and Transparency y Transparencia - coming news" proposals_info: "How does citizen proposals work?" + spending_proposals_info: "How does participatory budgeting work?" participation_world: "Direct citizen participation in the world" participation_facts: "Facts about citizen participation and direct democracy" faq: "Solution to tecnical problemas (FAQ)" @@ -44,6 +45,7 @@ en: how_to_use: "Use it freely or help us to improve it, it is free software" participation: "Citizen participation, transparency and open government" proposals_info: "Create your own proposals" + spending_proposals_info: "Create your own spending proposals" participation_world: "Systems of citizen participation that exist in the world" participation_facts: "To lose your fear" faq: "Frecuently asked question about tecnical problems" diff --git a/config/locales/pages.es.yml b/config/locales/pages.es.yml index 4e6877482..77a996ba5 100644 --- a/config/locales/pages.es.yml +++ b/config/locales/pages.es.yml @@ -35,6 +35,7 @@ es: how_to_use: "Utilízalo en tu municipio" participation: "Participación y Transparencia en Madrid - Próximas novedades" proposals_info: "¿Cómo funcionan las propuestas ciudadanas?" + spending_proposals_info: "¿Cómo funcionan los presupuestos participativos?" participation_world: "Participación ciudadana directa en el mundo" participation_facts: "Hechos sobre participación ciudadana y democracia directa" faq: "Soluciones a problemas técnicos (FAQ)" @@ -44,6 +45,7 @@ es: how_to_use: "Utilízalo libremente o ayúdanos a mejorarlo, es software libre" participation: "Participación Ciudadana, Transparencia y Gobierno Abierto" proposals_info: "Crea tus propias propuestas" + spending_proposals_info: "Envía tus propuestas de gasto" participation_world: "Sistemas de participación ciudadana que ya existen en el mundo" participation_facts: "Para perderle el miedo" faq: "Preguntas frecuentes sobre problemas técnicos" diff --git a/config/routes.rb b/config/routes.rb index 3472f1c4e..d10ffefd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,7 +53,7 @@ Rails.application.routes.draw do end end - resources :comments, only: :create, shallow: true do + resources :comments, only: [:create, :show], shallow: true do member do post :vote put :flag @@ -61,6 +61,8 @@ Rails.application.routes.draw do end end + resources :spending_proposals, only: [:index, :new, :create] + resources :legislations, only: [:show] resources :annotations do @@ -74,6 +76,11 @@ Rails.application.routes.draw do resource :account, controller: "account", only: [:show, :update, :delete] do collection { get :erase } end + + resources :notifications, only: [:index, :show] do + collection { put :mark_all_as_read } + end + resource :verification, controller: "verification", only: [:show] scope module: :verification do @@ -115,6 +122,13 @@ Rails.application.routes.draw do end end + resources :spending_proposals, only: [:index, :show] do + member do + put :accept + put :reject + end + end + resources :comments, only: :index do member do put :restore diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 8bd6f9876..47fc39300 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -14,6 +14,9 @@ Setting.create(key: 'max_votes_for_proposal_edit', value: '1000') Setting.create(key: 'proposal_code_prefix', value: 'MAD') Setting.create(key: 'votes_for_proposal_success', value: '100') +puts "Creating Geozones" +('A'..'Z').each{ |i| Geozone.create(name: "District #{i}") } + puts "Creating Users" def create_user(email, username = Faker::Name.name) @@ -180,6 +183,25 @@ end Flag.flag(flagger, proposal) end +puts "Creating Spending Proposals" + +resolutions = ["accepted", "rejected", nil] + +(1..30).each do |i| + geozone = Geozone.reorder("RANDOM()").first + author = User.reorder("RANDOM()").first + description = "

#{Faker::Lorem.paragraphs.join('

')}

" + spending_proposal = SpendingProposal.create!(author: author, + title: Faker::Lorem.sentence(3).truncate(60), + external_url: Faker::Internet.url, + description: description, + created_at: rand((Time.now - 1.week) .. Time.now), + resolution: resolutions.sample, + geozone: [geozone, nil].sample, + terms_of_service: "1") + puts " #{spending_proposal.title}" +end + puts "Creating Legislation" Legislation.create!(title: 'Participatory Democracy', body: 'In order to achieve...') diff --git a/db/migrate/20150928115005_create_notifications.rb b/db/migrate/20150928115005_create_notifications.rb new file mode 100644 index 000000000..42bbbf72e --- /dev/null +++ b/db/migrate/20150928115005_create_notifications.rb @@ -0,0 +1,9 @@ +class CreateNotifications < ActiveRecord::Migration + def change + create_table :notifications do |t| + t.belongs_to :user, index: true, foreign_key: true + t.belongs_to :activity, index: true, foreign_key: true + t.boolean :read, default: false + end + end +end diff --git a/db/migrate/20151218114205_create_spending_proposals.rb b/db/migrate/20151218114205_create_spending_proposals.rb new file mode 100644 index 000000000..5bcd3aba1 --- /dev/null +++ b/db/migrate/20151218114205_create_spending_proposals.rb @@ -0,0 +1,12 @@ +class CreateSpendingProposals < ActiveRecord::Migration + def change + create_table :spending_proposals do |t| + t.string :title + t.text :description + t.integer :author_id + t.string :external_url + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160104203329_add_spending_proposals_counter_to_tags.rb b/db/migrate/20160104203329_add_spending_proposals_counter_to_tags.rb new file mode 100644 index 000000000..2aade49cb --- /dev/null +++ b/db/migrate/20160104203329_add_spending_proposals_counter_to_tags.rb @@ -0,0 +1,6 @@ +class AddSpendingProposalsCounterToTags < ActiveRecord::Migration + def change + add_column :tags, :spending_proposals_count, :integer, default: 0 + add_index :tags, :spending_proposals_count + end +end diff --git a/db/migrate/20160104203438_add_spending_proposals_indexes.rb b/db/migrate/20160104203438_add_spending_proposals_indexes.rb new file mode 100644 index 000000000..777b0f678 --- /dev/null +++ b/db/migrate/20160104203438_add_spending_proposals_indexes.rb @@ -0,0 +1,5 @@ +class AddSpendingProposalsIndexes < ActiveRecord::Migration + def change + add_index :spending_proposals, :author_id + end +end diff --git a/db/migrate/20160105121132_create_geozones.rb b/db/migrate/20160105121132_create_geozones.rb new file mode 100644 index 000000000..3e43339e9 --- /dev/null +++ b/db/migrate/20160105121132_create_geozones.rb @@ -0,0 +1,11 @@ +class CreateGeozones < ActiveRecord::Migration + def change + create_table :geozones do |t| + t.string :name + t.string :html_map_coordinates + t.string :external_code + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160105170113_merge_activities_and_notifications.rb b/db/migrate/20160105170113_merge_activities_and_notifications.rb new file mode 100644 index 000000000..fac5b213d --- /dev/null +++ b/db/migrate/20160105170113_merge_activities_and_notifications.rb @@ -0,0 +1,10 @@ +class MergeActivitiesAndNotifications < ActiveRecord::Migration + def change + change_table :notifications do |t| + t.remove :read + t.remove :activity_id + t.references :notifiable, polymorphic: true + end + end + +end diff --git a/db/migrate/20160107114749_add_geozone_to_spending_proposal.rb b/db/migrate/20160107114749_add_geozone_to_spending_proposal.rb new file mode 100644 index 000000000..62130fd9b --- /dev/null +++ b/db/migrate/20160107114749_add_geozone_to_spending_proposal.rb @@ -0,0 +1,6 @@ +class AddGeozoneToSpendingProposal < ActiveRecord::Migration + def change + add_column :spending_proposals, :geozone_id, :integer, default: nil + add_index :spending_proposals, :geozone_id + end +end diff --git a/db/migrate/20160107132059_add_resolution_to_spending_proposals.rb b/db/migrate/20160107132059_add_resolution_to_spending_proposals.rb new file mode 100644 index 000000000..18365a715 --- /dev/null +++ b/db/migrate/20160107132059_add_resolution_to_spending_proposals.rb @@ -0,0 +1,6 @@ +class AddResolutionToSpendingProposals < ActiveRecord::Migration + def change + add_column :spending_proposals, :resolution, :string, default: nil + add_index :spending_proposals, :resolution + end +end diff --git a/db/migrate/20160108114750_add_counter_to_notifications.rb b/db/migrate/20160108114750_add_counter_to_notifications.rb new file mode 100644 index 000000000..7cf3c434c --- /dev/null +++ b/db/migrate/20160108114750_add_counter_to_notifications.rb @@ -0,0 +1,5 @@ +class AddCounterToNotifications < ActiveRecord::Migration + def change + add_column :notifications, :counter, :integer, default: 1 + end +end diff --git a/db/migrate/20160108133501_add_notifications_counter_cache_to_user.rb b/db/migrate/20160108133501_add_notifications_counter_cache_to_user.rb new file mode 100644 index 000000000..e106df6ae --- /dev/null +++ b/db/migrate/20160108133501_add_notifications_counter_cache_to_user.rb @@ -0,0 +1,5 @@ +class AddNotificationsCounterCacheToUser < ActiveRecord::Migration + def change + add_column :users, :notifications_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 8136d925b..9578a4763 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,8 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160108101736) do + +ActiveRecord::Schema.define(version: 20160108133501) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -171,6 +172,14 @@ ActiveRecord::Schema.define(version: 20160108101736) do add_index "flags", ["user_id", "flaggable_type", "flaggable_id"], name: "access_inappropiate_flags", using: :btree add_index "flags", ["user_id"], name: "index_flags_on_user_id", using: :btree + create_table "geozones", force: :cascade do |t| + t.string "name" + t.string "html_map_coordinates" + t.string "external_code" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "identities", force: :cascade do |t| t.integer "user_id" t.string "provider" @@ -191,7 +200,7 @@ ActiveRecord::Schema.define(version: 20160108101736) do create_table "locks", force: :cascade do |t| t.integer "user_id" t.integer "tries", default: 0 - t.datetime "locked_until", default: '2000-01-01 07:01:01', null: false + t.datetime "locked_until", default: '2000-01-01 00:01:01', null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end @@ -204,6 +213,15 @@ ActiveRecord::Schema.define(version: 20160108101736) do add_index "moderators", ["user_id"], name: "index_moderators_on_user_id", using: :btree + create_table "notifications", force: :cascade do |t| + t.integer "user_id" + t.integer "notifiable_id" + t.string "notifiable_type" + t.integer "counter", default: 1 + end + + add_index "notifications", ["user_id"], name: "index_notifications_on_user_id", using: :btree + create_table "organizations", force: :cascade do |t| t.integer "user_id" t.string "name", limit: 60 @@ -264,6 +282,21 @@ ActiveRecord::Schema.define(version: 20160108101736) do add_index "simple_captcha_data", ["key"], name: "idx_key", using: :btree + create_table "spending_proposals", force: :cascade do |t| + t.string "title" + t.text "description" + t.integer "author_id" + t.string "external_url" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "geozone_id" + t.string "resolution" + end + + add_index "spending_proposals", ["author_id"], name: "index_spending_proposals_on_author_id", using: :btree + add_index "spending_proposals", ["geozone_id"], name: "index_spending_proposals_on_geozone_id", using: :btree + add_index "spending_proposals", ["resolution"], name: "index_spending_proposals_on_resolution", using: :btree + create_table "taggings", force: :cascade do |t| t.integer "tag_id" t.integer "taggable_id" @@ -284,11 +317,13 @@ ActiveRecord::Schema.define(version: 20160108101736) do t.integer "debates_count", default: 0 t.integer "proposals_count", default: 0 t.string "kind", limit: 40 + t.integer "spending_proposals_count", default: 0 end add_index "tags", ["debates_count"], name: "index_tags_on_debates_count", using: :btree add_index "tags", ["name"], name: "index_tags_on_name", unique: true, 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 create_table "users", force: :cascade do |t| t.string "email", default: "" @@ -331,6 +366,7 @@ ActiveRecord::Schema.define(version: 20160108101736) do t.datetime "erased_at" t.boolean "public_activity", default: true t.boolean "newsletter", default: false + t.integer "notifications_count", default: 0 end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree @@ -405,5 +441,6 @@ ActiveRecord::Schema.define(version: 20160108101736) do add_foreign_key "identities", "users" add_foreign_key "locks", "users" add_foreign_key "moderators", "users" + add_foreign_key "notifications", "users" add_foreign_key "organizations", "users" end diff --git a/lib/comment_tree.rb b/lib/comment_tree.rb index 8d0e072dd..f2eadb8ab 100644 --- a/lib/comment_tree.rb +++ b/lib/comment_tree.rb @@ -2,24 +2,34 @@ class CommentTree ROOT_COMMENTS_PER_PAGE = 10 - attr_accessor :root_comments, :comments + attr_accessor :root_comments, :comments, :commentable, :page, :order def initialize(commentable, page, order = 'confidence_score') - @root_comments = commentable.comments.roots.send("sort_by_#{order}").page(page).per(ROOT_COMMENTS_PER_PAGE).for_render - - root_descendants = @root_comments.each_with_object([]) do |root, col| - col.concat(Comment.descendants_of(root).send("sort_descendants_by_#{order}").for_render.to_a) - end + @commentable = commentable + @page = page + @order = order @comments = root_comments + root_descendants + end - @comments_by_parent_id = @comments.each_with_object({}) do |comment, col| - (col[comment.parent_id] ||= []) << comment + def root_comments + commentable.comments.roots.send("sort_by_#{order}").page(page).per(ROOT_COMMENTS_PER_PAGE).for_render + end + + def root_descendants + root_comments.each_with_object([]) do |root, array| + array.concat(Comment.descendants_of(root).send("sort_descendants_by_#{order}").for_render.to_a) end end - def children_of(parent) - @comments_by_parent_id[parent.id] || [] + def ordered_children_of(parent) + comments_by_parent_id[parent.id] || [] + end + + def comments_by_parent_id + comments.each_with_object({}) do |comment, array| + (array[comment.parent_id] ||= []) << comment + end end def comment_authors diff --git a/spec/factories.rb b/spec/factories.rb index fc71e241c..1791b2b6e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -180,6 +180,14 @@ FactoryGirl.define do end end + factory :spending_proposal do + sequence(:title) { |n| "Spending Proposal #{n} title" } + description 'Spend money on this' + external_url 'http://external_documention.org' + terms_of_service '1' + association :author, factory: :user + end + factory :vote do association :votable, factory: :debate association :voter, factory: :user @@ -291,4 +299,12 @@ FactoryGirl.define do sequence(:track_id) { |n| "#{n}" } end + factory :notification do + user + association :notifiable, factory: :proposal + end + + factory :geozone do + sequence(:name) { |n| "District #{n}" } + end end diff --git a/spec/features/admin/spending_proposals_spec.rb b/spec/features/admin/spending_proposals_spec.rb new file mode 100644 index 000000000..a355c13ec --- /dev/null +++ b/spec/features/admin/spending_proposals_spec.rb @@ -0,0 +1,140 @@ +require 'rails_helper' + +feature 'Admin spending proposals' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + scenario 'Index shows spending proposals' do + spending_proposal = create(:spending_proposal) + visit admin_spending_proposals_path + + expect(page).to have_content(spending_proposal.title) + end + + scenario 'Accept from index' do + spending_proposal = create(:spending_proposal) + visit admin_spending_proposals_path + + click_link 'Accept' + + expect(page).to_not have_content(spending_proposal.title) + + click_link 'Accepted' + expect(page).to have_content(spending_proposal.title) + + expect(spending_proposal.reload).to be_accepted + end + + scenario 'Reject from index' do + spending_proposal = create(:spending_proposal) + visit admin_spending_proposals_path + + click_link 'Reject' + + expect(page).to_not have_content(spending_proposal.title) + + click_link('Rejected') + expect(page).to have_content(spending_proposal.title) + + expect(spending_proposal.reload).to be_rejected + end + + scenario "Current filter is properly highlighted" do + visit admin_spending_proposals_path + expect(page).to_not have_link('Unresolved') + expect(page).to have_link('Accepted') + expect(page).to have_link('Rejected') + + visit admin_spending_proposals_path(filter: 'unresolved') + expect(page).to_not have_link('Unresolved') + expect(page).to have_link('Accepted') + expect(page).to have_link('Rejected') + + visit admin_spending_proposals_path(filter: 'accepted') + expect(page).to have_link('Unresolved') + expect(page).to_not have_link('Accepted') + expect(page).to have_link('Rejected') + + visit admin_spending_proposals_path(filter: 'rejected') + expect(page).to have_link('Accepted') + expect(page).to have_link('Unresolved') + expect(page).to_not have_link('Rejected') + end + + scenario "Filtering proposals" do + create(:spending_proposal, title: "Recent spending proposal") + create(:spending_proposal, title: "Good spending proposal", resolution: "accepted") + create(:spending_proposal, title: "Bad spending proposal", resolution: "rejected") + + visit admin_spending_proposals_path(filter: 'unresolved') + expect(page).to have_content('Recent spending proposal') + expect(page).to_not have_content('Good spending proposal') + expect(page).to_not have_content('Bad spending proposal') + + visit admin_spending_proposals_path(filter: 'accepted') + expect(page).to have_content('Good spending proposal') + expect(page).to_not have_content('Recent spending proposal') + expect(page).to_not have_content('Bad spending proposal') + + visit admin_spending_proposals_path(filter: 'rejected') + expect(page).to have_content('Bad spending proposal') + expect(page).to_not have_content('Good spending proposal') + expect(page).to_not have_content('Recent spending proposal') + end + + scenario "Action links remember the pagination setting and the filter" do + per_page = Kaminari.config.default_per_page + (per_page + 2).times { create(:spending_proposal, resolution: "accepted") } + + visit admin_spending_proposals_path(filter: 'accepted', page: 2) + + click_on('Reject', match: :first, exact: true) + + expect(current_url).to include('filter=accepted') + expect(current_url).to include('page=2') + end + + scenario 'Show' do + spending_proposal = create(:spending_proposal, geozone: create(:geozone)) + visit admin_spending_proposals_path + + click_link spending_proposal.title + + expect(page).to have_content(spending_proposal.title) + expect(page).to have_content(spending_proposal.description) + expect(page).to have_content(spending_proposal.author.name) + expect(page).to have_content(spending_proposal.geozone.name) + end + + scenario 'Accept from show' do + spending_proposal = create(:spending_proposal) + visit admin_spending_proposal_path(spending_proposal) + + click_link 'Accept' + + expect(page).to_not have_content(spending_proposal.title) + + click_link 'Accepted' + expect(page).to have_content(spending_proposal.title) + + expect(spending_proposal.reload).to be_accepted + end + + scenario 'Reject from show' do + spending_proposal = create(:spending_proposal) + visit admin_spending_proposal_path(spending_proposal) + + click_link 'Reject' + + expect(page).to_not have_content(spending_proposal.title) + + click_link('Rejected') + expect(page).to have_content(spending_proposal.title) + + expect(spending_proposal.reload).to be_rejected + end + +end diff --git a/spec/features/comments/debates_spec.rb b/spec/features/comments/debates_spec.rb index 5adfa218c..7d807a6c0 100644 --- a/spec/features/comments/debates_spec.rb +++ b/spec/features/comments/debates_spec.rb @@ -20,6 +20,21 @@ feature 'Commenting debates' do end end + scenario 'Show' do + parent_comment = create(:comment, commentable: debate) + first_child = create(:comment, commentable: debate, parent: parent_comment) + second_child = create(:comment, commentable: debate, parent: parent_comment) + + visit comment_path(parent_comment) + + expect(page).to have_css(".comment", count: 3) + expect(page).to have_content parent_comment.body + expect(page).to have_content first_child.body + expect(page).to have_content second_child.body + + expect(page).to have_link "Go back to #{debate.title}", debate_path(debate) + end + scenario 'Comment order' do c1 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) c2 = create(:comment, :with_confidence_score, commentable: debate, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) @@ -254,7 +269,7 @@ feature 'Commenting debates' do fill_in "comment-body-debate_#{debate.id}", with: 'Testing submit button!' click_button 'Publish comment' - + # The button's text should now be "..." # This should be checked before the Ajax request is finished expect(page).to_not have_button 'Publish comment' diff --git a/spec/features/comments/proposals_spec.rb b/spec/features/comments/proposals_spec.rb index 1c277cf4d..d919f0b51 100644 --- a/spec/features/comments/proposals_spec.rb +++ b/spec/features/comments/proposals_spec.rb @@ -20,6 +20,21 @@ feature 'Commenting proposals' do end end + scenario 'Show' do + parent_comment = create(:comment, commentable: proposal) + first_child = create(:comment, commentable: proposal, parent: parent_comment) + second_child = create(:comment, commentable: proposal, parent: parent_comment) + + visit comment_path(parent_comment) + + expect(page).to have_css(".comment", count: 3) + expect(page).to have_content parent_comment.body + expect(page).to have_content first_child.body + expect(page).to have_content second_child.body + + expect(page).to have_link "Go back to #{proposal.title}", proposal_path(proposal) + end + scenario 'Comment order' do c1 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 100, cached_votes_total: 120, created_at: Time.now - 2) c2 = create(:comment, :with_confidence_score, commentable: proposal, cached_votes_up: 10, cached_votes_total: 12, created_at: Time.now - 1) diff --git a/spec/features/notifications_spec.rb b/spec/features/notifications_spec.rb new file mode 100644 index 000000000..1ffc6a854 --- /dev/null +++ b/spec/features/notifications_spec.rb @@ -0,0 +1,192 @@ +require 'rails_helper' + +feature "Notifications" do + let(:author) { create :user } + let(:user) { create :user } + let(:debate) { create :debate, author: author } + let(:proposal) { create :proposal, author: author } + + scenario "User commented on my debate", :js do + login_as user + visit debate_path debate + + fill_in "comment-body-debate_#{debate.id}", with: "I commented on your debate" + click_button "Publish comment" + within "#comments" do + expect(page).to have_content "I commented on your debate" + end + + logout + login_as author + visit root_path + + find(".icon-notification").click + + expect(page).to have_css ".notification", count: 1 + + expect(page).to have_content "Someone commented on" + expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']" + end + + scenario "Multiple comments on my proposal", :js do + login_as user + visit proposal_path proposal + + fill_in "comment-body-proposal_#{proposal.id}", with: "I agree" + click_button "Publish comment" + within "#comments" do + expect(page).to have_content "I agree" + end + + logout + login_as create(:user) + visit proposal_path proposal + + fill_in "comment-body-proposal_#{proposal.id}", with: "I disagree" + click_button "Publish comment" + within "#comments" do + expect(page).to have_content "I disagree" + end + + logout + login_as author + visit root_path + + find(".icon-notification").click + + expect(page).to have_css ".notification", count: 1 + + expect(page).to have_content "There are 2 new comments on" + expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']" + end + + scenario "User replied to my comment", :js do + comment = create :comment, commentable: debate, user: author + login_as user + visit debate_path debate + + click_link "Reply" + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: "I replied to your comment" + click_button "Publish reply" + end + + within "#comment_#{comment.id}" do + expect(page).to have_content "I replied to your comment" + end + + logout + login_as author + visit root_path + + find(".icon-notification").click + + expect(page).to have_css ".notification", count: 1 + expect(page).to have_content "Someone replied to your comment on" + expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']" + end + + scenario "Multiple replies to my comment", :js do + comment = create :comment, commentable: debate, user: author + 3.times do |n| + login_as create(:user) + visit debate_path debate + + within("#comment_#{comment.id}_reply") { click_link "Reply" } + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: "Reply number #{n}" + click_button "Publish reply" + end + + within "#comment_#{comment.id}" do + expect(page).to have_content "Reply number #{n}" + end + logout + end + + login_as author + visit root_path + + find(".icon-notification").click + + expect(page).to have_css ".notification", count: 1 + expect(page).to have_content "There are 3 new replies to your comment on" + expect(page).to have_xpath "//a[@href='#{notification_path(Notification.last)}']" + end + + scenario "Author commented on his own debate", :js do + login_as author + visit debate_path debate + + fill_in "comment-body-debate_#{debate.id}", with: "I commented on my own debate" + click_button "Publish comment" + within "#comments" do + expect(page).to have_content "I commented on my own debate" + end + + find(".icon-no-notification").click + expect(page).to have_css ".notification", count: 0 + end + + scenario "Author replied to his own comment", :js do + comment = create :comment, commentable: debate, user: author + login_as author + visit debate_path debate + + click_link "Reply" + within "#js-comment-form-comment_#{comment.id}" do + fill_in "comment-body-comment_#{comment.id}", with: "I replied to my own comment" + click_button "Publish reply" + end + + within "#comment_#{comment.id}" do + expect(page).to have_content "I replied to my own comment" + end + + find(".icon-no-notification") + + visit notifications_path + expect(page).to have_css ".notification", count: 0 + end + + context "mark as read" do + + scenario "mark a single notification as read" do + user = create :user + notification = create :notification, user: user + + login_as user + visit notifications_path + + expect(page).to have_css ".notification", count: 1 + + first(".notification a").click + visit notifications_path + + expect(page).to have_css ".notification", count: 0 + end + + scenario "mark all notifications as read" do + user = create :user + 2.times { create :notification, user: user } + + login_as user + visit notifications_path + + expect(page).to have_css ".notification", count: 2 + click_link "Mark all as read" + + expect(page).to have_css ".notification", count: 0 + expect(current_path).to eq(notifications_path) + end + + end + + scenario "no notifications" do + login_as user + visit notifications_path + + expect(page).to have_content "You don't have new notifications" + end + +end diff --git a/spec/features/spending_proposals_spec.rb b/spec/features/spending_proposals_spec.rb new file mode 100644 index 000000000..6b76b02e0 --- /dev/null +++ b/spec/features/spending_proposals_spec.rb @@ -0,0 +1,66 @@ +require 'rails_helper' + +feature 'Spending proposals' do + + let(:author) { create(:user, :level_two) } + + scenario 'Index' do + visit spending_proposals_path + + expect(page).to_not have_link('Create spending proposal', href: new_spending_proposal_path) + expect(page).to have_link('verify your account') + + login_as(author) + + visit spending_proposals_path + + expect(page).to have_link('Create spending proposal', href: new_spending_proposal_path) + expect(page).to_not have_link('verify your account') + end + + scenario 'Create' do + login_as(author) + + visit new_spending_proposal_path + fill_in 'spending_proposal_title', with: 'Build a skyscraper' + fill_in 'spending_proposal_description', with: 'I want to live in a high tower over the clouds' + fill_in 'spending_proposal_external_url', with: 'http://http://skyscraperpage.com/' + fill_in 'spending_proposal_captcha', with: correct_captcha_text + select 'All city', from: 'spending_proposal_geozone_id' + check 'spending_proposal_terms_of_service' + + click_button 'Create' + + expect(page).to have_content 'Spending proposal created successfully' + end + + scenario 'Captcha is required for proposal creation' do + login_as(author) + + visit new_spending_proposal_path + fill_in 'spending_proposal_title', with: 'Build a skyscraper' + fill_in 'spending_proposal_description', with: 'I want to live in a high tower over the clouds' + fill_in 'spending_proposal_external_url', with: 'http://http://skyscraperpage.com/' + fill_in 'spending_proposal_captcha', with: 'wrongText' + check 'spending_proposal_terms_of_service' + + click_button 'Create' + + expect(page).to_not have_content 'Spending proposal created successfully' + expect(page).to have_content '1 error' + + fill_in 'spending_proposal_captcha', with: correct_captcha_text + click_button 'Create' + + expect(page).to have_content 'Spending proposal created successfully' + end + + scenario 'Errors on create' do + login_as(author) + + visit new_spending_proposal_path + click_button 'Create' + expect(page).to have_content error_message + end + +end diff --git a/spec/helpers/geozones_helper_spec.rb b/spec/helpers/geozones_helper_spec.rb new file mode 100644 index 000000000..d85ff8753 --- /dev/null +++ b/spec/helpers/geozones_helper_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +describe GeozonesHelper do + + describe "#geozones_name" do + let(:geozone) { create :geozone } + + + it "returns geozone name if present" do + spending_proposal = create(:spending_proposal, geozone: geozone) + expect(geozone_name(spending_proposal)).to eq geozone.name + end + + it "returns default string for no geozone if geozone is blank" do + spending_proposal = create(:spending_proposal, geozone: nil) + expect(geozone_name(spending_proposal)).to eq "All city" + end + end + + describe "#geozone_select_options" do + it "returns array of ids and names ordered by name" do + g1 = create(:geozone, name: "AAA") + g3 = create(:geozone, name: "CCC") + g2 = create(:geozone, name: "BBB") + + select_options = geozone_select_options + + expect(select_options.size).to eq 3 + expect(select_options.first).to eq [g1.name, g1.id] + expect(select_options[1]).to eq [g2.name, g2.id] + expect(select_options.last).to eq [g3.name, g3.id] + end + end + +end diff --git a/spec/helpers/notifications_helper_spec.rb b/spec/helpers/notifications_helper_spec.rb new file mode 100644 index 000000000..ccb3b3a9d --- /dev/null +++ b/spec/helpers/notifications_helper_spec.rb @@ -0,0 +1,25 @@ +require 'rails_helper' + +describe NotificationsHelper do + + describe "#notification_action" do + let(:debate) { create :debate } + let(:debate_comment) { create :comment, commentable: debate } + + context "when action was comment on a debate" do + it "returns correct text when someone comments on your debate" do + notification = create :notification, notifiable: debate + expect(notification_action(notification)).to eq "comments_on" + end + end + + context "when action was comment on a debate" do + it "returns correct text when someone replies to your comment" do + notification = create :notification, notifiable: debate_comment + expect(notification_action(notification)).to eq "replies_to" + end + end + end + + +end diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb index 66757ddc0..8df88a46f 100644 --- a/spec/models/abilities/administrator_spec.rb +++ b/spec/models/abilities/administrator_spec.rb @@ -51,4 +51,6 @@ describe "Abilities::Administrator" do it { should_not be_able_to(:comment_as_moderator, proposal) } it { should be_able_to(:manage, Annotation) } + + it { should be_able_to(:manage, SpendingProposal) } end diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb index 4775cd1ee..d942e2d18 100644 --- a/spec/models/abilities/common_spec.rb +++ b/spec/models/abilities/common_spec.rb @@ -28,6 +28,9 @@ describe "Abilities::Common" do it { should_not be_able_to(:vote, Proposal) } it { should_not be_able_to(:vote_featured, Proposal) } + it { should be_able_to(:index, SpendingProposal) } + it { should_not be_able_to(:create, SpendingProposal) } + it { should_not be_able_to(:comment_as_administrator, debate) } it { should_not be_able_to(:comment_as_moderator, debate) } it { should_not be_able_to(:comment_as_administrator, proposal) } @@ -84,6 +87,8 @@ describe "Abilities::Common" do it { should be_able_to(:vote, Proposal) } it { should be_able_to(:vote_featured, Proposal) } + + it { should be_able_to(:create, SpendingProposal) } end describe "when level 3 verified" do @@ -91,5 +96,7 @@ describe "Abilities::Common" do it { should be_able_to(:vote, Proposal) } it { should be_able_to(:vote_featured, Proposal) } + + it { should be_able_to(:create, SpendingProposal) } end end diff --git a/spec/models/abilities/everyone_spec.rb b/spec/models/abilities/everyone_spec.rb index 4c532b7a4..3f1e57278 100644 --- a/spec/models/abilities/everyone_spec.rb +++ b/spec/models/abilities/everyone_spec.rb @@ -21,4 +21,9 @@ describe "Abilities::Everyone" do it { should_not be_able_to(:vote, Proposal) } it { should_not be_able_to(:flag, Proposal) } it { should_not be_able_to(:unflag, Proposal) } + + it { should be_able_to(:show, Comment) } + + it { should be_able_to(:index, SpendingProposal) } + it { should_not be_able_to(:create, SpendingProposal) } end diff --git a/spec/models/comment_spec.rb b/spec/models/comment_spec.rb index 11a02400a..73b70fb42 100644 --- a/spec/models/comment_spec.rb +++ b/spec/models/comment_spec.rb @@ -128,4 +128,5 @@ describe Comment do expect(Comment.not_as_admin_or_moderator.first).to eq(comment1) end end + end diff --git a/spec/models/geozone_spec.rb b/spec/models/geozone_spec.rb new file mode 100644 index 000000000..a29c4c918 --- /dev/null +++ b/spec/models/geozone_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe Geozone, type: :model do + let(:geozone) { build(:geozone) } + + it "should be valid" do + expect(geozone).to be_valid + end + + it "should not be valid without a name" do + geozone.name = nil + expect(geozone).to_not be_valid + end +end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb new file mode 100644 index 000000000..361211a51 --- /dev/null +++ b/spec/models/notification_spec.rb @@ -0,0 +1,50 @@ +require 'rails_helper' + +describe Notification do + + describe "#unread (scope)" do + it "returns only unread notifications" do + 2.times { create :notification } + expect(Notification.unread.size).to be 2 + end + end + + describe "#recent (scope)" do + it "returns notifications sorted by id descendant" do + old_notification = create :notification + new_notification = create :notification + + sorted_notifications = Notification.recent + expect(sorted_notifications.size).to be 2 + expect(sorted_notifications.first).to eq new_notification + expect(sorted_notifications.last).to eq old_notification + end + end + + describe "#for_render (scope)" do + it "returns notifications including notifiable and user" do + expect(Notification).to receive(:includes).with(:notifiable).exactly(:once) + Notification.for_render + end + end + + describe "#timestamp" do + it "returns the timestamp of the trackable object" do + comment = create :comment + notification = create :notification, notifiable: comment + + expect(notification.timestamp).to eq comment.created_at + end + end + + describe "#mark_as_read" do + it "destroys notification" do + notification = create :notification + expect(Notification.unread.size).to eq 1 + + notification.mark_as_read + expect(Notification.unread.size).to eq 0 + end + end + +end diff --git a/spec/models/spending_proposal_spec.rb b/spec/models/spending_proposal_spec.rb new file mode 100644 index 000000000..25bccbfc6 --- /dev/null +++ b/spec/models/spending_proposal_spec.rb @@ -0,0 +1,140 @@ +require 'rails_helper' + +describe SpendingProposal do + let(:spending_proposal) { build(:spending_proposal) } + + it "should be valid" do + expect(spending_proposal).to be_valid + end + + it "should not be valid without an author" do + spending_proposal.author = nil + expect(spending_proposal).to_not be_valid + end + + describe "#title" do + it "should not be valid without a title" do + spending_proposal.title = nil + expect(spending_proposal).to_not be_valid + end + + it "should not be valid when very short" do + spending_proposal.title = "abc" + expect(spending_proposal).to_not be_valid + end + + it "should not be valid when very long" do + spending_proposal.title = "a" * 81 + expect(spending_proposal).to_not be_valid + end + end + + describe "#description" do + it "should be sanitized" do + spending_proposal.description = "" + spending_proposal.valid? + expect(spending_proposal.description).to eq("alert('danger');") + end + + it "should not be valid when very long" do + spending_proposal.description = "a" * 6001 + expect(spending_proposal).to_not be_valid + end + end + + describe "resolution status" do + it "should be valid" do + spending_proposal.resolution = "accepted" + expect(spending_proposal).to be_valid + spending_proposal.resolution = "rejected" + expect(spending_proposal).to be_valid + spending_proposal.resolution = "wrong" + expect(spending_proposal).to_not be_valid + end + + it "can be accepted" do + spending_proposal.accept + expect(spending_proposal.reload.resolution).to eq("accepted") + end + + it "can be rejected" do + spending_proposal.reject + expect(spending_proposal.reload.resolution).to eq("rejected") + end + + describe "#accepted?" do + it "should be true if resolution equals 'accepted'" do + spending_proposal.resolution = "accepted" + expect(spending_proposal.accepted?).to eq true + end + + it "should be false otherwise" do + spending_proposal.resolution = "rejected" + expect(spending_proposal.accepted?).to eq false + spending_proposal.resolution = nil + expect(spending_proposal.accepted?).to eq false + end + end + + describe "#rejected?" do + it "should be true if resolution equals 'rejected'" do + spending_proposal.resolution = "rejected" + expect(spending_proposal.rejected?).to eq true + end + + it "should be false otherwise" do + spending_proposal.resolution = "accepted" + expect(spending_proposal.rejected?).to eq false + spending_proposal.resolution = nil + expect(spending_proposal.rejected?).to eq false + end + end + + describe "#unresolved?" do + it "should be true if resolution is blank" do + spending_proposal.resolution = nil + expect(spending_proposal.unresolved?).to eq true + end + + it "should be false otherwise" do + spending_proposal.resolution = "accepted" + expect(spending_proposal.unresolved?).to eq false + spending_proposal.resolution = "rejected" + expect(spending_proposal.unresolved?).to eq false + end + end + end + + describe "scopes" do + before(:each) do + 2.times { create(:spending_proposal, resolution: "accepted") } + 2.times { create(:spending_proposal, resolution: "rejected") } + 2.times { create(:spending_proposal, resolution: nil) } + end + + describe "unresolved" do + it "should return all spending proposals without resolution" do + unresolved = SpendingProposal.all.unresolved + expect(unresolved.size).to eq(2) + unresolved.each {|u| expect(u.resolution).to be_nil} + end + end + + describe "accepted" do + it "should return all accepted spending proposals" do + accepted = SpendingProposal.all.accepted + expect(accepted.size).to eq(2) + accepted.each {|a| expect(a.resolution).to eq("accepted")} + end + end + + describe "rejected" do + it "should return all rejected spending proposals" do + rejected = SpendingProposal.all.rejected + expect(rejected.size).to eq(2) + rejected.each {|r| expect(r.resolution).to eq("rejected")} + end + end + end + +end