From d25d6c178c2ed84ab1a06d0026efbc7e5af5f8d2 Mon Sep 17 00:00:00 2001 From: Amaia Castro Date: Tue, 20 Dec 2016 18:52:43 +0100 Subject: [PATCH] Legislation Questions model and basic admin --- Gemfile | 2 + Gemfile.lock | 2 + app/assets/javascripts/application.js | 1 + .../legislation/draft_versions_controller.rb | 1 - .../admin/legislation/questions_controller.rb | 38 ++++++++ app/models/abilities/administrator.rb | 1 + app/models/comment.rb | 2 +- app/models/legislation/process.rb | 1 + app/models/legislation/question.rb | 12 +++ app/models/legislation/question_option.rb | 8 ++ .../legislation/draft_versions/_form.html.erb | 2 +- .../legislation/draft_versions/edit.html.erb | 2 +- .../legislation/draft_versions/new.html.erb | 3 +- .../legislation/processes/_subnav.html.erb | 3 + .../legislation/questions/_form.html.erb | 46 ++++++++++ .../_question_option_fields.html.erb | 6 ++ .../admin/legislation/questions/edit.html.erb | 20 +++++ .../legislation/questions/index.html.erb | 42 +++++++++ .../admin/legislation/questions/new.html.erb | 16 ++++ config/i18n-tasks.yml | 1 + config/locales/admin.en.yml | 27 +++++- config/locales/admin.es.yml | 25 ++++++ config/routes.rb | 1 + ...1220120037_create_legislation_questions.rb | 23 +++++ db/schema.rb | 30 ++++++- spec/factories.rb | 10 +++ .../admin/legislation/questions_spec.rb | 89 +++++++++++++++++++ .../legislation/question_option_spec.rb | 18 ++++ spec/models/legislation/question_spec.rb | 9 ++ 29 files changed, 431 insertions(+), 10 deletions(-) create mode 100644 app/controllers/admin/legislation/questions_controller.rb create mode 100644 app/models/legislation/question.rb create mode 100644 app/models/legislation/question_option.rb create mode 100644 app/views/admin/legislation/questions/_form.html.erb create mode 100644 app/views/admin/legislation/questions/_question_option_fields.html.erb create mode 100644 app/views/admin/legislation/questions/edit.html.erb create mode 100644 app/views/admin/legislation/questions/index.html.erb create mode 100644 app/views/admin/legislation/questions/new.html.erb create mode 100644 db/migrate/20161220120037_create_legislation_questions.rb create mode 100644 spec/features/admin/legislation/questions_spec.rb create mode 100644 spec/models/legislation/question_option_spec.rb create mode 100644 spec/models/legislation/question_spec.rb diff --git a/Gemfile b/Gemfile index 6eb34093c..9ac7e06a9 100644 --- a/Gemfile +++ b/Gemfile @@ -67,6 +67,8 @@ gem 'redcarpet' gem 'rails-assets-markdown-it', source: 'https://rails-assets.org' +gem 'cocoon' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index fb6eeb62f..28f67e75d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,6 +104,7 @@ GEM cliver (0.3.2) cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) + cocoon (1.2.9) coffee-rails (4.2.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.2.x) @@ -467,6 +468,7 @@ DEPENDENCIES capistrano3-delayed-job (~> 1.0) capybara ckeditor (~> 4.2.0) + cocoon coffee-rails (~> 4.2.1) coveralls daemons diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index a23c3ed19..0ec556717 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -48,6 +48,7 @@ //= require social_share //= require markdown-it //= require markdown_editor +//= require cocoon //= require custom var initialize_modules = function() { diff --git a/app/controllers/admin/legislation/draft_versions_controller.rb b/app/controllers/admin/legislation/draft_versions_controller.rb index 8c20374d9..f47dde361 100644 --- a/app/controllers/admin/legislation/draft_versions_controller.rb +++ b/app/controllers/admin/legislation/draft_versions_controller.rb @@ -31,7 +31,6 @@ class Admin::Legislation::DraftVersionsController < Admin::Legislation::BaseCont def draft_version_params params.require(:legislation_draft_version).permit( - :legislation_process_id, :title, :changelog, :status, diff --git a/app/controllers/admin/legislation/questions_controller.rb b/app/controllers/admin/legislation/questions_controller.rb new file mode 100644 index 000000000..a643c39f5 --- /dev/null +++ b/app/controllers/admin/legislation/questions_controller.rb @@ -0,0 +1,38 @@ +class Admin::Legislation::QuestionsController < Admin::Legislation::BaseController + load_and_authorize_resource :process, class: "Legislation::Process" + load_and_authorize_resource :question, class: "Legislation::Question", through: :process + + def index + @questions = @process.questions + end + + def create + if @question.save + redirect_to admin_legislation_process_questions_path + else + render :new + end + end + + def update + if @question.update(question_params) + redirect_to admin_legislation_process_questions_path + else + render :edit + end + end + + def destroy + @question.destroy + redirect_to admin_legislation_process_questions_path + end + + private + + def question_params + params.require(:legislation_question).permit( + :title, + question_options_attributes: [:id, :value, :_destroy] + ) + end +end diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 82035042b..3c7adb145 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -48,6 +48,7 @@ module Abilities can [:manage], ::Legislation::Process can [:manage], ::Legislation::DraftVersion + can [:manage], ::Legislation::Question end end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 0bfb6a320..025f50e71 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -10,7 +10,7 @@ class Comment < ActiveRecord::Base validates :body, presence: true validates :user, presence: true - validates_inclusion_of :commentable_type, in: ["Debate", "Proposal"] + validates_inclusion_of :commentable_type, in: ["Debate", "Proposal", "Legislation::Question"] validate :validate_body_length diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 43cca02f6..ac303b37d 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -3,6 +3,7 @@ class Legislation::Process < ActiveRecord::Base include ActsAsParanoidAliases has_many :draft_versions, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id' + has_many :questions, class_name: 'Legislation::Question', foreign_key: 'legislation_process_id' validates :title, presence: true validates :description, presence: true diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb new file mode 100644 index 000000000..d139d75b2 --- /dev/null +++ b/app/models/legislation/question.rb @@ -0,0 +1,12 @@ +class Legislation::Question < ActiveRecord::Base + belongs_to :process, class_name: 'Legislation::Process', foreign_key: 'legislation_process_id' + + has_many :question_options, class_name: 'Legislation::QuestionOption', foreign_key: 'legislation_question_id', dependent: :destroy, inverse_of: :question + # has_many :answers + has_many :comments, as: :commentable + + accepts_nested_attributes_for :question_options, :reject_if => proc { |attributes| attributes[:value].blank? }, allow_destroy: true + + validates :process, presence: true + validates :title, presence: true +end diff --git a/app/models/legislation/question_option.rb b/app/models/legislation/question_option.rb new file mode 100644 index 000000000..6d3654877 --- /dev/null +++ b/app/models/legislation/question_option.rb @@ -0,0 +1,8 @@ +class Legislation::QuestionOption < ActiveRecord::Base + belongs_to :question, class_name: 'Legislation::Question', foreign_key: 'legislation_question_id', inverse_of: :question_options + + # has_many :answers + + validates :question, presence: true + validates :value, presence: true, uniqueness: { scope: :legislation_question_id } +end diff --git a/app/views/admin/legislation/draft_versions/_form.html.erb b/app/views/admin/legislation/draft_versions/_form.html.erb index fb61ca3d1..a55bddc66 100644 --- a/app/views/admin/legislation/draft_versions/_form.html.erb +++ b/app/views/admin/legislation/draft_versions/_form.html.erb @@ -9,7 +9,7 @@ <%= @draft_version.errors.count %> - <%= t("admin.legislation.draft_versions.errors.form.error", count: @process.errors.count) %> + <%= t("admin.legislation.draft_versions.errors.form.error", count: @draft_version.errors.count) %> diff --git a/app/views/admin/legislation/draft_versions/edit.html.erb b/app/views/admin/legislation/draft_versions/edit.html.erb index b48c6409b..f347101cf 100644 --- a/app/views/admin/legislation/draft_versions/edit.html.erb +++ b/app/views/admin/legislation/draft_versions/edit.html.erb @@ -1,6 +1,6 @@
- <%= link_to admin_legislation_processes_path, class: "back" do %> + <%= link_to admin_legislation_process_path(@process), class: "back" do %> <%= t("admin.legislation.draft_versions.edit.back") %> <% end %> diff --git a/app/views/admin/legislation/draft_versions/new.html.erb b/app/views/admin/legislation/draft_versions/new.html.erb index c1ee1be90..7f7562339 100644 --- a/app/views/admin/legislation/draft_versions/new.html.erb +++ b/app/views/admin/legislation/draft_versions/new.html.erb @@ -1,6 +1,6 @@
- <%= link_to admin_legislation_processes_path, class: "back" do %> + <%= link_to admin_legislation_process_path(@process), class: "back" do %> <%= t("admin.legislation.draft_versions.new.back") %> <% end %> @@ -12,6 +12,5 @@

<%= t("admin.legislation.draft_versions.new.title") %>

<%= render 'form', url: admin_legislation_process_draft_versions_path(@process) %> -
diff --git a/app/views/admin/legislation/processes/_subnav.html.erb b/app/views/admin/legislation/processes/_subnav.html.erb index a645faef4..97640c4f4 100644 --- a/app/views/admin/legislation/processes/_subnav.html.erb +++ b/app/views/admin/legislation/processes/_subnav.html.erb @@ -1,6 +1,9 @@