First step in configuring ubuntu

Some things to install when getting a brand new and fresh installation of Ubuntu. This is a reminder for myself for future times.

No Windows option in GRUB

https://askubuntu.com/questions/822759/no-windows-10-option-in-grub/822772

sudo update-grub

Show git branch name in ubuntu terminal

https://somescooby.wordpress.com/2015/11/17/how-to-show-git-branch-name-in-terminal-ubuntu/

Color Calibration

After experiencing some issues with the way the colours were displayed, I found a decent solution to add this command to the autostart applications:

xcalib -blue 0.9 0.0 80.0 -alter

Install Chrome

https://askubuntu.com/questions/510056/how-to-install-google-chrome

Comfortable-swipe:

https://github.com/Hikari9/comfortable-swipe-ubuntu

Install java

sudo apt-get install default-jdk

Install clojure

https://leiningen.org/#install

Install scala & sbt

sudo apt-get remove scala-library scala
sudo wget www.scala-lang.org/files/archive/scala-2.12.3.deb
sudo dpkg -i scala-2.11.8.deb
echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt

Install Erlang

https://www.erlang-solutions.com/resources/download.html

Install Intellij

sudo tar xf ideaIC-2017.2.5.tar.gz -C /opt/
cd /opt/idea-IC-172.4343.14/bin/
./idea.sh

Github

https://help.github.com/articles/connecting-to-github-with-ssh/

Install Franz, Viber & Skype

  • Download Franz for your distribution from MeetFranz.com
  • change into the same directory as the downloaded file, then sudo tar -xf Franz-linux-x64-0.9.10.tgz -C /opt/franz
  • (optional) wget "https://cdn-images-1.medium.com/max/360/1*v86tTomtFZIdqzMNpvwIZw.png" -O franz-icon.png then sudo cp franz-icon.png /opt/franz
  • (optional) sudo touch /usr/share/applications/franz.desktop then sudo vim /usr/share/applications/franz.desktop

paste the following lines into the file, then save the file:

[Desktop Entry]
Name=Franz
Comment=
Exec=/opt/franz/Franz
Icon=/opt/franz/franz-icon.png
Terminal=false
Type=Application
Categories=Messaging,Internet

view raw
setup.md
hosted with ❤ by GitHub

https://www.viber.com/products/linux/

sudo dpkg -i viber.deb

https://www.skype.com/en/download-skype/skype-for-computer/

sudo dpkg -i skypeforlinux-64.deb

Add more languages for keyboard

https://askubuntu.com/questions/811166/how-to-add-any-language-keyboard-in-ubuntu-16-04

Free linux-like command on Mac OS

Source: https://apple.stackexchange.com/questions/4286/is-there-a-mac-os-x-terminal-version-of-the-free-command-in-linux-systems

Use fish-shell and install the free plugin:

https://github.com/fisherman/fisherman

https://github.com/fisherman/free

After installing, just run in your terminal

fish

then do:

fisher free

and voila, it is installed! Now you can just type

free

in the fish-shell. It will only display the memory in GB. Output looks like this:

free

Setup Jenkis job to run on every git commit

# Source: https://stackoverflow.com/questions/14274293/show-current-state-of-jenkins-build-on-github-repo/26910986#26910986

Install Hudson Post Build Task Plugin in Jenkins:

https://wiki.jenkins.io/display/JENKINS/Post+build+task

Create access token in Github:

https://github.com/settings/tokens

create access token

This will create an access token. Copy the value (because afterwards it will not be available)

Create Github web hook for your repo:

https://github.com/ORG/REPO_NAME/settings/hooks/new

webhook

The Jenkins Job:

  1. Set the name and the url to your project on github

J1

2. Source Code Mgmt – add your repo url, credentials if needed and sent the branch to any.

J_2_ed

3. Build Triggers – choose Poll SCM

J_3

4. Add task that will put the status to pending on github

curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/ORG/REPO_NAME/statuses/$(git rev-parse HEAD) -d "{
  \"state\": \"pending\",
  \"target_url\": \"${BUILD_URL}\",
  \"description\": \"The build is pending!\"
}"

J_pending

5. Post-build actions:

5a. Add a Post Task Plugin that always put success

curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/ORG/REPO_NAME/statuses/$(git rev-parse HEAD) -d "{
  \"state\": \"success\",
  \"target_url\": \"${BUILD_URL}\",
  \"description\": \"The build has succeeded!\"
}"

J_4

5b. Add a Post Task Plugin that will put failure if “marked build as failure”

curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/ORG/REPO_NAME/statuses/$(git rev-parse HEAD) -d "{
  \"state\": \"failure\",
  \"target_url\": \"${BUILD_URL}\",
  \"description\": \"The build has failed!\"
}"

J5

Autocomplete with acts-as-taggable-on and rails-jquery-autocomplete in Rails

In this small guide you will find out how to set up autocomplete for tag using acts-as-taggable-on and rails-jquery-autocomplete gems.

How to set up acts-as-taggable-on: Implement tags in Rails

This guide will focus on autocomplete functionality.
A little bit of background. I have the startup model which can have many fields of activity, like IT, design etc.

In Gem file add the following gems:

gem 'rails-jquery-autocomplete'
gem 'jquery-ui-rails'

I’ve added jquery-ui-rails because in the new version of jquery-rails the ui part was removed and some functionality is missing.

In assets/javascripts/application.js

//= require jquery-ui
//= require jquery_ujs
//= require jquery-ui/autocomplete
//= require autocomplete-rails

In assets/stylesheets/application.css.scss, at the top of the file

/*
*= require jquery-ui
*/

My file was applicaton.scss initially, I had to rename it to css.scss. If you have .css file, you should be fine 🙂

In routes.rb

resources :startups do
  get :autocomplete_field_name, :on => :collection
end

In startup_controller.rb

autocomplete :field, :name, :class_name => 'ActsAsTaggableOn::Tag'

!Important.

You have to add :class_name => ‘ActsAsTaggableOn::Tag’ for this to work.

In views
For new.html.haml

f.input :field_list, :url => autocomplete_field_name_startups_path, :as => :autocomplete, :input_html => { :data => { :delimiter => ', '}}

For edit.html.haml is:

f.input :field_list, :url => autocomplete_field_name_startups_path, :as => :autocomplete, :input_html => { :value => @startup.field_list.join(","), :data => { :delimiter => ', '}}

Upvotes in Rails

I will implement Like/Unlike functionality as it is working in Facebook.
There will be no downvote/dislike nor rating.

Let’s suppose we have the following models:

User(id,…)
Startup(id, …)
Upvote(id, user_id, startup_id)

In upvote.rb:

class Upvote < ActiveRecord::Base
  belongs_to :user
  belongs_to :startup
end

In user.rb

class User < ActiveRecord::Base
  has_many :upvotes

  def already_upvoted?(startup)
    User.joins(:upvotes).where(:upvotes => {:startup_id => startup.id}).size > 0
  end
end

In startup.rb

class Startup < ActiveRecord::Base
  has_many :upvotes
end

In routes.rb

resources :startups do
  member do
    get "upvote"
    get "undo_upvote"
  end
end

Probably you can find a more proper name for undo_upvote functionality 😀

In startup_controller.rb:

def upvote
  @startup = Startup.find(params[:id])
  upvote = @startup.upvotes.find_or_create_by(user: current_user)
  upvote.save
  redirect_to startups_path
end

def undo_upvote
  @startup = Startup.find(params[:id])
  upvote = @startup.upvotes.find_by(user: current_user)
  upvote.destroy
  redirect_to startups_path
end

In Startup view

-if current_user.already_upvoted?(startup)
 = link_to "Unlike",undo_upvote_startup_path(startup)
-else
 = link_to "Like",upvote_startup_path(startup)

Now a user can like/unlike a startup.

Rails: Authenticate with Google and Devise

This is a small guide that will help you setup Google authentication in your rails app (with Devise).

Isert these lines in your Gem file

gem 'devise'
gem 'omniauth-ouath2'
gem 'omniauth-google-oauth2'

!Note: When using gem omniauth I got a nasty error when user tried to authenticate:

Authentication failure! invalid_credentials:
OAuth2::Error, redirect_uri_mismatch:
{
  "error" : "redirect_uri_mismatch"
}
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"state"=>"bb02b2af24f91c9878bf168f5721dfe78dd686b73286fbe0", "code"=>"4/7ylzDJRjHGCeTPmNMeD7CAEj3l0CKBubPes8UhABzFs"}

After hours of trying to solve the problem, the solution I found was to user gem “omniauth-oauth2” instead. Everything worked as a charm.

Run

bundle install

If you haven’t integrated Login with other providers, you need to run these 2 lines:

rails g migration AddProviderToUsers provider:string uid:string
rake db:migrate

You need now an client id and client secret. Go to

https://code.google.com/apis/console/

And create your app

create_project

Some set ups

You must ensure that “Contacts API” and “Google+ API” are enabled.
Click on your project, then in the right side menu choose “API Manager”

api_manager

In the search bar type the name of the API you want to enable, select it and click “Enable”. You’re done.

Get your client id and client secret
In API manager, go to Credentials and hit “Add Credentials” and select “Oauth 2.0 client ID”

oauth

Select web application, write your app’s name, and also include two links:

Authorized JavaScript Origins: http://localhost:3000
Authorized redirects URI: http://localhost:3000/users/auth/google_oauth2/callback

The second link is crucial to set up in order for everything to work.

Capture888

Once you click create, you will get the client_id and client_secret of your application.
Go to config/application.yml and add the following lines:

GOOGLE_CLIENT_ID: YOUR_CLIENT_ID
GOOGLE_CLIENT_SECRET: YOUR_CLIENT_SECRET

Go to config/secrets.yml and add the following lines for every environment:

google_client_id: <%= ENV["GOOGLE_CLIENT_ID"] %> 
google_client_secret: <%= ENV["GOOGLE_CLIENT_SECRET"] %>

Go to config/initializers/devise.rb and include the following lines in your code

OmniAuth.config.full_host = Rails.env.production? ? 'https://domain.com' : 'http://localhost:3000' 
 config.omniauth :google_oauth2, 
   Rails.application.secrets.google_client_id, 
   Rails.application.secrets.google_client_secret, 
   :skip_jwt => true 
   {}

In config/routes.rb add the following:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

Go to controllers folder and create a new folder called users. There create a file named omniauth_callbacks_controller.rb

Copy paste the following content:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController  
   def google_oauth2 
   # You need to implement the method below in your model (e.g. app/models/user.rb) 
   @user = User.connect_to_google(request.env["omniauth.auth"]) 
 
   if @user.persisted? 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" 
     sign_in_and_redirect @user, :event => :authentication 
   else 
     session["devise.google_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
   end 
 end 
end

In your user.rb file add the following changes:
for devise add

 :omniauthable, 
 :omniauth_providers => [:google_oauth2]

It should look like this

 devise :database_authenticatable, :registerable, 
             :recoverable, :rememberable, :trackable, :validatable, 
             :omniauthable,:omniauth_providers => [:google_oauth2]

Add the needed functions. You can adjust them and extract the data you need.

def self.connect_to_google(auth) 
 data = auth.info 
 where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
   user.email = data["email"] 
   user.password = Devise.friendly_token[0,20] 
   user.first_name = data["first_name"] 
   user.last_name = data["last_name"] 
   user.avatar = process_uri(data["image"]) 
 end 
end

#This one is needed to process the link you will get for your image. Y

 
def self.process_uri(uri) 
 avatar_url = URI.parse(uri) 
 avatar_url.scheme = 'https' 
 avatar_url.to_s 
end

In the devise views there is one /shared/_links.html.erb
There you should have

<%- if devise_mapping.omniauthable? %> 
 <%- resource_class.omniauth_providers.each do |provider| %> 
 <%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br /> 
 <% 
end -%>

This will show the link on the sign up/sign in pages that will allow you to sign in with Google and other providers you are going to set up.

Have fun!

How to show git branch name in terminal Ubuntu

For the branch name to appear in your terminal, go to home directory and do

vim .bashrc

and add the following at the end

function parse_git_branch () {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
 
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"
 
PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "