Adding customizable fields in Devise
Here is a good blog post I came across when looking for how to customize my sign-in with Devise and add different fields, like a phone number
http://www.peoplecancode.com/tutorials/adding-custom-fields-to-devise
let's talk about Bridgerton tea, my ask is open
Cosmic Funnies
Three Goblin Art

Kaledo Art
Jules of Nature

No title available
Today's Document

blake kathryn
Sweet Seals For You, Always

ellievsbear
$LAYYYTER

Origami Around

@theartofmadeline
untitled

★
he wasn't even looking at me and he found me
TVSTRANGERTHINGS
PUT YOUR BEARD IN MY MOUTH
One Nice Bug Per Day

Andulka

seen from Italy

seen from Singapore
seen from Malaysia
seen from United States

seen from Malaysia
seen from United States
seen from Sweden

seen from United States
seen from United States

seen from Brazil

seen from United States

seen from United States

seen from United States

seen from Malaysia

seen from Canada
seen from United States

seen from Singapore

seen from United States
seen from Peru
seen from United States
@ajosepha
Adding customizable fields in Devise
Here is a good blog post I came across when looking for how to customize my sign-in with Devise and add different fields, like a phone number
http://www.peoplecancode.com/tutorials/adding-custom-fields-to-devise
Formatting Numbers
I had a coding interview today where the question was how to format numbers. Given a number, such as 12345678, how do you format it the way we read numbers in the US, ie 12,345,678. You need to add a comma every third space, counting backwards from the end of the string. The first comma will be at the index -4, then index -8, and so on and so forth. Or, counting from the other end, you have a comma at index 2 and 7, with the next one coming at index 11.
The first thing I realized is that I would need to convert my FixNum to a string, so that I could have a data structure with an index. Also, whatever happens to the number, I'll need to return a number at the end of the operation.
Then, I need a way of checking if the number has 4 or more digits in it. If it does, then I want to do something to add commas. If not, then I'm free to skip down and just return the number. I also want to see how many commas I'll need to have. I do this by checking how many times the length of the string goes into 3 (since I'll need a break every three characters). Ruby rounds down if there is a remainder, so 5/3 returns 1, which works perfectly, since a 5-digit number would require one comma. But, a six digit number would only need one comma, so i actually need to find out how many commas I need for the length - 1. Othwerwise I would get a strange comma at the begining of the number
Finally, I need to loop through my number and insert a comma every third position, counting from the end of the number. This is a perfect case for a while loop, one that increments up to the number of commas that I will need to insert. Every -4th index position I will insert a comma, then increment my counter up one. I felt it was easier to count backwards from the end of the string.
But, there is also another way of doing this. Instead of looping through the string, I could also create arrays of three digits counting from the end of the number, the join them into a string. Also, counting with negative index numbers is kinda weird, and may pose challenges for other people looking to read the code.
This gist first reverses the string of the number, to avoid the weird counting backwards issue. Then, instead of counting the number of commas I need, the method breaks off units of three from the string and makes them an array. It then pushes that into the empty array I created at the top, units. This will be a 2-d array of however many blocks of three or less(at the end) that I have.
Then, the method inserts a comma between each of the arrays, and converts the units variable into a string with the join method. Finally, I destructively reverse the string to read the number in the correct order (and undo the reverse I did as the first step).
And there you have it! Of course, ruby provides libraries for formatting numbers with international standards, and Rails deals with this issue with NumberHelper to format numbers into strings.
Fun with bash
I got a new computer as a belated birthday present (yay!) and have been playing around with iterm2 and customizing colors. I found a pretty cool site called BashRc generator that lets you create and preview colors for terminal. Here's the link: http://bashrcgenerator.com/
Installing PostGres on Yosemite
I've been having a super rough time installing postgres on Yosemite, and finally figured out how to do it? Installing from the postgres app wouldn't let me install the pg gem, which i wanted for pushing repos to heroku.
The solution was to brew install the latest version of Posgres by running $ brew install postgresql. Then, I ran $ gem install pg, and lo and behold, it worked!
Custom background image in rails
This blog post was really helpful.
http://brandonhilkert.com/blog/page-specific-javascript-in-rails/
Devise me!
As promised in my last blog entry, I’m working on updating my birth control site to Rails, and adding the option of having reviews, so people can share their experiences on birth control pills/patches/rings. To do this, I needed to create some users. Knowing that the users would need to login, I decided to use the great Devise platform to create my user models and views. All was working swimmingly, until I added Foundation to the site, and then the forms got SUPER UGLY.
Yep, that’s not cute. At first I thought I could simply customize the views in my erb file in the app/views/users/sessions/new.html.erb, but any change I made in the erb was simply not showing up on my localhost. I kept getting that super ugly form page, and couldn’t customize it AT ALL!
So, I looked in the terminal to see what was going on, and this is what I saw
Rails wasn’t rendering the html from my erb file, but instead was going to the gemfile to render the page from there. Very strange…
The solution turns out to be in the config/initializers/devise.rb file, where devise gets its settings. I turned the scoped views on, and then my changes to the erb showed up. Problem solved!
TLDR
The problem: I can’t customize my html for users after using the Devise gem
The solution: turn on scoped views in devise config file
What is patch?
I’m going through moving my BCP project to Rails, and I came across the PATCH http method. Patch is used to modify an existing resources, ie edit a review. It’s similar to Put, but put replaces the resources, whereas patch edits them .
What is patch?
I'm going through moving my BCP project to Rails, and I came across the PATCH http method. Patch is used to modify an existing resources, ie edit a review. It's similar to Put, but put replaces the resources, whereas patch edits it.
I'm back in the bay!
Well, it's definitely been a while since I've blogged, but I'm back! Back to blogging, and back in San Francisco (where I grew up). While the city has certainly changed a lot since I was a young thing in high school, at least the Richmond district hasn't undergone a revolution (at least not one that I can see yet). I'm writing in the old café Zephyr, now known as La Promenade Cafe, but despite the French name, the food is still mediocre and the ambiance is still wonderful. Before I moved to SF I took a month off to travel around Israel, Jordan and Germany.
Beach in Tel Aviv
Petra in Jordan
Church in Berlin
The places may seem random, but that's where my international friends are these days, and I'm a consummate cheapskate and hate paying for hotels. Then I had thanksgiving with the fam in Arizona. Suffice it say, after all this travel, I'm very happy to be in one place for a while.
In my free time I've been going back to JavaScript basics, and reading Eloquent JavaScript. I like the book a lot, and it does an excellent job of explaining some JavaScript nuances. I'm also moving my BCP project over to Rails, so keep tuned for updates!
Back to EloquentJS. One of the problems was how to flatten an array.
The first thing I had to think about was how to use "reduce." According to MDN, "The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value." Ooof what does that mean!
Reduce, similar to forEach, takes an array and iterates through it, performing some sort of function on the current value and then returns it. So, it can be used to find and return a minimum value in an array.
Here is my solution:
The first thing I am doing is calling reduce on the variable arrays (which was defined in the problem). Reduce then takes a callback function with three arguments: the previous value in the array, the current value in the array, and the index. The if statement ensures that reduce will not try to evaluate an undefined value, which will cause an error.
Then, the newArray variable is the concatenated array that will be returned.
And there you go!
Angular ng-class
Long time no writing, but here I am again. In my job i'm using a lot of AngularJS, and I wanted to share one of my favorite angular built-in angular directives--ng-class.
Ng-class is a way to dynamically assign css classes to your DOM elements. So for example, if you have an object of items like the following
And you want the text to turn red when the value is close. You could accomplish this with an ng class that adds the class red
In your html, you can accomplish this with an ng-class. in your HTML, you would write the following:
The first variable in the ng-class, in quotes, is the class you want to dynamically add. The second variable (not in quotes) is the expression you want the class applied to. In this case it is where the menuitem is closed.
And there you go!
Interview prep with linked lists
I'm prepping for interviews, and it seems like one topic I'm going to be coming across is linked lists. I found a good gist of implementing a linked list. Linked lists are a data structure that have nodes that contain values. There is a null value before the head of the node, and one after the last node in the list. Each node has a connection to the values around it, but unlike in the case of arrays there is no index value with linked lists, so only push and pop operations are available. Furthermore, finding specific values require traversing the whole linked list.
Implementing a linked list requires two classes--one for the nodes and one for adding a value to the list. In this blog post, i'm only going to discuss the mechanism behind the push operation of the linked list, since the other methods have somewhat similar code behind them.
So the first step in creating a linked list is to create the nodes.
The nodes are a class, with attr_accessors value and next node. So, the value and next_node can change In order for a node to exist, it must have a value, so it is initialized with a value. It also must have the possibility for a node after it, to keep track of its position (unlike the diagram, these nodes only know about the node after them).
Next, we need to create the list.
The first thing to do is initialize the list. It's initialized with a head. If the head is nil, the first value is the value initialized with.
If the list doesn't have any values, then the added value will be the head value. Otherwise, you need to walk down the list using the next method until you reach the bottom of the list. At that point you will exit the while loop and the value at the end of the list is the value you are looking to add.
And that's it! Stay tuned for more interview news.
Testing 1, 2, 3
Last week I had the chance to attend a 3-day testing workshop at Flatiron. It was super helpful, and a great chance to get better at test-driven development. Test driven development involves writing tests for your methods, then writing the methods themselves. Effective testing describes what your methods will do, and as the application grows, it makes it easier to pinpoint where there are errors. Writing the tests first works great as a guideline so you know exactly what the outcomes of each method will be. As applications become larger, testing helps ensure that methods don’t break, and if they do, points out exactly what is going wrong.
Types of testing
Overall, there are two types of testing: Behavior-driven development, and test-driven development. They use different tools to run the tests and work slightly differently. BDD works on testing a feature as a whole, and requires you to think like the user. It works from the outside in, and examines the feature as a whole.
TDD looks at your methods themselves. You describe the method, and what you expect the result to be. The following diagram shows the testing framework well. The outer layer represents the BDD, where you test features and get them to fail. Once you write failing tests, you can go into the TDD layer and write unit-tests that will pass. Then go back out and refactor your code in the BDD layer, and violà you’re a testing pro!
Testing tools
Rspec –this is your main testing feature in Ruby.
Cucumber—This is what you use for feature tests
FactoryGirl—This will give you test data to use
Capybara—this lets you work with a “headless” browser so you can look at features that the browser would render
Fish on Wheels
A small fishtank that is driven by a goldfish with the help of computer vision, put together by studio diip - video embedded below:
By using a camera and computer vision software it is possible to make a fish control a robot car over land. By swimming towards an interesting object, the fish can explore the world beyond the limits of his tank.
Link
How I feel about writing code sometimes
It's may not be the best way to do things, and it's probably pretty dangerous, but I guess it gets the job done...
Ouch!
I'm Partial to Partials!
So I've been working on refactoring our code to make it a bit cleaner in our game. In the first iteration, I put a lot of the logic in the views file which made it pretty damn bloated. The views had to have some logic (aside from the layout), to evaluate different values that were present only in the view, using variables that came from our models and the database. It ended up looking pretty crazy
So, you may ask, what are partials you may ask? Partials (or the files that start with an underscore in your code), are bits of re-usable code that you can insert into a view file to DRY it up. In rails, they are marked with an underscore (_) before the name of the file. They are also used in Sinatra and Angular JS, which renders different partials instead of a new page.
You may need a partial if you call each on one of your variables multiple times. In a way, it is kind of like storing a method in your code, since you can call the partial multiple times. In our map project for example we use partials to tell the player if he or she has certain features available in the zip code.
The formatted_feature variable is passed in from the view like so.
This is telling ruby to get the partial named "buystatus", which appears as _buystatus in the file structure, and pass it to local variables. The locals hash passes in variables from the view to the partial. In this case, "items" represents the community centers, and the second value-pair represents the name "community_centers" (so we didn't have to type the name of every feature, we stored it in an array then called the values of the array).
One mistake I made is not giving every partial with the same name the same number of local variables when I passed it in my view.
D3 visualizations in HTML
Yesterday we started learning about D3 visualizations in HTML and SVG The difference between the two is that html can render rectangles and bars, while SVG can render different shapes. There are a 4 basic steps to creating a D3 visualization.
API/DOM selectors
At the start of the visualization it is unlikely that you will have divs already assigned, and will have an empty. Here, the code is selecting all divs with a class of chart. In the html, there aren't any divs with a class of chart, but this isn't a problem since D3 will go in at a later stage and fill in these divs.
2.
This binds the data to the divs.
3.
This step will match the data and divs that you have. If you are adding data that doesn't have a div yet, it will create new divs using the append function, and if not, it will remove divs using the remove function.
4. This step will create the visualization, telling the bars to be ten times the value specified in data.
Old MacDonald had a farm
One of the things that has been most complicated for me are the ActiveRecord associations when using Rails. I was talking with my teammate Sterling about this today, and she used an excellent metaphor of a farmer. So, this blog post will expand on that. I'm going to attempt to explain the belongs_to has_many relationship, and the has_one belongs_to association. Hopefully this will prevent confusion in the future.
So, Old MacDonald has a farm.
And on this farm he has animals, such as cow, a duck, a sheep and a dog. If we are representing the local farms in this app, in our model the cows would be farm/app/models/cow.rb.
And, our farmer model is represented here at farm/app/models/farmer.rb. I'm only looking at one farmer that there is only one farmer on this farm, though old MacDonald could have a family or live in a commune with other farmers. The model supports having multiple farmers.
Let's look at the cows, understanding that Old MacDonald can have other animals, but their database representation will follow this same pattern. Old MacDonald has many cows, and each cow belongs only to Old MacDonald. So, Old MacDonald has_many cows (plural), and the cows(plural) belong_to Old MacDonald. In the database, the class that is belonging to another class would have a column with a foreign key to represent this relationship. so the cow model will hold the relationship with Old MacDonald. So our migration at farm/app/db/20140608/_create_cows would look something like the following:
As you can see, there is a column with the belongs_association in the migration. This is a one-to-many type of association. Farmers can have many cows, but each cow can only belong to one farmer. The has_many and belongs_two associations are somewhat paired. My farmer must have_many (though he could only have one or zero, depending on what kind of farmer he is), cows, while the cows must belong_to a single farmer.
Now onto the has_one type of association. This type of association sets up a one-to-one relationship between models. Both elements or classes must have exactly one instance of the other class. On a sidenote, the has_many type of association returns an array of Active Record objects when queried by the database. This next type of association, the has_one, returns a single ActiveRecord object when queried by the database.
Let's give our cows a brand. The would be a has one association, since each cow can only fit one brand on their body. Editing our Cow model at farm/app/models/cow.rb, we would have something like:
As you can see in the code, our cow has_one brand. Now, if we were to keep track of the brands, the brand belong_to a cow, setting up this one:one relationship. In our code, the model in farm/app/models/brand.rb would be represented as
Like the cows table, in our database, the brands would have a column with the cow_id to show which cow they belong to. The migration would look something like the following
As you can see, just like in the cow table, the brands table have a belongs_to column that would hold the ID of the cow. One of the nice things that ActiveRecord does is increment the ID's of the items in your table once you create a database, so the column for cow_id would be automatically generated by some awesome Rails magic.
<script src="https://gist.github.com/ajosepha/8709589.js"></script>And there you have it! Let's go eat a steak!