From 60140999efd447c0ba7442c7d04e1e5b754de611 Mon Sep 17 00:00:00 2001 From: Jorge Gomez Sancha Date: Tue, 9 Dec 2014 11:21:43 +0100 Subject: [PATCH 1/6] Working version of Add new project with Select2 --- Gemfile | 2 +- Gemfile.lock | 6 ++-- app/assets/javascripts/application.js | 2 +- app/assets/javascripts/general.js.coffee | 26 ++++++++++++-- app/assets/stylesheets/application.css.scss | 20 +++++++++-- app/controllers/donations_controller.rb | 11 +++++- app/controllers/projects_controller.rb | 10 +++--- app/models/donation.rb | 12 +++++-- app/models/project.rb | 5 +++ app/views/donations/_form.html.erb | 19 ++++++----- config/routes.rb | 1 - ...reating_project_while_tracking_don_spec.rb | 34 +++++++++++++++++++ 12 files changed, 120 insertions(+), 28 deletions(-) create mode 100644 spec/features/creating_project_while_tracking_don_spec.rb diff --git a/Gemfile b/Gemfile index 8486566..a3cd719 100644 --- a/Gemfile +++ b/Gemfile @@ -19,9 +19,9 @@ gem 'turbolinks' gem 'friendly_id', '~> 5.0.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+ gem 'acts-as-taggable-on', '~> 3.4' -gem 'rails4-autocomplete' gem 'i18n_country_select' gem 'i18n-country-translations' +gem 'select2-rails' gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index ab3fdd2..fc7b460 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -115,8 +115,6 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 4.1.6) sprockets-rails (~> 2.0) - rails4-autocomplete (1.1.1) - rails (>= 3.0) railties (4.1.6) actionpack (= 4.1.6) activesupport (= 4.1.6) @@ -145,6 +143,8 @@ GEM sass (~> 3.2.2) sprockets (~> 2.8, < 3.0) sprockets-rails (~> 2.0) + select2-rails (3.5.9.1) + thor (~> 0.14) slop (3.6.0) spring (1.2.0) sprockets (2.12.3) @@ -193,9 +193,9 @@ DEPENDENCIES pg quiet_assets rails (= 4.1.6) - rails4-autocomplete rspec-rails sass-rails (~> 4.0.3) + select2-rails spring sqlite3 turbolinks diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 25e6422..2aad3b5 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -15,7 +15,7 @@ //= require jquery-ui/tooltip //= require jquery_ujs //= require tipsy -//= require autocomplete-rails //= require slidebars +//= require select2 //= require turbolinks //= require_tree . diff --git a/app/assets/javascripts/general.js.coffee b/app/assets/javascripts/general.js.coffee index fdb30c1..2f069c6 100644 --- a/app/assets/javascripts/general.js.coffee +++ b/app/assets/javascripts/general.js.coffee @@ -1,11 +1,31 @@ -$( "#donation_project_name" ).autocomplete - source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] - $(document).on "page:change", -> $(".popup").click (event) -> event.preventDefault() window.open $(this).attr("href"), "popupWindow", "width=600,height=600,scrollbars=yes" return + $('#donation_project_name').select2 + placeholder: 'Project' + ajax: + url: '/projects' + dataType: 'json' + quietMillis: 250, + results: (data, page) -> + return { results: $.map data, (obj) -> {id: obj.name, text: obj.name} } + cache: true + data: (term, page) -> + return { q: term } + # query: (query) -> + # data = {results: [{id: 'Wadus', text: 'Wadus'}, {id: 'Wadus 2', text: 'Wadus 2'}]} + # query.callback data + allowClear: 'true' + createSearchChoice: (term, data) -> + return {id:term, text: term + ' (Add project)'} + createSearchChoicePosition: 'bottom' + $('#donation_project_name').on "select2-selecting", (e) -> + if e.choice.text.indexOf('(Add project)') > 0 + $('#project_attributes').slideDown() + else + $('#project_attributes').slideUp() $('.tipsit').tipsy({fade: true; gravity: 's'}) my_slidebars = new $.slidebars(); return diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index a28485b..93fec57 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -15,6 +15,7 @@ *= require jquery-ui/autocomplete *= require tipsy *= require slidebars + *= require select2 *= */ @@ -135,9 +136,10 @@ h2 { font-size: 24px; } .main_form { clear: both; margin: 0 0 20px 0;} .track_don_project, .track_don_quantity, .track_don_date { display: inline-block; } -.track_don_project { width: 540px; margin-bottom: 20px; overflow: hidden;} +.track_don_project { width: 540px; overflow: hidden;} .track_don_quantity { width: 90px; margin-right: 10px; } -.track_don_quantity_B { width: 375px; margin-bottom: 20px; float: right; } +.track_don_quantity_B { width: 375px; float: right; } +.main_form .track_don_quantity_B input { width: 90px; margin: 0;} .track_don_quantity_B_option { background: #D5D7A3; margin-bottom: 4px; } .track_don_quantity_B_option .hint { display: inline-block; } .main_form .track_don_quantity_B_option_hint { display: inline-block; width: auto; font-size: 12px; vertical-align: top; padding: 6px 0 0 0;} @@ -147,9 +149,13 @@ h2 { font-size: 24px; } .track_don_quantity input { width: 74px; } .track_don_date input { width: 100%; font-size: 12px;} .track_don_options { margin: 0 0 20px 0; clear: both;} -.hint { font-size: 12px} +.hint { font-size: 12px; margin-bottom: 20px; } #i_want_to_explain_content { display: none; } #i_want_to_explain_content textarea { width: 97%; height: 100px; font-size: 13px; padding-top: 10px; margin: 10px 0;} +#project_attributes { display:none; + input, textarea { width: 96.5%; font-size: 13px; border: 1px dashed $main-color; font-weight:bold;} + textarea { width:99%; padding-top: 10px; height: 100px; } +} .track_don_quantity_B { @@ -297,6 +303,14 @@ hr { height: 1px; border:0; background-color: #D9DAAA; margin: 60px 0px 15px; wi .ui-autocomplete { border: 1px solid red; } +.select2-container { width: 540px; font-size: 22px; font-weight: bold; font-family: Raleway; height: 40px; margin-bottom: 8px !important; + a.select2-choice { line-height: 32px; display: block; height: 35px; border: 1px dashed #7c9200; padding: 4px 6px 0px; + + } +} +.select2-results .select2-highlighted { background: #7c9200 !important; } +.select2-drop-active { border: 1px dashed #7c9200 !important; border-top: none !important; } +.select2-search input[type=text] { font-family: Raleway; font-size: 18px; } #first_donation_pending { padding-top: 1em; padding-bottom: 1em; margin-bottom: 2em; } #footer { text-align: center; font-size: 12px } diff --git a/app/controllers/donations_controller.rb b/app/controllers/donations_controller.rb index 1204942..bbd7a56 100644 --- a/app/controllers/donations_controller.rb +++ b/app/controllers/donations_controller.rb @@ -33,7 +33,16 @@ def create private def donation_params - params.require(:donation).permit(:quantity, :currency, :date, :project_id, :comment, :quantity_privacy) + params.require(:donation).permit( + :quantity, + :currency, + :date, + :comment, + :quantity_privacy, + :project_id, + :project_name, + :project_description, + :project_url) end def save_donation_to_cookie(donation_params) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 2675c94..d711147 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -1,10 +1,12 @@ class ProjectsController < ApplicationController before_filter :set_new_donation, only: :show - autocomplete :project, :name - def index - @projects = Project.all + @projects = Project.search(params[:q]) + respond_to do |format| + format.html + format.json { render json: @projects} + end end def show @@ -16,7 +18,6 @@ def new end def create - # render plain: params[:project].inspect @project = Project.new(project_params) if @project.save flash[:success] = "Project created. Share the word and start donation-saving!" @@ -27,7 +28,6 @@ def create end private - def project_params params.require(:project).permit(:name, :description, :url, :twitter) end diff --git a/app/models/donation.rb b/app/models/donation.rb index ab883fb..04a322e 100644 --- a/app/models/donation.rb +++ b/app/models/donation.rb @@ -2,13 +2,13 @@ class Donation < ActiveRecord::Base belongs_to :project belongs_to :user + attr_accessor :project_name, :project_description, :project_url + before_validation :set_project monetize :quantity_cents, as: :quantity, with_model_currency: :currency default_scope -> { order('created_at DESC') } scope :last_month, -> { where('date >= ?', 1.month.ago) } - - attr_accessor :project_name # acts_as_taggable_on :tags #validates :quantity_cents, presence: true @@ -16,5 +16,13 @@ class Donation < ActiveRecord::Base validates :project_id, presence: true validates :user_id, presence: true validates :date, presence: true + + def set_project + unless self.project.present? + self.project = Project.create_with( + :description => @project_description, + :url => @project_url).find_or_create_by(:name => @project_name) + end + end end diff --git a/app/models/project.rb b/app/models/project.rb index 64a9214..7230db1 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -9,5 +9,10 @@ class Project < ActiveRecord::Base validates :name, presence: true, length: { minimum: 5 } validates :description, presence: true, length: { minimum: 25 } validates :url, presence: true, length: { minimum: 5 } + + def self.search(query) + return Project.where(["name like ?", "%#{query}%"]) if query.present? + Project.all + end end diff --git a/app/views/donations/_form.html.erb b/app/views/donations/_form.html.erb index 3dbabb9..7034575 100644 --- a/app/views/donations/_form.html.erb +++ b/app/views/donations/_form.html.erb @@ -7,15 +7,20 @@
<%= @project.name %>
+
+ <%= link_to t('need_to_trackdon_for_another_project'), "#{root_path}#track_donation" %> +
<% else %> - <%= f.autocomplete_field :project_name, autocomplete_project_name_projects_path, :id_element => '#donation_project_id', placeholder: 'Project', :"data-autocomplete-label" => "Sorry, nothing found." %> - <% end %> - - <% if params[:controller] == 'projects' %> -
<%= link_to t('need_to_trackdon_for_another_project'), track_donation_link %>
- <% else %> + <%= f.hidden_field :project_name %>
<%= f.label(:project_name, 'ie. Wikipedia, Redd Cross, UNR') %>
+
+ <%= f.text_area :project_description %> +
<%= f.label(:project_description, 'Project Description') %>
+ <%= f.text_field :project_url %> +
<%= f.label(:project_url, 'Project URL') %>
+
<% end %> +