Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cgansen committed Sep 24, 2010
0 parents commit ea1f639
Show file tree
Hide file tree
Showing 73 changed files with 9,331 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config/database.yml
config/deploy.rb
log
tmp
3 changes: 3 additions & 0 deletions Capfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'
184 changes: 184 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
asd
== Welcome to Rails

Rails is a web-application and persistence framework that includes everything
needed to create database-backed web-applications according to the
Model-View-Control pattern of separation. This pattern splits the view (also
called the presentation) into "dumb" templates that are primarily responsible
for inserting pre-built data in between HTML tags. The model contains the
"smart" domain objects (such as Account, Product, Person, Post) that holds all
the business logic and knows how to persist themselves to a database. The
controller handles the incoming requests (such as Save New Account, Update
Product, Show Post) by manipulating the model and directing data to the view.

In Rails, the model is handled by what's called an object-relational mapping
layer entitled Active Record. This layer allows you to present the data from
database rows as objects and embellish these data objects with business logic
methods. You can read more about Active Record in
link:files/vendor/rails/activerecord/README.html.

The controller and view are handled by the Action Pack, which handles both
layers by its two parts: Action View and Action Controller. These two layers
are bundled in a single package due to their heavy interdependence. This is
unlike the relationship between the Active Record and Action Pack that is much
more separate. Each of these packages can be used independently outside of
Rails. You can read more about Action Pack in
link:files/vendor/rails/actionpack/README.html.


== Getting started

1. Start the web server: <tt>ruby script/server</tt> (run with --help for options)
2. Go to http://localhost:3000/ and get "Welcome aboard: You’re riding the Rails!"
3. Follow the guidelines to start developing your application


== Web servers

Rails uses the built-in web server in Ruby called WEBrick by default, so you don't
have to install or configure anything to play around.

If you have lighttpd installed, though, it'll be used instead when running script/server.
It's considerably faster than WEBrick and suited for production use, but requires additional
installation and currently only works well on OS X/Unix (Windows users are encouraged
to start with WEBrick). We recommend version 1.4.11 and higher. You can download it from
http://www.lighttpd.net.

If you want something that's halfway between WEBrick and lighttpd, we heartily recommend
Mongrel. It's a Ruby-based web server with a C-component (so it requires compilation) that
also works very well with Windows. See more at http://mongrel.rubyforge.org/.

But of course its also possible to run Rails with the premiere open source web server Apache.
To get decent performance, though, you'll need to install FastCGI. For Apache 1.3, you want
to use mod_fastcgi. For Apache 2.0+, you want to use mod_fcgid.

See http://wiki.rubyonrails.com/rails/pages/FastCGI for more information on FastCGI.

== Example for Apache conf

<VirtualHost *:80>
ServerName rails
DocumentRoot /path/application/public/
ErrorLog /path/application/log/server.log

<Directory /path/application/public/>
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
</Directory>
</VirtualHost>

NOTE: Be sure that CGIs can be executed in that directory as well. So ExecCGI
should be on and ".cgi" should respond. All requests from 127.0.0.1 go
through CGI, so no Apache restart is necessary for changes. All other requests
go through FCGI (or mod_ruby), which requires a restart to show changes.


== Debugging Rails

Have "tail -f" commands running on both the server.log, production.log, and
test.log files. Rails will automatically display debugging and runtime
information to these files. Debugging info will also be shown in the browser
on requests from 127.0.0.1.


== Breakpoints

Breakpoint support is available through the script/breakpointer client. This
means that you can break out of execution at any point in the code, investigate
and change the model, AND then resume execution! Example:

class WeblogController < ActionController::Base
def index
@posts = Post.find_all
breakpoint "Breaking out from the list"
end
end

So the controller will accept the action, run the first line, then present you
with a IRB prompt in the breakpointer window. Here you can do things like:

Executing breakpoint "Breaking out from the list" at .../webrick_server.rb:16 in 'breakpoint'

>> @posts.inspect
=> "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
#<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
>> @posts.first.title = "hello from a breakpoint"
=> "hello from a breakpoint"

...and even better is that you can examine how your runtime objects actually work:

>> f = @posts.first
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
>> f.
Display all 152 possibilities? (y or n)

Finally, when you're ready to resume execution, you press CTRL-D


== Console

You can interact with the domain model by starting the console through script/console.
Here you'll have all parts of the application configured, just like it is when the
application is running. You can inspect domain models, change values, and save to the
database. Starting the script without arguments will launch it in the development environment.
Passing an argument will specify a different environment, like <tt>script/console production</tt>.

To reload your controllers and models after launching the console run <tt>reload!</tt>



== Description of contents

app
Holds all the code that's specific to this particular application.

app/controllers
Holds controllers that should be named like weblog_controller.rb for
automated URL mapping. All controllers should descend from
ActionController::Base.

app/models
Holds models that should be named like post.rb.
Most models will descend from ActiveRecord::Base.

app/views
Holds the template files for the view that should be named like
weblog/index.rhtml for the WeblogController#index action. All views use eRuby
syntax. This directory can also be used to keep stylesheets, images, and so on
that can be symlinked to public.

app/helpers
Holds view helpers that should be named like weblog_helper.rb.

app/apis
Holds API classes for web services.

config
Configuration files for the Rails environment, the routing map, the database, and other dependencies.

components
Self-contained mini-applications that can bundle together controllers, models, and views.

db
Contains the database schema in schema.rb. db/migrate contains all
the sequence of Migrations for your schema.

lib
Application specific libraries. Basically, any kind of custom code that doesn't
belong under controllers, models, or helpers. This directory is in the load path.

public
The directory available for the web server. Contains subdirectories for images, stylesheets,
and javascripts. Also contains the dispatchers and the default HTML files.

script
Helper scripts for automation and generation.

test
Unit and functional tests along with fixtures.

vendor
External libraries that the application depends on. Also includes the plugins subdirectory.
This directory is in the load path.
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
4 changes: 4 additions & 0 deletions app/controllers/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
end
49 changes: 49 additions & 0 deletions app/controllers/page_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class PageController < ApplicationController
helper :page
verify :method => :post, :only => [:remove_link, :add_link]

def index
redirect_to :action => 'home'
end

def home
@links = Link.find(:all)
return if not @links.any?

@max, @min, @divisor = get_link_cloud_data(@links)
end

def add_link
@link = Link.new(params[:link])
@link.save!
@links = Link.find(:all)
@max, @min, @divisor = get_link_cloud_data(@links)
render :partial => 'link', :collection => @links
end

def click_link
link = Link.find(params[:id])
click = Click.new(:ip => request.remote_ip)
link.clicks << click
link.increment!(:click_count)
redirect_to link.url
end

def remove_link
Link.destroy(params[:id])
render :nothing => true
end

protected

def get_link_cloud_data(links)
max = min = links.first.click_count
links.each do |link|
max = link.click_count if !link.click_count.nil? and link.click_count > max
min = link.click_count if !link.click_count.nil? and link.click_count < min
end
divisor = ((max - min) / 7) + 1 # TODO: replace 7 with the sizes array size itself
return max, min, divisor
end

end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
15 changes: 15 additions & 0 deletions app/helpers/page_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module PageHelper

def link_title_text(link)
if link.last_click
pluralize(link.click_count, 'click')
else
'No clicks'
end
end

def sizes
['smaller', 'small', 'medium', 'big', 'bigger', 'huge', 'gigantic']
end

end
3 changes: 3 additions & 0 deletions app/models/click.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Click < ActiveRecord::Base
has_one :link
end
20 changes: 20 additions & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Link < ActiveRecord::Base
acts_as_list

has_many :clicks, :dependent => :destroy

before_create :set_clicks_to_zero

def last_click
updated_at
end

def total_clicks
self.clicks.size
end

protected
def set_clicks_to_zero
self.click_count = 0
end
end
15 changes: 15 additions & 0 deletions app/views/layouts/page.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>dynamic start page</title>
<%= stylesheet_link_tag 'standard' %>
<%= javascript_include_tag :defaults %>
</head>
<body>
<div id="content">
<%= @content_for_layout %>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions app/views/page/_add_link.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= form_remote_tag :url => { :action => 'add_link' }, :update => 'link_cloud' %>
<p><strong>add a new link:</strong> <label for="link_text">text</label>: <%= text_field :link, :text, {:size => 50 } %> <label for="link_url">url</label>: <%= text_field :link, :url, {:size => 50, :value => 'http://' } %> <%= submit_tag "add link" %></p>
</form>
9 changes: 9 additions & 0 deletions app/views/page/_link.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div id="link_<%=link.id%>" style="float:left;">
<%= link_to(link.text, click_url(:id => link, :text => link.text.gsub(/[^\w]+/,'-')), {:class => "link #{sizes[(link.click_count - @min) / @divisor]}", :title => link_title_text(link) }) %>
<span class="link_editor" id="link_editor_<%=link.id%>" style="display:none;">
<%= link_to_remote('remove link',
:url => {:action => 'remove_link', :id => link},
:update => :none,
:confirm => "#{link.text}\n\nRemove this link?",
:before => "Effect.Fade($('link_#{link.id}'), {duration: 0.5} );") %>
</span></div>
1 change: 1 addition & 0 deletions app/views/page/_link_cloud.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= generate_link_cloud %>
10 changes: 10 additions & 0 deletions app/views/page/home.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="link_cloud"><%= render :partial => 'link', :collection => @links %></div>

<div id="helpers">
<div style="padding: 0 5px;">
<p style="float:right"><%= link_to_function 'show link modifiers',
"objs = document.getElementsByClassName('link_editor'); for(i = 0; i < objs.length; i++){Element.toggle(objs[i].id);}"%>
</p>
<%= render :partial => 'add_link' %>
</div>
</div>
Loading

0 comments on commit ea1f639

Please sign in to comment.