From eca25e220094252b6ae555b4987031aa6d8649eb Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Thu, 31 Jul 2014 16:48:13 +0200 Subject: [PATCH 01/18] fixes #40 empty breadcrumb names --- app/controllers/helena/application_controller.rb | 4 ++++ config/locales/en.yml | 1 + .../helena/application_controller_spec.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 spec/controllers/helena/application_controller_spec.rb diff --git a/app/controllers/helena/application_controller.rb b/app/controllers/helena/application_controller.rb index 9fc1eff..e5c3143 100644 --- a/app/controllers/helena/application_controller.rb +++ b/app/controllers/helena/application_controller.rb @@ -7,5 +7,9 @@ class ApplicationController < ::ApplicationController def authenticate_administrator fail(ActionController::RoutingError, 'Access Denied') unless can_administer? end + + def add_breadcrumb(name, path = nil, options = {}) + super(name.presence || t('shared.untitled'), path, options) + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 718f885..14bb2a7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -10,6 +10,7 @@ en: code_hint: The code must consist only of lowercase letters, numbers and underscores. Don't begin with a digit or an underscore and don't end with an underscore. move_up: Move up move_down: Move down + untitled: Untitled actions: deleted: "'%{resource}' successfully deleted" diff --git a/spec/controllers/helena/application_controller_spec.rb b/spec/controllers/helena/application_controller_spec.rb new file mode 100644 index 0000000..0a4ccad --- /dev/null +++ b/spec/controllers/helena/application_controller_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe Helena::ApplicationController do + describe '.add_breadcrumb' do + it 'names an empty element "Untitled"' do + application_controller = Helena::ApplicationController.new + + application_controller.add_breadcrumb('') + breadcrumbs = application_controller.add_breadcrumb(nil) + + expect(breadcrumbs.first.name).to eq 'Untitled' + expect(breadcrumbs.last.name).to eq 'Untitled' + end + end +end From 8484d3cea8da113ad7bbd1e37ad55ee8665c6a34 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Thu, 31 Jul 2014 17:09:27 +0200 Subject: [PATCH 02/18] cleanup nasty code --- .rubocop.yml | 10 +++++----- app/controllers/helena/sessions_controller.rb | 13 ++++++------- app/models/helena/concerns/application_model.rb | 2 +- .../concerns/questions/validates_one_label.rb | 7 +++---- app/models/helena/question.rb | 4 ++-- app/models/helena/sub_question.rb | 5 +---- .../helena/admin/survey_controller_spec.rb | 2 +- spec/features/helena/admin/manage_question_spec.rb | 6 +++--- 8 files changed, 22 insertions(+), 27 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a30c212..e7922a0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,8 +1,8 @@ AllCops: - Includes: - - Rakefile - - config.ru - Excludes: + Include: + - '**/Rakefile' + - '**/config.ru' + Exclude: - bin/** - spec/dummy/db/schema.rb - spec/dummy/config/initializers/secret_token.rb @@ -11,6 +11,6 @@ AllCops: Documentation: Enabled: false LineLength: - Enabled: true + Enabled: true Max: 160 diff --git a/app/controllers/helena/sessions_controller.rb b/app/controllers/helena/sessions_controller.rb index 2d2a677..6c5189c 100644 --- a/app/controllers/helena/sessions_controller.rb +++ b/app/controllers/helena/sessions_controller.rb @@ -58,9 +58,8 @@ def session_answers end def session_params - if params[:session] - params.require(:session).permit(answers: @question_group.question_codes, question_types: @question_group.question_codes) - end + return unless params[:session] + params.require(:session).permit(answers: @question_group.question_codes, question_types: @question_group.question_codes) end def question_group_questions @@ -73,10 +72,10 @@ def update_answers value = session_params[:answers][question_code] if session_params - unless value.blank? - answer = Helena::Answer.build_generic(question_code, value, request.remote_ip) - @session.answers << answer - end + next if value.blank? + + answer = Helena::Answer.build_generic(question_code, value, request.remote_ip) + @session.answers << answer end session_answers end diff --git a/app/models/helena/concerns/application_model.rb b/app/models/helena/concerns/application_model.rb index 804f7c7..5858472 100644 --- a/app/models/helena/concerns/application_model.rb +++ b/app/models/helena/concerns/application_model.rb @@ -7,7 +7,7 @@ module ApplicationModel include Mongoid::Timestamps included do - before_destroy :removable? + before_destroy :removable? # Removable is given by default. Override for custom behaviour def removable? diff --git a/app/models/helena/concerns/questions/validates_one_label.rb b/app/models/helena/concerns/questions/validates_one_label.rb index d804660..1c43dda 100644 --- a/app/models/helena/concerns/questions/validates_one_label.rb +++ b/app/models/helena/concerns/questions/validates_one_label.rb @@ -12,10 +12,9 @@ module ValidatesOneLabel def labels_preselection selected_labels = labels.select { |label| label.preselected == true } - if selected_labels.size > 1 - selected_labels.each { |label| label.errors.add(:preselected, I18n.t(:taken, scope: 'activerecord.errors.messages')) } - errors.add(:labels, I18n.t('helena.admin.radio_group.only_one_preselection_allowed')) - end + return if selected_labels.size <= 1 + selected_labels.each { |label| label.errors.add(:preselected, I18n.t(:taken, scope: 'activerecord.errors.messages')) } + errors.add(:labels, I18n.t('helena.admin.radio_group.only_one_preselection_allowed')) end end end diff --git a/app/models/helena/question.rb b/app/models/helena/question.rb index aec95a2..f2ba9f8 100644 --- a/app/models/helena/question.rb +++ b/app/models/helena/question.rb @@ -29,8 +29,8 @@ class Question validates :code, presence: true - # consist of lowercase characters or digits, not starting with a digit or underscore and not ending with an underscore - # foo_32: correct, 32_foo: incorrect, _bar: incorrect, bar_: incorrect, FooBaar: incorrect + # consist of lowercase characters or digits, not starting with a digit or underscore and not ending with an underscore + # foo_32: correct, 32_foo: incorrect, _bar: incorrect, bar_: incorrect, FooBaar: incorrect validates :code, format: { with: CODE_FORMAT } validate :uniqueness_of_code diff --git a/app/models/helena/sub_question.rb b/app/models/helena/sub_question.rb index 0e153a7..5060aff 100644 --- a/app/models/helena/sub_question.rb +++ b/app/models/helena/sub_question.rb @@ -18,10 +18,7 @@ class SubQuestion def uniqueness_of_code question_code_occurences = question.question_group.version.question_code_occurences - - if question_code_occurences[code] > 1 - errors.add :code, :taken, value: code - end + errors.add(:code, :taken, value: code) if question_code_occurences[code] > 1 end end end diff --git a/spec/controllers/helena/admin/survey_controller_spec.rb b/spec/controllers/helena/admin/survey_controller_spec.rb index c9254a8..13c829d 100644 --- a/spec/controllers/helena/admin/survey_controller_spec.rb +++ b/spec/controllers/helena/admin/survey_controller_spec.rb @@ -78,7 +78,7 @@ end it 'counts position up when creating a new survey' do - post :create, survey: { name: 'New Survey' , language: 'en' } + post :create, survey: { name: 'New Survey', language: 'en' } expect(first_survey.reload.position).to eq 1 expect(second_survey.reload.position).to eq 2 diff --git a/spec/features/helena/admin/manage_question_spec.rb b/spec/features/helena/admin/manage_question_spec.rb index 7fdaa0a..8cc48c5 100644 --- a/spec/features/helena/admin/manage_question_spec.rb +++ b/spec/features/helena/admin/manage_question_spec.rb @@ -19,7 +19,7 @@ end within '.breadcrumb' do - expect(page).to have_link'Surveys', href: helena.admin_surveys_path + expect(page).to have_link 'Surveys', href: helena.admin_surveys_path expect(page).to have_link draft_version.survey.name, href: helena.admin_survey_question_groups_path(draft_version.survey) expect(page).to have_text question_group.title end @@ -84,7 +84,7 @@ expect { click_link 'Move down' }.to change { second_question.reload.position }.from(1).to(2) end - within "#helena_#{dom_id second_question}" do + within "#helena_#{dom_id second_question}" do expect { click_link 'Move up' }.to change { second_question.reload.position }.from(2).to(1) end @@ -98,7 +98,7 @@ visit helena.admin_survey_question_group_questions_path(draft_version.survey, question_group) - within"#helena_#{dom_id question}" do + within "#helena_#{dom_id question}" do expect { click_link 'Delete' }.to change { question_group.reload.questions.count }.by(-1) end end From d81a1984d742a2fb41f7b77fc392bbdd7e49a9d8 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Thu, 31 Jul 2014 17:10:50 +0200 Subject: [PATCH 03/18] bundle update --- Gemfile.lock | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 295562c..5976ce1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - helena (0.3.1) + helena (0.3.2) bootstrap-sass breadcrumbs_on_rails haml @@ -51,7 +51,7 @@ GEM erubis (>= 2.6.6) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) - bootstrap-sass (3.2.0.0) + bootstrap-sass (3.2.0.1) sass (~> 3.2) breadcrumbs_on_rails (2.3.0) bson (2.3.0) @@ -106,11 +106,12 @@ GEM highline (1.6.21) hike (1.2.3) i18n (0.6.11) - i18n-tasks (0.6.3) + i18n-tasks (0.7.1) activesupport easy_translate (>= 0.5.0) erubis highline + i18n slop (>= 3.5.0) term-ansicolor terminal-table From b912699a0be3cdede25419e0e37ef0ec782dc4c2 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Thu, 31 Jul 2014 17:11:01 +0200 Subject: [PATCH 04/18] bump version --- lib/helena/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helena/version.rb b/lib/helena/version.rb index 3aa47a1..007d43f 100644 --- a/lib/helena/version.rb +++ b/lib/helena/version.rb @@ -1,3 +1,3 @@ module Helena - VERSION = '0.3.1' + VERSION = '0.3.2' end From 47385ae1125b7c9119b3bd29f0d3eef51a2b1be9 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Thu, 31 Jul 2014 17:43:13 +0200 Subject: [PATCH 05/18] switched to byebug because debugger lacks in support of mri-2.1.2 https://github.com/cldwalker/debugger/issues/125#issuecomment-43353446 --- Gemfile | 2 +- Gemfile.lock | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index fd43e40..15dd166 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gemspec # To use debugger group :development, :test do gem 'pry' - gem 'pry-debugger' + gem 'pry-byebug' gem 'ruby-progressbar' gem 'colorize' gem 'jquery-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 5976ce1..581df5d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,9 @@ GEM breadcrumbs_on_rails (2.3.0) bson (2.3.0) builder (3.2.2) + byebug (2.7.0) + columnize (~> 0.3) + debugger-linecache (~> 1.2) capybara (2.4.1) mime-types (>= 1.16) nokogiri (>= 1.3.3) @@ -76,12 +79,7 @@ GEM thor database_cleaner (1.3.0) debug_inspector (0.0.2) - debugger (1.6.8) - columnize (>= 0.3.1) - debugger-linecache (~> 1.2.0) - debugger-ruby_core_source (~> 1.3.5) debugger-linecache (1.2.0) - debugger-ruby_core_source (1.3.5) diff-lcs (1.2.5) docile (1.1.5) easy_translate (0.5.0) @@ -157,9 +155,9 @@ GEM coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - pry-debugger (0.2.3) - debugger (~> 1.3) - pry (>= 0.9.10, < 0.11.0) + pry-byebug (1.3.3) + byebug (~> 2.7) + pry (~> 0.10) quiet_assets (1.0.3) railties (>= 3.1, < 5.0) rack (1.5.2) @@ -277,7 +275,7 @@ DEPENDENCIES launchy mongoid-rspec pry - pry-debugger + pry-byebug quiet_assets rspec-collection_matchers rspec-rails From a93e508d0b24f211e8a697d5e88a49d049494bc0 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 09:26:21 +0200 Subject: [PATCH 06/18] exclude some gems to speed up travis --- Gemfile | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 15dd166..e9a8fec 100644 --- a/Gemfile +++ b/Gemfile @@ -12,19 +12,22 @@ gemspec # To use debugger group :development, :test do - gem 'pry' - gem 'pry-byebug' - gem 'ruby-progressbar' - gem 'colorize' + unless ENV['TRAVIS'] + gem 'pry' + gem 'pry-byebug' + gem 'ruby-progressbar' + gem 'colorize' + gem 'quiet_assets' + gem 'better_errors' + gem 'launchy' + gem 'binding_of_caller' + gem 'selenium-webdriver' + gem 'i18n-tasks' + end + gem 'jquery-rails' gem 'bootstrap-sass' gem 'simple_form' gem 'breadcrumbs_on_rails' - gem 'quiet_assets' - gem 'better_errors' - gem 'launchy' - gem 'binding_of_caller' gem 'coveralls', require: false - gem 'selenium-webdriver' - gem 'i18n-tasks' end From 957cba4b6b495a012d128fed952949f92f2fd0e5 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 09:41:30 +0200 Subject: [PATCH 07/18] remove Gemfile.lock before install --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0dd3b08..d247f77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,5 @@ rvm: services: - mongodb + +before_install: rm Gemfile.lock || true From 85616b57edb886c2c8b633e23b8e057a0f438489 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 10:23:16 +0200 Subject: [PATCH 08/18] added more space for the button --- app/views/helena/admin/surveys/_form.html.haml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/helena/admin/surveys/_form.html.haml b/app/views/helena/admin/surveys/_form.html.haml index ceb755a..bfa6672 100644 --- a/app/views/helena/admin/surveys/_form.html.haml +++ b/app/views/helena/admin/surveys/_form.html.haml @@ -7,4 +7,5 @@ = v.simple_fields_for :survey_detail do |detail| = detail.input :title = detail.input :description, as: :text, input_html: { cols: 60, rows: 10 } + %br = f.submit t('shared.save'), class: 'btn btn-success' From 042a4edaf96a47ee0ca8ae9cf124cfd528846e68 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 10:24:01 +0200 Subject: [PATCH 09/18] map wrapper automatically for booleans --- app/views/helena/admin/questions/_form.html.haml | 2 +- app/views/helena/admin/questions/_labels.html.haml | 4 ++-- app/views/helena/admin/questions/_sub_questions.html.haml | 2 +- config/initializers/simple_form_bootstrap.rb | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/views/helena/admin/questions/_form.html.haml b/app/views/helena/admin/questions/_form.html.haml index 840c084..75b4b7c 100644 --- a/app/views/helena/admin/questions/_form.html.haml +++ b/app/views/helena/admin/questions/_form.html.haml @@ -3,7 +3,7 @@ = f.input :code, hint: t('shared.code_hint') = f.input :_type, collection: Helena::Question::TYPES.map { |type| [type.model_name.human, type] } - if @question.respond_to? :required - = f.input :required, as: :boolean, wrapper: :checkbox + = f.input :required, as: :boolean - if @question.is_a?(Helena::Questions::LongText) || @question.is_a?(Helena::Questions::StaticText) = f.input :default_value - if @question.is_a?(Helena::Questions::ShortText) diff --git a/app/views/helena/admin/questions/_labels.html.haml b/app/views/helena/admin/questions/_labels.html.haml index ccc1f2a..606146e 100644 --- a/app/views/helena/admin/questions/_labels.html.haml +++ b/app/views/helena/admin/questions/_labels.html.haml @@ -14,11 +14,11 @@ %td.position = label.input :position, as: :integer, label: false %td.preselected - = label.input :preselected, as: :boolean, wrapper: :checkbox, label: Helena::Label.human_attribute_name(:preselected) + = label.input :preselected, as: :boolean, label: Helena::Label.human_attribute_name(:preselected) %td.text = label.input :text, label: false %td.value = label.input :value, label: false %td.delete - unless label.object.new_record? - = label.input :_destroy, as: :boolean, label: Helena::Label.human_attribute_name(:_destroy), wrapper: :checkbox + = label.input :_destroy, as: :boolean, label: Helena::Label.human_attribute_name(:_destroy) diff --git a/app/views/helena/admin/questions/_sub_questions.html.haml b/app/views/helena/admin/questions/_sub_questions.html.haml index 65f091e..f48a461 100644 --- a/app/views/helena/admin/questions/_sub_questions.html.haml +++ b/app/views/helena/admin/questions/_sub_questions.html.haml @@ -20,7 +20,7 @@ = sub_question.input :code, label: false, hint: t('shared.code_hint') - if @question.is_a?(Helena::Questions::CheckboxGroup) %td.preselected - = sub_question.input :preselected, as: :boolean, wrapper: :checkbox + = sub_question.input :preselected, as: :boolean %td.text = sub_question.input :text, label: false - if @question.is_a?(Helena::Questions::CheckboxGroup) diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index 27d6857..5036c5a 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -9,6 +9,7 @@ b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end + config.wrapper_mappings = { boolean: :checkbox } config.wrappers :checkbox, tag: :div, class: 'checkbox', error_class: 'has-error' do |b| b.use :html5 b.wrapper tag: :label do |ba| From 69c32fe8ab2b099c87959a6f0d154c18cf9f5669 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 11:20:35 +0200 Subject: [PATCH 10/18] little dirty hack to fix #41 --- config/initializers/simple_form.rb | 27 +++++++++++++++++++- config/initializers/simple_form_bootstrap.rb | 5 +--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index bbb49a2..495ffad 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -1,3 +1,28 @@ +# FIXME: Dirty ugly hack https://gist.github.com/tokenvolt/6599141 +inputs = %w[ + CollectionSelectInput + DateTimeInput + FileInput + GroupedCollectionSelectInput + NumericInput + PasswordInput + RangeInput + StringInput + TextInput +] + +inputs.each do |input_type| + superclass = "SimpleForm::Inputs::#{input_type}".constantize + + new_class = Class.new(superclass) do + def input_html_classes + super.push('form-control') + end + end + + Object.const_set(input_type, new_class) +end + # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| # Wrappers are used by the form builder to generate a @@ -140,5 +165,5 @@ # config.cache_discovery = !Rails.env.development? # Default class for inputs - config.input_class = 'form-control' + config.input_class = '' end diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index 5036c5a..182da49 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -12,10 +12,7 @@ config.wrapper_mappings = { boolean: :checkbox } config.wrappers :checkbox, tag: :div, class: 'checkbox', error_class: 'has-error' do |b| b.use :html5 - b.wrapper tag: :label do |ba| - ba.use :input - ba.use :label_text - end + b.use :label_input # FIXME: shouldn't be such a hack https://gist.github.com/tokenvolt/6599141#comment-1275095 b.use :hint, wrap_with: { tag: :p, class: 'help-block' } b.use :error, wrap_with: { tag: :span, class: 'help-block text-danger' } From fd5550f83b9125bf454dcd4b4e4cfc70fc1ca275 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 11:21:02 +0200 Subject: [PATCH 11/18] gem update --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 581df5d..601967b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,7 +104,7 @@ GEM highline (1.6.21) hike (1.2.3) i18n (0.6.11) - i18n-tasks (0.7.1) + i18n-tasks (0.7.2) activesupport easy_translate (>= 0.5.0) erubis From 7887598b9d4850e2a33eed2eaad81c3dfce48fdd Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 11:54:03 +0200 Subject: [PATCH 12/18] using proper implementation of simple form with bootstrap using simple_form 3.1.0.rc2 --- Gemfile | 23 ++-- Gemfile.lock | 5 +- .../admin/questions/_sub_questions.html.haml | 3 +- config/initializers/simple_form.rb | 52 +++---- config/initializers/simple_form_bootstrap.rb | 130 ++++++++++++++---- helena.gemspec | 2 +- 6 files changed, 140 insertions(+), 75 deletions(-) diff --git a/Gemfile b/Gemfile index e9a8fec..aeadbe0 100644 --- a/Gemfile +++ b/Gemfile @@ -12,22 +12,19 @@ gemspec # To use debugger group :development, :test do - unless ENV['TRAVIS'] - gem 'pry' - gem 'pry-byebug' - gem 'ruby-progressbar' - gem 'colorize' - gem 'quiet_assets' - gem 'better_errors' - gem 'launchy' - gem 'binding_of_caller' - gem 'selenium-webdriver' - gem 'i18n-tasks' - end + gem 'pry' + gem 'pry-byebug' + gem 'ruby-progressbar' + gem 'colorize' + gem 'quiet_assets' + gem 'better_errors' + gem 'launchy' + gem 'binding_of_caller' + gem 'selenium-webdriver' + gem 'i18n-tasks' gem 'jquery-rails' gem 'bootstrap-sass' - gem 'simple_form' gem 'breadcrumbs_on_rails' gem 'coveralls', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index 601967b..ccf7743 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -13,7 +13,7 @@ PATH rails (~> 4.1.1) rails-i18n sass-rails (~> 4.0.3) - simple_form + simple_form (~> 3.1.0.rc2) GEM remote: https://rubygems.org/ @@ -220,7 +220,7 @@ GEM multi_json (~> 1.0) rubyzip (~> 1.0) websocket (~> 1.0.4) - simple_form (3.0.2) + simple_form (3.1.0.rc2) actionpack (~> 4.0) activemodel (~> 4.0) simplecov (0.9.0) @@ -281,4 +281,3 @@ DEPENDENCIES rspec-rails ruby-progressbar selenium-webdriver - simple_form diff --git a/app/views/helena/admin/questions/_sub_questions.html.haml b/app/views/helena/admin/questions/_sub_questions.html.haml index f48a461..da6f742 100644 --- a/app/views/helena/admin/questions/_sub_questions.html.haml +++ b/app/views/helena/admin/questions/_sub_questions.html.haml @@ -20,7 +20,7 @@ = sub_question.input :code, label: false, hint: t('shared.code_hint') - if @question.is_a?(Helena::Questions::CheckboxGroup) %td.preselected - = sub_question.input :preselected, as: :boolean + = sub_question.input :preselected %td.text = sub_question.input :text, label: false - if @question.is_a?(Helena::Questions::CheckboxGroup) @@ -29,5 +29,4 @@ %td.delete - unless sub_question.object.new_record? = sub_question.input :_destroy, as: :boolean, - wrapper: :checkbox, label: Helena::SubQuestion.human_attribute_name(:_destroy) diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index 495ffad..bf7be1c 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -1,28 +1,3 @@ -# FIXME: Dirty ugly hack https://gist.github.com/tokenvolt/6599141 -inputs = %w[ - CollectionSelectInput - DateTimeInput - FileInput - GroupedCollectionSelectInput - NumericInput - PasswordInput - RangeInput - StringInput - TextInput -] - -inputs.each do |input_type| - superclass = "SimpleForm::Inputs::#{input_type}".constantize - - new_class = Class.new(superclass) do - def input_html_classes - super.push('form-control') - end - end - - Object.const_set(input_type, new_class) -end - # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| # Wrappers are used by the form builder to generate a @@ -30,7 +5,8 @@ def input_html_classes # wrapper, change the order or even add your own to the # stack. The options given below are used to wrap the # whole input. - config.wrappers :default, class: :input, hint_class: :field_with_hint, error_class: :field_with_errors do |b| + config.wrappers :default, class: :input, + hint_class: :field_with_hint, error_class: :field_with_errors do |b| ## Extensions enabled by default # Any of these extensions can be disabled for a # given input by passing: `f.input EXTENSION_NAME => false`. @@ -67,6 +43,12 @@ def input_html_classes b.use :label_input b.use :hint, wrap_with: { tag: :span, class: :hint } b.use :error, wrap_with: { tag: :span, class: :error } + + ## full_messages_for + # If you want to display the full error message for the attribute, you can + # use the component :full_error, like: + # + # b.use :full_error, wrap_with: { tag: :span, class: :error } end # The default wrapper to be used by the FormBuilder. @@ -90,7 +72,7 @@ def input_html_classes config.error_notification_tag = :div # CSS class to add for error notification helper. - config.error_notification_class = 'alert alert-error' + config.error_notification_class = 'error_notification' # ID to add for error notification helper. # config.error_notification_id = nil @@ -116,10 +98,10 @@ def input_html_classes # config.item_wrapper_class = nil # How the label text should be generated altogether with the required text. - # config.label_text = lambda { |label, required| "#{required} #{label}" } + # config.label_text = lambda { |label, required, explicit_label| "#{required} #{label}" } # You can define the class to use on all labels. Default is nil. - # config.label_class = 'control-label' + # config.label_class = nil # You can define the class to use on all forms. Default is simple_form. # config.form_class = :simple_form @@ -165,5 +147,15 @@ def input_html_classes # config.cache_discovery = !Rails.env.development? # Default class for inputs - config.input_class = '' + # config.input_class = nil + + # Define the default class of the input wrapper of the boolean input. + config.boolean_label_class = 'checkbox' + + # Defines if the default input wrapper class should be included in radio + # collection wrappers. + # config.include_default_input_wrapper_class = true + + # Defines which i18n scope will be used in Simple Form. + # config.i18n_scope = 'simple_form' end diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index 182da49..7d3408d 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -1,52 +1,130 @@ # Use this setup block to configure all options available in SimpleForm. SimpleForm.setup do |config| - config.wrappers :bootstrap, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + config.error_notification_class = 'alert alert-danger' + config.button_class = 'btn btn-default' + config.boolean_label_class = nil + + config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'control-label' + + b.use :input, class: 'form-control' + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder - b.use :label + b.optional :maxlength + b.optional :readonly + b.use :label, class: 'control-label' + b.use :input - b.use :error, wrap_with: { tag: 'span', class: 'help-block text-danger' } + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end - config.wrapper_mappings = { boolean: :checkbox } - config.wrappers :checkbox, tag: :div, class: 'checkbox', error_class: 'has-error' do |b| + config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 - b.use :label_input # FIXME: shouldn't be such a hack https://gist.github.com/tokenvolt/6599141#comment-1275095 + b.optional :readonly + + b.wrapper tag: 'div', class: 'checkbox' do |ba| + ba.use :label_input + end - b.use :hint, wrap_with: { tag: :p, class: 'help-block' } - b.use :error, wrap_with: { tag: :span, class: 'help-block text-danger' } + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end - config.wrappers :prepend, tag: 'div', class: 'control-group', error_class: 'error' do |b| + config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'control-label' + b.use :input + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder - b.use :label - b.wrapper tag: 'div', class: 'controls' do |input| - input.wrapper tag: 'div', class: 'input-prepend' do |prepend| - prepend.use :input - end - input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } - input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + b.optional :maxlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-control' + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end end - config.wrappers :append, tag: 'div', class: 'control-group', error_class: 'error' do |b| + config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder - b.use :label - b.wrapper tag: 'div', class: 'controls' do |input| - input.wrapper tag: 'div', class: 'input-append' do |append| - append.use :input + b.optional :maxlength + b.optional :readonly + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + + b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr| + wr.wrapper tag: 'div', class: 'checkbox' do |ba| + ba.use :label_input, class: 'col-sm-9' end - input.use :hint, wrap_with: { tag: 'span', class: 'help-block' } - input.use :error, wrap_with: { tag: 'span', class: 'help-inline' } + + wr.use :error, wrap_with: { tag: 'span', class: 'help-block' } + wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } end end - # Wrappers for forms and inputs using the Twitter Bootstrap toolkit. - # Check the Bootstrap docs (http://twitter.github.com/bootstrap) + config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'sr-only' + + b.use :input, class: 'form-control' + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + # Wrappers for forms and inputs using the Bootstrap toolkit. + # Check the Bootstrap docs (http://getbootstrap.com) # to learn about the different styles for forms and inputs, # buttons and other elements. - config.default_wrapper = :bootstrap + config.default_wrapper = :vertical_form end diff --git a/helena.gemspec b/helena.gemspec index f66270d..a64787f 100644 --- a/helena.gemspec +++ b/helena.gemspec @@ -27,7 +27,7 @@ survey/test development, longitudinal studies and instant feedback.' s.add_dependency 'jquery-rails' s.add_dependency 'sass-rails', '~> 4.0.3' # version needed here http://stackoverflow.com/questions/22392862/undefined-method-environment-for-nilnilclass-when-importing-bootstrap s.add_dependency 'bootstrap-sass' - s.add_dependency 'simple_form' + s.add_dependency 'simple_form', '~> 3.1.0.rc2' s.add_dependency 'breadcrumbs_on_rails' s.add_dependency 'rails-i18n' From d1f6b4fa28ca5267ee28b63659931a51fc782530 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:31:34 +0200 Subject: [PATCH 13/18] remove unnecessary line breaks --- app/views/helena/admin/question_groups/_form.html.haml | 1 - app/views/helena/admin/questions/_form.html.haml | 2 -- app/views/helena/admin/surveys/_form.html.haml | 1 - app/views/helena/admin/versions/_form.html.haml | 1 - 4 files changed, 5 deletions(-) diff --git a/app/views/helena/admin/question_groups/_form.html.haml b/app/views/helena/admin/question_groups/_form.html.haml index 56da380..df885ec 100644 --- a/app/views/helena/admin/question_groups/_form.html.haml +++ b/app/views/helena/admin/question_groups/_form.html.haml @@ -1,4 +1,3 @@ = simple_form_for [:admin, @survey, @question_group] do |f| = f.input :title - %br = f.submit t('shared.save'), class: 'btn btn-success' diff --git a/app/views/helena/admin/questions/_form.html.haml b/app/views/helena/admin/questions/_form.html.haml index 75b4b7c..3bedd8e 100644 --- a/app/views/helena/admin/questions/_form.html.haml +++ b/app/views/helena/admin/questions/_form.html.haml @@ -12,6 +12,4 @@ = render 'labels', form: f - if @question.includes_subquestions? = render 'sub_questions', form: f - - %br = f.submit t('shared.save'), class: 'btn btn-success' diff --git a/app/views/helena/admin/surveys/_form.html.haml b/app/views/helena/admin/surveys/_form.html.haml index bfa6672..ceb755a 100644 --- a/app/views/helena/admin/surveys/_form.html.haml +++ b/app/views/helena/admin/surveys/_form.html.haml @@ -7,5 +7,4 @@ = v.simple_fields_for :survey_detail do |detail| = detail.input :title = detail.input :description, as: :text, input_html: { cols: 60, rows: 10 } - %br = f.submit t('shared.save'), class: 'btn btn-success' diff --git a/app/views/helena/admin/versions/_form.html.haml b/app/views/helena/admin/versions/_form.html.haml index c4759d8..d9bad2b 100644 --- a/app/views/helena/admin/versions/_form.html.haml +++ b/app/views/helena/admin/versions/_form.html.haml @@ -1,5 +1,4 @@ = simple_form_for [:admin, @survey, @version] do |f| = f.input :notes = f.input :session_report, as: :text, input_html: { cols: 60, rows: 10 }, hint: t('.session_report_hint') - = f.submit t('shared.save'), class: 'btn btn-success' From 9d7301aab13ef221a14e71a37fcf2361633c63f2 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:42:51 +0200 Subject: [PATCH 14/18] fixed broken test --- Gemfile | 2 +- Gemfile.lock | 1 + app/views/helena/admin/questions/_sub_questions.html.haml | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index aeadbe0..5a2447a 100644 --- a/Gemfile +++ b/Gemfile @@ -22,7 +22,7 @@ group :development, :test do gem 'binding_of_caller' gem 'selenium-webdriver' gem 'i18n-tasks' - + gem 'simple_form' gem 'jquery-rails' gem 'bootstrap-sass' gem 'breadcrumbs_on_rails' diff --git a/Gemfile.lock b/Gemfile.lock index ccf7743..31f4295 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -281,3 +281,4 @@ DEPENDENCIES rspec-rails ruby-progressbar selenium-webdriver + simple_form diff --git a/app/views/helena/admin/questions/_sub_questions.html.haml b/app/views/helena/admin/questions/_sub_questions.html.haml index da6f742..7ae1abb 100644 --- a/app/views/helena/admin/questions/_sub_questions.html.haml +++ b/app/views/helena/admin/questions/_sub_questions.html.haml @@ -20,7 +20,7 @@ = sub_question.input :code, label: false, hint: t('shared.code_hint') - if @question.is_a?(Helena::Questions::CheckboxGroup) %td.preselected - = sub_question.input :preselected + = sub_question.input :preselected, as: :boolean %td.text = sub_question.input :text, label: false - if @question.is_a?(Helena::Questions::CheckboxGroup) @@ -28,5 +28,5 @@ = sub_question.input :value, label: false, as: :hidden, value: 1 %td.delete - unless sub_question.object.new_record? - = sub_question.input :_destroy, as: :boolean, + = sub_question.input :_destroy, as: :boolean, label: Helena::SubQuestion.human_attribute_name(:_destroy) From 658d0d95c29d7691b7451f0e2300a83af43fa94b Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:43:15 +0200 Subject: [PATCH 15/18] fixed missing mapping for boolean values --- config/initializers/simple_form_bootstrap.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb index 7d3408d..8da5e52 100644 --- a/config/initializers/simple_form_bootstrap.rb +++ b/config/initializers/simple_form_bootstrap.rb @@ -4,6 +4,8 @@ config.button_class = 'btn btn-default' config.boolean_label_class = nil + config.wrapper_mappings = { boolean: :vertical_boolean } + config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| b.use :html5 b.use :placeholder From c77285886d53808b93793a052d709287794d0983 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:48:08 +0200 Subject: [PATCH 16/18] makeup --- config/initializers/simple_form.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb index bf7be1c..52861a2 100644 --- a/config/initializers/simple_form.rb +++ b/config/initializers/simple_form.rb @@ -5,8 +5,9 @@ # wrapper, change the order or even add your own to the # stack. The options given below are used to wrap the # whole input. - config.wrappers :default, class: :input, - hint_class: :field_with_hint, error_class: :field_with_errors do |b| + config.wrappers :default, class: :input, + hint_class: :field_with_hint, + error_class: :field_with_errors do |b| ## Extensions enabled by default # Any of these extensions can be disabled for a # given input by passing: `f.input EXTENSION_NAME => false`. From d3d537ad5169673e69f7205b9c5cc55b6de89b61 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:51:26 +0200 Subject: [PATCH 17/18] don't break deployment with Gemfile.lock --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d247f77..0dd3b08 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,5 +4,3 @@ rvm: services: - mongodb - -before_install: rm Gemfile.lock || true From a5d83c1090c30fa3f99c42ea7e8aacd53e8f39a4 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Mon, 4 Aug 2014 12:58:11 +0200 Subject: [PATCH 18/18] makeup --- app/models/helena/sub_question.rb | 5 ++++- app/views/helena/admin/questions/_sub_questions.html.haml | 4 ++-- app/views/helena/admin/versions/_form.html.haml | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/helena/sub_question.rb b/app/models/helena/sub_question.rb index 5060aff..0d0ded1 100644 --- a/app/models/helena/sub_question.rb +++ b/app/models/helena/sub_question.rb @@ -18,7 +18,10 @@ class SubQuestion def uniqueness_of_code question_code_occurences = question.question_group.version.question_code_occurences - errors.add(:code, :taken, value: code) if question_code_occurences[code] > 1 + + return true if question_code_occurences[code] <= 1 + + errors.add(:code, :taken, value: code) end end end diff --git a/app/views/helena/admin/questions/_sub_questions.html.haml b/app/views/helena/admin/questions/_sub_questions.html.haml index 7ae1abb..95f3b4c 100644 --- a/app/views/helena/admin/questions/_sub_questions.html.haml +++ b/app/views/helena/admin/questions/_sub_questions.html.haml @@ -28,5 +28,5 @@ = sub_question.input :value, label: false, as: :hidden, value: 1 %td.delete - unless sub_question.object.new_record? - = sub_question.input :_destroy, as: :boolean, - label: Helena::SubQuestion.human_attribute_name(:_destroy) + = sub_question.input :_destroy, as: :boolean, + label: Helena::SubQuestion.human_attribute_name(:_destroy) diff --git a/app/views/helena/admin/versions/_form.html.haml b/app/views/helena/admin/versions/_form.html.haml index d9bad2b..c4759d8 100644 --- a/app/views/helena/admin/versions/_form.html.haml +++ b/app/views/helena/admin/versions/_form.html.haml @@ -1,4 +1,5 @@ = simple_form_for [:admin, @survey, @version] do |f| = f.input :notes = f.input :session_report, as: :text, input_html: { cols: 60, rows: 10 }, hint: t('.session_report_hint') + = f.submit t('shared.save'), class: 'btn btn-success'