Skip to content

Commit

Permalink
Code was re organised #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Pbasnal committed Jul 11, 2014
1 parent f9f49d2 commit 173ee4f
Show file tree
Hide file tree
Showing 27 changed files with 434 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ build/
/log/
/tmp
*.log
test.html.haml
14 changes: 9 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,20 @@ gem 'bootstrap-sass'

# The following gems for testing purpose in development and testing environment
group :development, :test do
# Rspec is used to write the test cases
# Rspec is used to write the test cases
gem 'rspec-rails'
# Use factory girl to pass random data for test cases
# Use factory girl to pass random data for test cases
gem 'factory_girl_rails'
# Use faker to generate fake strings and data
# Use faker to generate fake strings and data
gem 'faker'
# Use to clean database after executing a test case
# Use to clean database after executing a test case
gem 'database_cleaner'
# Use to track how much code has been tested
# Use to track how much code has been tested
gem 'simplecov'
# Webmock to stub http requests
gem 'webmock'
# VCR to record the responses from web and replay them when needed
gem 'vcr'
end

gem 'simple_form'
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ GEM
execjs
coffee-script-source (1.7.0)
connection_pool (2.0.0)
crack (0.4.2)
safe_yaml (~> 1.0.0)
database_cleaner (1.3.0)
devise (3.2.4)
bcrypt (~> 3.0)
Expand Down Expand Up @@ -210,6 +212,7 @@ GEM
rspec-mocks (~> 3.0.0)
rspec-support (~> 3.0.0)
rspec-support (3.0.2)
safe_yaml (1.0.3)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -270,8 +273,12 @@ GEM
execjs (>= 0.3.0)
json (>= 1.8.0)
uuidtools (2.1.4)
vcr (2.9.2)
warden (1.2.3)
rack (>= 1.0)
webmock (1.18.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)

PLATFORMS
ruby
Expand Down Expand Up @@ -307,3 +314,5 @@ DEPENDENCIES
therubyracer
turbolinks
uglifier (>= 1.3.0)
vcr
webmock
6 changes: 6 additions & 0 deletions app/assets/javascripts/spreadsheets.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
xyz = ->
alert "welcome"
return
1 change: 1 addition & 0 deletions app/assets/stylesheets/bootstrap_config.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import "bootstrap";

body
{
padding-top: 80px;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/spreadsheets.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the spreadsheets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
1 change: 0 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

end
6 changes: 3 additions & 3 deletions app/controllers/callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def setSheet
end

def insertEmailInDatabase(tkn)
@tkn = tkn
session = GoogleDrive.login_with_oauth(@tkn["access_token"])
@ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0]
@tkn = tkn
session = GoogleDrive.login_with_oauth(@tkn["access_token"])
@ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0]

rowcount = @ws.rows.count

Expand Down
57 changes: 57 additions & 0 deletions app/controllers/spreadsheets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class SpreadsheetsController < ApplicationController
include GoogleSpreadsheets

def index
@spreadsheets = Spreadsheet.all.to_a
end

def new
@req = params
=begin
if params[:access_token]
spreadsheet = Spreadsheet.where(access_token: params['access_token'])[0]
else
spreadsheet = Spreadsheet.new
spreadsheet.add_tokens(request.env['omniauth.auth'].fetch('credentials'))
end
if spreadsheet.save
# Spreadsheets from google
@spreadsheets = get_spreadsheets(spreadsheet)
@token = spreadsheet.access_token
@msg = 'work'
else
@msg = 'no work'
# Handle if data does not get saved
end
=end
end

def edit
token = spreadsheet_params['token']
spreadsheet = Spreadsheet.where(access_token: token)[0]
spreadsheet.add_spreadsheet_credentials(spreadsheet_params)
#binding.pry
spreadsheet.save

@spreadsheets = Spreadsheet.all.to_a

render action: 'index'
end

def update
spreadsheet = Spreadsheet.find(params['id'])
@worksheet = get_worksheets(spreadsheet)
User.add_users_from_worksheet(@worksheet)
redirect_to users_path
end

def destroy
Spreadsheet.find(params['id']).delete
redirect_to spreadsheets_path
end

def spreadsheet_params
params.permit(:title, :id, :token)
end
end
2 changes: 2 additions & 0 deletions app/helpers/spreadsheets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SpreadsheetsHelper
end
28 changes: 28 additions & 0 deletions app/models/spreadsheet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Spreadsheet
include Mongoid::Document

field :spreadsheet_id, type: String
field :spreadsheet_title, type: String
field :refresh_token, type: String
field :expires_at, type: Time
field :access_token, type: String

validates :access_token, uniqueness: true

def add_tokens(tokens = {})
self['access_token'] = tokens.fetch('token')
self['refresh_token'] = tokens.fetch('refresh_token')
self['expires_at'] = tokens.fetch('expires_at')
end

def add_spreadsheet_credentials(credentials = {})
#binding.pry
self['spreadsheet_id'] = credentials['id']
self['spreadsheet_title'] = credentials['title']
end

def access_token
self['access_token']
end

end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ class User

belongs_to :newsletter, counter_cache: :users_count

def self.add_users_from_worksheet(worksheet, column = 1)
worksheet.rows.count.times do |i|
User.create(email_id: worksheet[i + 1, column],
is_subscribed: true,
joined_on: Date.today,
source: 'Google Spreadsheet')
end
end
end
48 changes: 24 additions & 24 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@
.navbar-brand
%h4 LightAir

%ul.nav.navbar-nav
%li.active
%a{href: "#{home_index_path}"}
%h4 Home
%li
%a{href: "#"}
%h4 Newsletters
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"}
%h4
Add Users
%span.caret
%ul.dropdown-menu{role: "menu", "aria-labelledby" => "dropdownMenu1" }
%li
%a{role: "menuitem", tabindex: "-1", href: "#{new_user_path}"}Manually
%li
%a{role: "menuitem", tabindex: "-2", href: "#{callback_index_path}"}Google Spreadsheets
%li
%a{role: "menuitem", tabindex: "-1", href: "#{linkedin_path}"}Linkedin Connections
%li
%a{href: "#{users_path}"}
%h4 All Users
%div.nav-collapse
%ul.nav.navbar-nav
%li.active
%a{href: "#{home_index_path}"}
%h4 Home
%li
%a{href: "#"}
%h4 Newsletters
%li
%a{href: "#{users_path}"}
%h4 Show Users
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown"}
%h4
Add Users
%span.caret
%ul.dropdown-menu{role: "menu", "aria-labelledby" => "dropdownMenu1" }
%li
%a{role: "menuitem", tabindex: "-1", href: "#{new_user_path}"}Add Manually
%li
%a{role: "menuitem", tabindex: "-2", href: "#{spreadsheets_path}"}Google Spreadsheets


%ul.nav.navbar-nav.navbar-right
%li.active
%a{href: "#"}
Subscriber count :
=User.count
%h4
Subscriber count :
=User.count
%div.container
%div.row
%div.col-sm-9= yield
Expand Down
1 change: 1 addition & 0 deletions app/views/spreadsheets/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=debug @sp
31 changes: 31 additions & 0 deletions app/views/spreadsheets/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
%h4
=link_to 'Add New Spreadsheet', 'auth/google/'

%table.table.table-striped
%tr.info
%td
%strong #
%td
%strong
Spreadsheets (
=Spreadsheet.count
)
%td
%strong Operations
- i = 0
- @spreadsheets.each do |ss|
%tr
%td
= i = i + 1
%td
- if ss['spreadsheet_title']
=ss['spreadsheet_title']
-else
=link_to 'Spreadsheet not initialised', new_spreadsheet_path(access_token: ss['access_token'])
%td
=link_to 'Delete', spreadsheet_path(ss), method: :delete
-if ss['spreadsheet_title']
|
=link_to 'Update', spreadsheet_path(ss), method: :patch


7 changes: 7 additions & 0 deletions app/views/spreadsheets/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=@req
%br
-#count = @spreadsheets.count
=#count
-#count.times do |i|
=#link_to @spreadsheets[i]['title'], edit_spreadsheet_path(@spreadsheets[i]['id'], title: @spreadsheets[i]['title'], token: @token) if @spreadsheets[i]['mimeType'].include?('spreadsheet')
%br
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
module Lightair
class Application < Rails::Application
config.assets.precompile += %w(*.png, *.jpg, *.jpeg, *.gif)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
7 changes: 3 additions & 4 deletions config/initializers/google_oauth.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_ID'], ENV['GOOGLE_KEY'],
{
name: "google",
name: 'google',
scope: 'userinfo.profile,userinfo.email,drive,https://spreadsheets.google.com/feeds',
prompt: 'consent',
access_type: "offline",
access_type: 'offline',
#redirect_uri: 'http://localhost:8080/auth/google/spreadsheets'
}

#provider :linkedin, '75cmlzl0cpmwa2', 'I5y0aRBkfpgMmTqp', redirect_uri: 'http://localhost:8080/auth/linkedin/callback', scope:'r_emailaddress r_network r_contactinfo rw_company_admin rw_nus rw_groups w_messages r_basicprofile r_fullprofile'
end
11 changes: 5 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
Rails.application.routes.draw do

=begin
get '/home/sheets', to: 'home#add_from_google', as: 'add_from_google'
get '/auth/:provider/callback', to: 'callbacks#omniauth'
get '/linkedin', to: 'callbacks#linkedin', as: 'linkedin'
get '/callbacks/sheet/:id', to: 'callbacks#setSheet', as: 'set_sheet'
get '/callbacks/index', to: 'callbacks#index', as: 'callback_index'
get '/callbacks/update/:id', to: 'callbacks#update', as: 'callback_update'

resources :users
resources :newsletters
resources :home
=end
get '/auth/:provider/callback', to: 'spreadsheets#new'
resources :users, :newsletters, :home, :spreadsheets

root :to => "home#index"
root :to => 'home#index'

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
Loading

0 comments on commit 173ee4f

Please sign in to comment.