rake db:migrate
-- runs the migrations and then dumbs the structure of the database to db/schema.rb
.
rake db:test:prepare
-- load the updated (from above rake db:migrate
) schema into the test database.
Because rake db:migrate
dumps the db structure into schema.rb, you can quickly restore the db using the rake db:schema:load
task instead of running all the migrations all over again on a large project. See p143 of Rails 4 In Action.
rake db:test:prepare
is similar to rake db:schema:load
. It loads the schema into the test database making the fields available on the dev db also available on the test db.
rake db:test:clone
is a combination of rake db:schema:dump
and rake db:test:load
. See this SO post.
rake db:rollback
to undo a migration you just ran.
###Find source location of Ruby method
See this blog post. Basically ruby has this method source_location
that you call on a method.
###Run suite of RSpec tests 5 times
Here's a bash one-liner for i in {1..5}; do bundle exec rspec; done
. Pretty cool. See this article for details.
###Git Add Patch Mode for New File
git add -N new_file
git add -p new_file
###Add to params hash from view Talking w Matt, it looks like i can add directly to the params hash from the view. This could be interesting for my search by tags feature.
###Testing JS w Selenium
See the testing JS portion of this railscasts episode.
###jQuery Fundamentals Notes from jQuery Fundamentals course on Pluralsight.
NOTE: This course was from 2011 so some of the specific techniques may be outdated.
What is jQuery? A single JS file. Cross-browser compatible (don't have to worry about which browser you're on). jQuery offers 'Selectors' with which we can select parts of the DOM and manipulate them. jQuery also offers 'Events' and 'Ajax' (asynchronous javascript and xml). Finally, jQuery offers a very rich ecosystem of plugins to add specific functionality.
Why use jQuery?
- Cross browser functionality.
- Locate specific elements with specific classes.
- Apply styles to multiple elements.
- Handle events in a cross-browser manner.
- Makes client-side dev fun again cause you don't have to worry as much about the browser.
Detecting when a Page has loaded.
%script
$(document).ready(function(){
//Perform some action on the page
});
You can also just put your script below the %body
tag so that it is loaded only after the body of the page is loaded.
Note, the $
is an alias that stands for 'jquery'.
Handling Events:
Events notify a program that a user performed some type of action. What JS do you write to handle a button click? Well, it actually differs for each browser. jQuery though, extracts the different code used with each browser and allows you to make one, unified call. See Handling Events, 2:15 through to end of video for discussion of implementing an event function, dblclick
in this case.
See Click Event Demo video at 4:15 which talks forms and events and manipulating element text.
The Mouse Events Demo starting around 5:00 talks through event objects which is really useful.
// This is part of a larger code block...
.mouseup(function (e) {
alert($(e.target).attr('id'));
$(this).text('X: ' + e.pageX + ' Y: ' + e.pageY);
});
Binding to Events with the on()
function.
$('#MyDiv').on('click', function() {
//Handle click event
});
Use off()
to unbind handlers previously bound to an element. See 2:00.
$('#test').click(handler);
can be unbound using $('#test').off();
.
The jQuery Source Viewer looks like a great tool.
Binding with on()
example:
$('#MyDiv').on('mouseenter mouseleave mouseup', function (e) {
$(this).toggleClass('Highlight');
$(this).css('cursor', 'pointer');
if (e.type == 'mouseup') {
$(this).text('X: ' + e.pageX + ' Y: ' + e.pageY);
}
});
live(), delegate(), and on(). These functions allow new DOM elements to automatically be "attached" to an event handler. They also allow children to be added to a container without explicitly attaching an event handler to each child. This may be important when I go to add event handlers to each of my md_notes tags.
NOTE: live()
was removed in jQuery 1.9.
See 7:15 for discussion of using on()
instead of bind()
, delegate()
, and live()
. Looks like I may need this for tagging in my app.
Using on() Demo. Handling events without actually attaching an event handler to every single DOM element in our target.
###JS Testing Setup
- Add Jasmine to Gemfile in the
:test, :development
group. Bundle install rails g jasmine:install
- To see a sample Jasmine test suite, run
rails g jasmine:examples
###button_tag and hidden_field_tag
Rails has some really cool helpers for creating buttons and hidden form fields. See this SO post for a good discussion of the hidden_field_tag
and also check out the Rails API docs for documentation on the helper methods themselves.
###Push 'Dev' branch to github
git push origin dev
###Rails Generators
- Controller --
rails g controller tags index new show ...
###rspec assigns() method
Been getting the following error when running specs in my md_notes app:
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
To resolve, add config.i18n.enforce_available_locales = true
to the body of the Application
class in config/application.rb
.
See this SO post for a great explanation.
###rspec --init
Run rspec --init
from the command line.
This creates:
- /spec
- spec_helper.rb
- .rspec
###Rake tasks
See rake tasks with the rake -T
command. See also rake -P
.
###REGEX
Dir["lib/ruby_meetup/*.rb"].each { |file| require file[/ruby_meetup\/.+/] }
Needed to match the ruby_meetup/some_ruby_file.rb
portion of each file path so I could require the file. This is from the ruby_meetup project. Note the string[/some_regex/]
notation. This way I don't have to do a .match()
deal.
###String Literals http://stackoverflow.com/questions/1634349/escaping-a-string-in-ruby http://stackoverflow.com/questions/15743513/why-does-the-json-returned-from-my-sinatra-app-give-a-syntax-error
Ran up against this with the ruby_meetup gem. Needed to take a JSON response and turn it into a string in order to parse it with JSON.parse(some_string)
.
###TracePoint
http://blog.codesonic.com/2013/01/21/fun-and-frolics-with-the-new-ruby-2-0-0-tracepoint-class/
Trace Ruby program execution. See also get_events.rb
for an implementation.
###Initial Rails/Testing Setup
mkdir md_notes
git init
rvm use ruby-2.0.0@md_notes --create --ruby-version
gem install rails
rails new . --database=postgresql -T
.
-- create rails app in current directory--database=postgresql
-- creates rails app with postgres instead of sqlite-T
-- don't add default test directory structure
- Edit
config/database.yml
remove production configuration and setusername
todavemox
. See commit here.- note,
davemox
is a postgresql user that is automatically created when psql is installed with homebrew
- note,
rake db:create:all
to create all databases in the config- Add the unicorn gem to
Gemfile
andbundle install
. - Setup
unicorn.rb
andProcfile
as required per Heroku.
###NOTES
- Each entry should also have a 'sourced from' path url so I can go and check the actual code snippet on my computer.