Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding project on the fly when tracking donations #49

Merged
merged 7 commits into from
Dec 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 'redcarpet'

gem 'bcrypt', '~> 3.1.7'
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -146,6 +144,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)
Expand Down Expand Up @@ -194,10 +194,10 @@ DEPENDENCIES
pg
quiet_assets
rails (= 4.1.6)
rails4-autocomplete
redcarpet
rspec-rails
sass-rails (~> 4.0.3)
select2-rails
spring
sqlite3
turbolinks
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//= require jquery-ui/tooltip
//= require jquery_ujs
//= require tipsy
//= require autocomplete-rails
//= require slidebars
//= require select2
//= require turbolinks
//= require_tree .
26 changes: 23 additions & 3 deletions app/assets/javascripts/general.js.coffee
Original file line number Diff line number Diff line change
@@ -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_attributes_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_attributes_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
Expand Down
22 changes: 18 additions & 4 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*= require jquery-ui/autocomplete
*= require tipsy
*= require slidebars
*= require select2
*=
*/

Expand Down Expand Up @@ -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;}
Expand All @@ -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 {

Expand Down Expand Up @@ -258,7 +264,7 @@ hr { height: 1px; border:0; background-color: #D9DAAA; margin: 60px 0px 15px; wi
padding: 0;

li {
width: 30%;
width: 29%;
margin-right: 26px;
background-color: $main-color;
text-align: center;
Expand Down Expand Up @@ -320,6 +326,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 }
Expand Down
21 changes: 19 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
include ProjectsHelper
include DonationsHelper

before_action :set_locale

Expand All @@ -27,7 +26,25 @@ def extract_locale_from_subdomain

def set_new_donation
new_donation_currency = logged_in? ? current_user.currency : 'EUR'
@donation = Donation.new currency: new_donation_currency
@donation = Donation.new currency: new_donation_currency,
project: (@project.nil? ? Project.new : @project)
end

def donation_save(donation_params)
@donation = current_user.donations.build(donation_params)
if donation_params[:project_id] and project = Project.find_by(id: donation_params[:project_id])
@donation.project = project
end
if @donation.save
redirect_to donation_path(@donation, :share_links => true)
else
render 'new'
end
end

def cookie_donation
JSON.parse(cookies[:donation]).with_indifferent_access
end
helper_method :cookie_donation

end
17 changes: 4 additions & 13 deletions app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def show
def create
unless logged_in?
save_donation_to_cookie(donation_params)
cookie_donation
redirect_to root_url
redirect_to signup_path
else
donation_save(donation_params)
if cookies[:donation]
Expand All @@ -33,20 +32,12 @@ 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_attributes: [:name, :description, :url, :id])
end

def save_donation_to_cookie(donation_params)
cookies[:donation] = {
:quantity => donation_params[:quantity],
:currency => donation_params[:currency],
:date => donation_params[:date],
#:tag_list => donation_params[:tag_list],
:project_id => donation_params[:project_id],
:comment => donation_params[:comment],
:quantity_privacy => donation_params[:quantity_privacy],
:user_id => donation_params[:user_id]
}.to_json
cookies[:donation] = donation_params.to_json
end

end
10 changes: 5 additions & 5 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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!"
Expand All @@ -27,7 +28,6 @@ def create
end

private

def project_params
params.require(:project).permit(:name, :description, :url, :twitter)
end
Expand Down
19 changes: 9 additions & 10 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def index

def show
@user = User.find(params[:id])
@donations = @user.donations
@projects = @user.projects
end

def new
Expand All @@ -36,14 +34,7 @@ def create
@user = User.new(user_params)
if @user.save
log_in @user
if cookies[:donation]
donation_save(cookie_donation)
flash[:success] = "Hey, donation tracked, and you have your profile ready to keep tracking donations! Now this is a great day."
cookies.delete(:donation)
else
flash[:success] = "Welcome to TrackDons. Hope you track a lot of dons!"
redirect_to @user
end
save_pending_donations || redirect_to(@user, success: "Welcome to TrackDons. Hope you track a lot of dons!")
else
render 'new'
end
Expand Down Expand Up @@ -77,4 +68,12 @@ def admin_user
redirect_to(root_url) unless current_user.admin?
end

def save_pending_donations
if cookies[:donation]
donation_save(cookie_donation)
flash[:success] = "Hey, donation tracked, and you have your profile ready to keep tracking donations! Now this is a great day."
cookies.delete(:donation)
end
end

end
7 changes: 6 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ def markdown(text)

markdown.render(text).html_safe
end

private

def language_name_for(locale)
I18n.backend.send(:translations)[locale][:language_name]
end

def current_user_profile?
@user == current_user
end
end
17 changes: 2 additions & 15 deletions app/helpers/donations_helper.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
module DonationsHelper

def donation_save(donation_params)
@donation = current_user.donations.build(donation_params)
if @donation.save
redirect_to donation_path(@donation, :share_links => true)
else
render 'new'
end
def temp_donations_exist?
cookies[:donation].present?
end

def cookie_donation
cookie_donation = JSON.parse(cookies[:donation]).with_indifferent_access
end

def temp_donations_exist
cookies[:donation] # check if the cookie donation is set
end

def in_projects_page?
controller_name == 'projects' && action_name == 'show'
end
Expand Down
15 changes: 11 additions & 4 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
class Donation < ActiveRecord::Base

belongs_to :project
accepts_nested_attributes_for :project

belongs_to :user
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
#validates :currency, presence: true
validates :project_id, presence: true
validates :user_id, presence: true
validates :date, presence: true

def set_project
if self.project && self.project.new_record?
self.project = Project.create_with({
description: self.project.description,
url: self.project.url}).find_or_create_by(name: self.project.name)
end
end

end
11 changes: 8 additions & 3 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
class Project < ActiveRecord::Base

has_many :donations
has_many :users, through: :donations

extend FriendlyId
extend FriendlyId
friendly_id :name, :use => [:slugged]

validates :name, presence: true, length: { minimum: 5 }
validates :name, presence: true, length: { minimum: 5 }, uniqueness: true
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
Loading