Archive for February, 2011

WordPress determine version number

Wednesday, February 23rd, 2011

wp-includes/version.php

WordPress custom sidebar category filtered archive code

Wednesday, February 23rd, 2011

<div id=”sidebar”>
<ul>
<?php
$myposts = get_posts(“category=1”);
foreach($myposts as $post) : ?>
<li><a href=”<?php the_permalink(); ?>“><?php the_time(‘M’) ?>. – <?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>

mostly in thanks to:

http://www.z-oc.com/blog/2008/03/category-based-archive/

http://www.themightymo.com/2009/07/22/includeexclude-category-from-wordpress-wp_get_archives/

http://kwebble.com/blog/2007_08_15/archives_for_a_category

WordPress Post to Category ID association in the Database

Wednesday, February 23rd, 2011

WordPress 3

Start with a Post and get Category ID:

Get the id number(A) for the post you’re interested in in wp_posts table,  id column

Go to wp_term_relationships table, and find that same id number(A) in the object_id column, read over to the term_taxonomy_id column and get that number, that’s the category id

Start with a Category ID and get Category Name:

Go to the wp_terms table, and find the category you’d like in the name column, read over to the term_id and note the number

Find all Cateogry IDs:

Go to the wp_term_taxonomy table and sort by taxonomy value ‘category category number is in term_taxonomy_id column

Ruby on Rails : rateatrack app notes to self

Tuesday, February 22nd, 2011

add in item to an array/object example:

@artists << track.artist

order object contents example:

def index
@tracks = Track.all(:order => ‘artist‘)

simple rating average math:

rating = params[‘rating’].to_i
@track.average = ((@track.average * @track.numberofvotes) + newlysubmittedrating)/(@track.numberofvotes + 1)
@track.numberofvotes = @track.numberofvotes + 1

simple db update conditional redirect with reporting:

if @track.update_attributes(params[:track])
format.html { redirect_to(“/”, :notice => ‘Track updated.’) }

similar db object one liners:

  • @object.save
  • @object.destroy
  • @track = Track.find(params[:id])
  • @object = Track.new
  • @record = Track.find_by_sql “SELECT * …”

simple char substitution:

somevariablename = variablecontainingcharstoswap.gsub(“_”, ” “)

method simply needs to render a view other than that matching its name:

render :viewname

.each loop fills text variable, then renders it:

@record.each do |track|
$str << “#EXTINFO:#{track.length},#{track.title}<br />#{track.url}<br />”
end
render :text => $str

notice: to substitute variables into such a statement, #{variablename}

simple and example model with validation and some messaging:

class Track < ActiveRecord::Base
validates :url,  :presence => true
validates :title, :presence => true, :length => { :minimum => 1 }
validates :artist, :presence => true
validates :length, :presence => true
validates_format_of :url, :title, :artist, :length, :with =>/^[a-zA-Z \\ \/. : 0-9]+$/x, :message=>”alphanumeric characters, colons, periods, forward/backward slashes only please”
end

routes.rb

Rateatrack::Application.routes.draw do

match “tracks/generate/” => “tracks#generate” // custom :action route
resources :tracks

get “home/index”

root :to => “tracks#index” // sidestep app index, route to desired controller
end

heroku only uses Postgres, used MySQL locally, fun experiment to RELY on persistence:

database.yml

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: rateatrack_development
pool: 5
username: user_name
password: password_removed
socket: /tmp/mysql.sock

production:
encoding: unicode
adapter: postgresql
database: rateatrack_production

simple schema.rb

ActiveRecord::Schema.define(:version => 0) do

create_table “tracks”, :force => true do |t|
t.string  “url”,     :limit => 64
t.string  “title”,   :limit => 64
t.string  “artist”,  :limit => 64
t.integer “length”
t.decimal “average”, :default => 0.0, :precision => 8, :scale => 2
t.integer “votes”,   :default => 0
end

end

RoR version of include, partial, snippet, and so on…

<%= render ‘form’ %>

this will pull from the same folder as the calling resource, file name _form.erb

form example with error checking:

<% @track %>
<%= form_for(@track) do |track_form| %>
<% if @track.errors.any? %>
<%= pluralize(@track.errors.count, “error”) %> prohibited this post from being saved:
<% @track.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
</div>
<% end %>
<table><tr><th><%= track_form.label :url %></th></tr>
<tr><td><%= track_form.text_field :url %></td></tr></table>
<%= track_form.submit %>
<% end %>

Gemfile

gem ‘rails’, ‘3.0.4’
gem ‘mysql2’

Ruby on Rails simple authentication

Tuesday, February 22nd, 2011

project/app/controllers/application_controller.rg

class ApplicationController < ActionController::Base
protect_from_forgery

private

def authenticate
authenticate_or_request_with_http_basic do |user_name, password|
user_name == ‘admin’ && password == ‘password’
end
end
end

example usage: project/app/controllers/example_controller.rb

class TracksController < ApplicationController

before_filter :authenticate, :except => [:index, :show]

end

Create Rails Application Directory Godaddy Hosting

Friday, February 18th, 2011

If you use 4GH:

http://community.godaddy.com/help/article/1091

visit http://www.YOUR-URL-HERE.com/_h_admin_/index.php

Ruby on Rails 3 Mac OSX 10.6.6 – Some Basics

Thursday, February 17th, 2011

If you just switched to Rails 3, prepare for some changes…

Need an IDE?

http://netbeans.org/downloads/index.html

http://www.aptana.com/

Recommended tutorials:

http://netbeans.org/kb/docs/ruby/rapid-ruby-weblog.html

http://edgeguides.rubyonrails.org/getting_started.html

Gems mysql2 problems?:

http://stackoverflow.com/questions/3129644/rails-3-mysql-problems

New Project:

Navigate to the folder where you’d like to create a new project, terminal:

$ rails new projectname

Start Server:

Navigate to the new project, terminal:

$ rails s

Create a database for the new project, terminal (if you have phpMyAdmin simply type name and push Create):

$ mysqladmin -u root -p create projectname

Scaffold example:

$ rails generate scaffold Post name:string title:string content:text

Migrate now (env specific):

$ rake db:migrate RAILS_ENV=production

link_to example:

<%= link_to “My Blog”, posts_path %>

Console:

$ rails console

Active Record Doc:

http://edgeguides.rubyonrails.org/active_record_querying.html

Unescaped HTML (escaped now default in 3):

<%= raw post.name %>

Layout and Rendering:

http://edgeguides.rubyonrails.org/layouts_and_rendering.html

Routing from the Outside In:

http://edgeguides.rubyonrails.org/routing.html

Ruby on Rails Guide:

http://edgeguides.rubyonrails.org/index.html

Ruby on Rails Tutorial:

http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

WordPress control number of posts per archive page

Wednesday, February 16th, 2011

edit archive.php

replace this:

< ?php get_header(); ? >

with something along these lines:

< ?php get_header(); ? >
< ?php
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args=array(
‘showposts’ => 10,
‘paged’=>$paged,
‘caller_get_posts’=> 1
);
query_posts($args);
? >

Perl valid email regular expression

Tuesday, February 1st, 2011

# Check for valid email address

/
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$
/

CSS !important override

Tuesday, February 1st, 2011

A rule that has the !important property will always be applied no matter where that rule appears in the CSS document.

example:

p { color: #ff0000 !important; }

CSS text-transform

Tuesday, February 1st, 2011

{text-transform:uppercase}
{text-transform:capitalize}
{text-transform:lowercase}

also consider:
{font-variant:small-caps}

CSS pseudo elements & pseudo classes

Tuesday, February 1st, 2011

CSS Pseudo Elements

:after
Adds content after an element

:before
Adds content before an element

:first-letter
Adds a style to the first character of a text

:first-line
Adds a style to the first line of a text

example:
h1:after
{
content:url(smiley.gif);
}

Posted on 5 Oct 2009, 17:00 – Category: CSS
CommentsEditDelete

Pseudo Classes

:active
Adds a style to an element that is activated

:first-child
Adds a style to an element that is the first child of another element

:focus
Adds a style to an element that has keyboard input focus

:hover
Adds a style to an element when you mouse over it

:lang
Adds a style to an element with a specific lang attribute

:link
Adds a style to an unvisited link

:visited
Adds a style to a visited link

CSS em

Tuesday, February 1st, 2011

pixels/16=em
example:
h1 {font-size:2.5em} /* 40px/16=2.5em */
h2 {font-size:1.875em} /* 30px/16=1.875em */
p {font-size:0.875em} /* 14px/16=0.875em */

Perl string substitution substr

Tuesday, February 1st, 2011

example:

my $text = “This is one heck of a story”;

my $swap = “heck”;

my $replacement = “hell”;

substr($text, index($text, $swap), length($swap), $replacement);

print $text;

Perl chop chomp

Tuesday, February 1st, 2011

sub spacer

{ print “<br />”; }

my $text = “This is one heck of a story”;

# remove the last character

print chop($text); spacer(); # returns y

print $text; spacer(); # we can see that it also DID alter the variable

# remove newlines from the end of a string only

$text = $text . “\n\n”;

print $text; spacer();

print chomp($text); spacer(); # again, returns # of items “chomp”ed 🙂

print $text; spacer();

Perl output pretty HTML

Tuesday, February 1st, 2011

# outputting pretty HTML

# basically a matter of including \n
# and using Perl qq function

my (@time,@days,@months,$time,$days,$months);

@time = localtime;

@days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

@months = qw(January February March April May June July August September October November December);

print qq(\n<html>\n<head>\n<title>Get the date</title>\n</head>\n<body>\n);

print qq(Today is <strong>$days[$time[6]], $months[$time[4]] $time[3]</strong>.);

print “\n</body>\n”;

print ‘</html>’;

CakePHP Setting up your system to Bake

Tuesday, February 1st, 2011

http://i.justrealized.com/2009/02/16/how-to-run-xampps-php-cli-and-cakephp-console/
start baking:

C:\xampp\htdocs\project\app>”../cake/console/cake” bake

Starting a new project with CakePHP

Tuesday, February 1st, 2011

download the current release:

cakephp.org

unzip, copy/rename folder as desired

check apache file permissions
app/tmp – 0777

app/config
core.php – change Security.salt to new value
database.php.default – remove .default, and configure values according to your database

done

CakePHP naming conventions

Tuesday, February 1st, 2011

URL will trigger controller
example:
http://appname.com/data_users

in app/controllers:
data_users_controller.php
in there:
class DataUsersController extends Controller {
}

this should be the same as the table it will reference:
data_users

in app/models:
data_user.php
class data_user extends AppModel {
}

in app/views/data_users:
function.ctp