Small Detour, YT videos, getting the basics right.

LiftApp has been in production mode for quite some time now. We are ironing things out and definitely will continue adding new features (Adding your own routines, statistics etc, authorization). You can check it out here.
So what’s the detour about? I discovered couple of incredible guides which i decided to take, and it ended up being great decision.

First one is Miroslaw Zelent‘s website and his huge YT channel. I went through all of the HTML, CSS3, C++, Javascript videos and additional material on his blog and I have to say, that was probably the best learning experience I ever had. Videos are well structured in to one big coherent tutorial. Each video has a follow up based of the previous video, Mr. Zelent himself shows a lot of passions, concern and attention to detail with a bit of humour mixed in and loads of references to games, movies and general geek stuff. It was a pure joy to follow. Most importantly I managed to soak a lot of knowledge, learned the new semantic HTML5 tags, understood CSS3 well enough to actually make my websites not look like something that dog barfed out 🙂 Solidified my knowledge of low level innerworkings of C++ (Zelent’s explanations of recursion and pointers are probably the best ones I have ever seen, very basic and straight to the point followed by perfect examples, I do recommend it for anyone struggling to understand those concepts)

Second is Wes Bos’ javascript30 free guide. I didn’t feel well versed in intricates of ES6 but the videos Wes provided were down to the point with great tips and a lot of good practices which I picked up. (Spread operators, Arrow functions, const instead of var)
Overall it made me feel like a better developer.

Next post? Tomorrow, with some cool news related to Grand Finale of DSP in Warsaw. Stay tuned 😉

Advertisements

SQL

Learning SQL was more or less a neccessity for me, especially that knowing SQL can help understand ORM (Object Relational Mapping) of other languages. Not to mention that more complex queries generated by AR can be speedup by raw SQL. Careful not to get injected though!

What I picked up from learning SQL so far.  SQL (Structured Query Language – I guess I should have said that at the start) “consists” of couple of parts:

1) SELECTing

SELECT allows you to … select columns from table. The simples query would look like this:

 SELECT * FROM users

This query would return you all records and columns from the user table. Pretty simple.
* here acts a wildcard. In plain English you would read it as “select everything from users table”.

Having that data to play around. We can manipulate it a bit.

 SELECT * FROM users ORDER BY name

That will simply select all columns, from the users table and order them by name. If name is of type string then it will sort it alphabetically. It’s a recurring thing in SQL. You select data, aggregate and sort (or something else, depending on your query’s clauses or conditions)

That’s the GENERAL idea of selecting. I purposely omitted more “complex” operations such as JOINing, relations, group by’s, having, limits and offsets because I’m fairly inexperienced with them to openly blog about it. I will certainly return to it when I become more confident with it. Studying queries generated by ActiveRecord helps a lot here.

The general syntax I’m trying to follow is as following:


SELECT column1, column2
FROM tables AS whatever_name
WHERE some_constraint
ORDER BY column_name
LIMIT some_number

It’s worth mentioning that logical operators as AND and OR are also commonly used in SQL.

2) INSERT, DELETE, UPDATE

Those are very straightforward. You select table, then columns and (optionally) add a condition.

 INSERT INTO table VALUES (some_value, second_value, and so on...) 

Above insert works given that you are inserting columns in the correct order. You can be more explicit and write it this way:

INSERT INTO table (column_name, column_two, column_three)
VALUES (first_value, second_value, third_value)

Update and Delete are also very simple:

UPDATE table SET column_name = 'Some updated value'
WHERE condition (EG: user_id = 1)

It’s important to notice that you have to add the WHERE with condition to update specific record or set of records. If you don’t specify it then all records will be updated!

Delete:

DELETE FROM table_name
WHERE condition (same idea as with update)

Same deal as with UPDATE. You have to specify what exactly you want to delete. Otherwise all  your table records will vanish.

That’s all for now. This post will get a major update when I discover more stuff about SQL.

render method and some cool features

This post is very short as I want to share something very cool i found out with the rails render method.

Take a look at both examples (code simplified)

# views/routines/index.html.erb
@routine.exercises.each do |exercise|
  exercise.name
  exercise.sets
  exercise.reps
  # ... More data
  # and CRUD actions at the bottom
end

Pretty standard right?  I found out there’s actually much nicer way of accomplishing that. It also DRY’s up the code considerably.

All you have to do is to create an exercise partial and name it like this: _exercise.html.erb  and put it inside exercise views  together with all the code in the block above, just like we would usually do. With that we can now do the following:


# views/routines/index.html.erb

# This is rough coude without all the needed erb tags, don't forget them!

@routine.exercises.each do |exercise|

render exercise

end

That’s all! At first I tried to be smarter than Rails (unsuccessfully) and tried using string interpolation ( I wasn’t aware and didn’t believe that it’d work otherway) as below:


# views/routines/index.html.erb

# ... code code code

render "exercise/#{exercise}"

# ... end of code

But rails takes care of everything and simply requires the object alone to be passed and it will figure the path of the partial by itself!

https://liftapp-prod.herokuapp.com/

That’s the new url for project LiftApp. I have been fiddling a lot with the heroku itself and ended up taking two courses on udacity.com.

One for the topic I was already pretty familiar with: Linux  Command Basics. and Configuring Linux Web Servers. First one was more of a brushup for my linux terminal commands (Still managed to learn couple of VERY useful things, ls with pattern matching, command history, reverse search). Figured out how to operator terminal’s text editor – nano (which actually was very easy, for some reason I was always intimidated by it.) Oh, and found out about less and its scroll reading ability.

Second course was very exciting. I always felt like I lacked the knowledge of networking and server administration. It helped me catch up a lot. It helped me decipher the meanings of d’s x’s r’s w’s and dashes when you run

ls -al

almost everything about Vagrant, Virtualbox, ports …

Speaking of ports, here’s a useful list of the ones I should probably remember:

Default ports for different protocols:

HTTP: 80, HTTPS: 443, SSH: 22, FTP: 21, POP3: 25, SMTP: 110

The course also taught me a lot about managing users, adding and changing their privileges, groups, owners, setting up firewall (and best practices that come with it, for example, disabling all outgoing and incoming connections as the first step for configuring the firewall then slowly going through the ports that we have to open for our app to be functional) and ssh’ing to the virtual servers – this knowledge will be especially useful in the last phase of liftapp deployment on Digital Ocean.

Next post will include my struggles with Elixir and a little bit about progress on Liftapp itself. I got a bit sidetracked by the server and networking stuff, but it proven to be very fun and useful in the end.

Rolling your own authentication, Elixir and things I learned and a little about our App.

Time flies and so does the progress on our collaboration project – LiftApp. It’s in very crude state, but I’m content with how things are progressing, and about one of those things I’d like to talk about – authentication system and things I learned along the way.

I hear a lot of people say that using devise (alternatively – sorcery) and I wholeheartedly agree – those gems have a lot of support, hundreds of thousands of users testing it, fixing it and polishing in general. It’s bound to have infinitely less security vulnerabilities compared to something, especially a beginner, would try himself / herself.

However, I had very little idea how it all worked under the hood and as part of my deliberate learning process, I spent couple days on thorough research on how it all works.
After that I feel like taking responsibility and try to explain it in-depth. Thanks a lot Michael Hartl and Railstutorial!

For LiftApp the use of authentication system is simple. We want each user to have their own  routines, exercises and statistics, which also means, an associated profile.
And for that we need the user to be able to register on our website (Later on I will finally try out login through FB, twitter without the account creation – No idea how for now, but it’s in the plans)

What have I learned from rolling my own authentication system?

Signup works a lot like typical CRUD (Create, Read, Update, Destroy). However
the RUD part is not neccessary. You create an User model as you would do for any other
model.
– When generating a rails model you can create column names explicitly and implicitly,

bin/rails g model User name email

will give the same result as:

bin/rails g model User name:string email:string

However, I still prefer the explicit way. I believe that saving couple of keystrokes is not worth the potential bug hunting.

Adding index to correct column can greatly improve performance and also prevent some nasty scenarios from happening (double click register for double user database entry anyone?) You can add indices to column through rails migration.
– You can use callbacks before the model is saved. For anything that requires some sort of manipulation you can just use:

before_save { things_in_block_you_want_to_do_before_inserting_to_db }

– has_secure_password is a nice method which is very essential to rolling your own authentication system. It comes from ActiveModel::SecurePassword module . It loads the bcrypt which is a secure  hashing function, adds basic validations for password, adds
authenticate method which is the base of the login part of our authentication system.

Helpers are accessible only views, they are modules so you can include them in controllers to get access to particular helper’s methods.

 Including helper in application_controller.rb will allow the included helper to be available. The reason for that is that all controllers inherit from ApplicationController.

Rails try to match the routes from the top to down of routes.rb meaning that we can still have working routes such as:

get '/signup', to: 'users#new'

and have user resources
declared below that. Signup will work perfectly fine because it will be the first matching route that rails will find (it won’t go to new_user_path)

 

– You can specify different path when using form_for, it’s a method and like most rails methods, it takes extra arguments as hash. Passing url: different_path, will allow you to use specified different_path route.

– HTTP being a stateless protocol disallows us from using any information gained from the previous request.

Session is a place to store any kind of data during one request that you can / want to read and use in other requests. Normally they are stored inside of cookies (which btw, apparently it can hold only 4kb of characters, given that UTF-8 characters are between 1 and 4 bytes, you can store from 1024 to 4096 UTF-8 format characters.) In other words, you can use data stored in cookie at any later time.

Login bases on creating a session which contains user email and then comparing hashed passwords. We use the cookie where we store encrypted users_id which our browsers can retrieve for subsequent requests – thus allowing us to persist our data and stay logged in. Logging out simply deletes the data that browser need to keep the connection “alive”.

That turned out to be way more than i initially expected… In other unrelated news, I picked up myself an elixir book and decided to learn functional programming on the side,
I really love the concept, but there’s definitely a lot to learn before I make anything serious with it. For now I’d love to issue a challenge to myself and figure out the way to somehow use elixir in our contest project!

 

Deliberate learning, programming and you.

I’m big fan of Quora, /r/webdev, /r/learnprogramming, hackernews and many other places where I read and learn about programming.
With such a huge amount of information and learning hubs, comes equally big problem.
Information overload, saturation – you name it. Especially as someone who isn’t well versed in programming, it quickly becomes overwhelming and your learning “progress” isn’t mirrored in your actual coding skills. I used to follow a lot of tutorials from Youtube, Udemy and countless of blogsites (I even signed up for the mailing lists whenever I could, sadly my old mailbox is unusable become of that) and while I felt I’m making progress,
in reality, I wasn’t. What I did was mindlessly type on my keyboard while trying to understand what the hell am I doing. It was fine at the start when the topics were very easy, but the moment it got harder, I just typed the code anyway because why not? That’s what people advised me to do – Type and don’t copy paste.

The sudden reality check hit me when I decided to create my own project. I had an idea, plan and loads of motivation, but the moment I tried to put it to reality – my mind was blank, and to be fair, it got me scared a little. All this time I thought I was learning.

Since then, I completely changed the way I learn new things. I learn deliberately.

deliberate
adjective
dɪˈlɪb(ə)rət/

1. done consciously and intentionally.

How does it look in practice? It’s a lot more work. Learning new language? Test out all the syntax and how you can use it. The moment you learn how to declare variables, use them
in your short program, play around with it. What happens if you assign multiple values to one variable? What can you do with that variable? What if you add two variables together?
How do other programmers use them? Test, test, google, google, test and more test and google, basically test out whatever comes to your mind!

With that approach, I arrived at reading rails code on github and see how it all works under the hood and I’m learning tons of new things.

Try to apply that to everything programming related (and not only).

I think it’s a lot like maths, if you don’t know the basic algebra then you will have issues learning calculus and so on! Learn the basics, learn them well (and deliberately) and when you feel confident, jump to the next part.

Next post will cover a bit about reading Rails source code and what I picked up from it.

RSpec short syntax. Changing the way my specs look.

I found this interesting page in rspec documentation and together with netguru’s open source project I learned much more succinct syntax for Ruby on Rails specs.

Here’s short demonstration of very early model specs in our collaborative LiftApp project.
Before:before_rspec

and after:after_rspec

Which one do you prefer? I personally like the second method better. If I want to add a new validation (and there WILL be more validations) I just have to find the part I want to validate, add context and voila!. Clear and concise. The first screenshot has way too much duplication and can easily become a wall of unreadable specs.

(Please don’t mind the “describe ‘description'” too much ;))

In the next post I will talk a bit about power of deliberate learning and how simply “learning” might not be enough. Until then!

 

Get rid of login/password prompt for git push.

For unknown reasons I just kept retyping it thousands of times in my past projects.
Today was the last time.

How?

Generally when adding / cloning your github repository you probably use something similar to this (at least I did):

git remote add origin https://github.com/yourname/yourrepo

With this you use the HTTPS as a way of authenticating. It requires your
github login and password.

What you should try instead is:

git remote add origin git@github.com:yourname/yourrepo.git

If you already added it with the first method and you want to change the method
of authentication (just like I wanted to) then all you have to do is use:

 set-url 

just like this:

git remote set-url origin git@github.com:yourname/yourrepo.git

Voila! No more login / password prompts.