Skip to content

Commit

Permalink
something working and specs...
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Sep 11, 2009
1 parent 3e35079 commit fbf152e
Show file tree
Hide file tree
Showing 78 changed files with 5,928 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
*.sqlite3
tmp
logs
26 changes: 19 additions & 7 deletions app/controllers/application_controller.rb
@@ -1,10 +1,22 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
helper :all
protect_from_forgery # See ActionController::RequestForgeryProtection for details
filter_parameter_logging :password

protected

def current_user
@current_user ||= User.find_by_id(session[:user_id])
end
helper_method :current_user

def current_user=(user)
session[:user_id] = user.try(:id)
@current_user = user
end

# Scrub sensitive parameters from your log
# filter_parameter_logging :password
end
def access_denied
flash[:error] = "You do not have access!"
redirect_to login_path
end
end
2 changes: 2 additions & 0 deletions app/controllers/home_controller.rb
@@ -0,0 +1,2 @@
class HomeController < ApplicationController
end
35 changes: 35 additions & 0 deletions app/controllers/restful_controller.rb
@@ -0,0 +1,35 @@
class RestfulController < ApplicationController
before_filter :can_create, :only=>%w[new create]
before_filter :can_write, :only=>%w[edit update destroy]
before_filter :can_read, :only=>%w[show]

make_resourceful do
actions :all
end

protected

def can_create
can_do(:create)
end

def can_write
can_do(:write)
end

def can_read
can_do(:read)
end

def can_do(action)
return if (current_user||User.new).send("can_#{action}?", requested_object)
access_denied
end

def requested_object
case params[:action]
when 'new','create' then build_object
else current_object
end
end
end
22 changes: 22 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,22 @@
class UsersController < RestfulController
skip_before_filter :verify_authenticity_token, :only => [:create]

def new
end

def create
if data = RPXNow.user_data(params[:token])
data = {:name=>data[:username],:email=>data[:email], :identifier=>data[:identifier]}
self.current_user = User.find_by_identifier(data[:identifier]) || User.create!(data)
redirect_to '/'
else
flash[:error] = :default
redirect_to login_path
end
end

def destroy
self.current_user = nil
redirect_to login_path
end
end
5 changes: 5 additions & 0 deletions app/helpers/link_helper.rb
@@ -0,0 +1,5 @@
module LinkHelper
def link_to_s(object)
link_to object.to_s, object
end
end
58 changes: 58 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,58 @@
class User < ActiveRecord::Base
NAME_FORMAT = /^[-_a-zA-Z\d]+$/

validates_uniqueness_of :name, :allow_nil=>false, :on=>:update
validates_uniqueness_of :email, :allow_nil=>true, :on=>:update
validates_format_of :name, :with=>NAME_FORMAT, :on=>:update
validates_format_of :email, :with=>/.+@.+\..+/, :allow_nil=>true, :on=>:update

before_create :clean_name, :uniquify_name, :remove_email_when_taken

def to_s
name
end

def can_create?(object)
return true if admin?
case object
when User then anonymouse?
else raise "NOT SUPPORTED"
end
end

def can_write?(object)
return true if admin?
case object
when User then object == self
else raise "NOT SUPPORTED"
end
end

def can_read?(object)
return true if admin?
case object
when User then true
else raise "NOT SUPPORTED"
end
end

protected

def anonymouse?
new_record?
end

def clean_name
self.name = name.split(//).select{|x| x =~ NAME_FORMAT}.to_s
end

def uniquify_name
while User.find_by_name(name)
self.name = "#{name}#{rand(100)}"
end
end

def remove_email_when_taken
self.email = nil if email and User.find_by_email(email)
end
end
1 change: 1 addition & 0 deletions app/views/home/index.erb
@@ -0,0 +1 @@
TEST
16 changes: 16 additions & 0 deletions app/views/layouts/_flash.erb
@@ -0,0 +1,16 @@
<%
{
:error=>{:color=>'red', :default=>'FAIL!'},
:notice=>{:color=>'green', :default=>'SUCCESS!'}
}.each do |k, v|
%>
<% if flash and flash[k] %>
<div style="color:<%=v[:color]%>">
<% if flash[k] == :default %>
<%= v[:default] %>
<% else %>
<%= flash[k] %>
<% end %>
</div>
<% end %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/layouts/_user.erb
@@ -0,0 +1,3 @@
<% if current_user %>
Hello <%= link_to_s current_user %>
<% end %>
16 changes: 16 additions & 0 deletions app/views/layouts/application.erb
@@ -0,0 +1,16 @@
<html>
<head>
<title>CMDLiner</title>
<link rel="shortcut icon" href="favicon.ico" >
</head>
<body>
<div>
<%= render 'layouts/user' %>
<%= render 'layouts/flash' %>
</div>

<div>
<%= yield %>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions app/views/users/new.erb
@@ -0,0 +1 @@
<%= RPXNow.embed_code('cmdliner', users_url) %>
1 change: 1 addition & 0 deletions app/views/users/show.erb
@@ -0,0 +1 @@
Hello I am <%= @user %> and this is my page!
6 changes: 5 additions & 1 deletion config/environment.rb
Expand Up @@ -3,5 +3,9 @@
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
config.gem "grosser-rpx_now", :lib => 'rpx_now', :version => '0.5.9', :source=>"http://gems.github.com"

config.time_zone = 'UTC'
end
end

RPXNow.api_key = 'd32a482b8a63a6e94a4e7c8422b78047392c6ef5'
8 changes: 4 additions & 4 deletions config/environments/test.rb
Expand Up @@ -22,7 +22,7 @@
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test

# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
# config.active_record.schema_format = :sql
config.gem "rspec", :version=>">=1.2.8", :lib=>false
config.gem "rspec-rails", :version=>">=1.2.7.1", :lib=>false
config.gem "spork", :version=>">=0.5.11", :lib=>false
config.gem "thoughtbot-factory_girl", :version=>'>=1.2.2', :lib => "factory_girl", :source => "http://gems.github.com"
4 changes: 4 additions & 0 deletions config/routes.rb
@@ -1,2 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.root :controller => 'home'
map.login '/login', :controller => "users", :action => "new"

map.resources :users
end
20 changes: 20 additions & 0 deletions db/migrate/20090910191750_add_users.rb
@@ -0,0 +1,20 @@
class AddUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name, :null=>false
t.string :identifier, :null=>false, :unique=>true
t.string :email, :uniq=>true
t.string :website
t.boolean :admin, :default=>false, :null=>false
t.timestamps
end

# enforce uniqueness
add_index :users, :name, :unique => true
add_index :users, :email, :unique => true
end

def self.down
drop_table :users
end
end
27 changes: 27 additions & 0 deletions db/schema.rb
@@ -0,0 +1,27 @@
# This file is auto-generated from the current state of the database. Instead of editing this file,
# please use the migrations feature of Active Record to incrementally modify your database, and
# then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your database schema. If you need
# to create the application database on another system, you should be using db:schema:load, not running
# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

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

create_table "users", :force => true do |t|
t.string "name", :null => false
t.string "identifier", :null => false
t.string "email"
t.string "website"
t.boolean "admin", :default => false, :null => false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["name"], :name => "index_users_on_name", :unique => true

end
10 changes: 10 additions & 0 deletions lib/rails/active_record.rb
@@ -0,0 +1,10 @@
# http://github.com/grosser/clear_empty_attributes/blob/master/lib/clear_empty_attributes.rb
class ActiveRecord::Base
before_validation :clear_empty_attrs
protected
def clear_empty_attrs
@attributes.each do |key,value|
self[key] = nil if value.is_a?(String) && value.blank?
end
end
end
49 changes: 49 additions & 0 deletions lib/ruby/string.rb
@@ -0,0 +1,49 @@
class String
def split_after(max, options={})
zero_length_space = '​' #aka &#8203;
options[:with] ||= zero_length_space
split(/ /).map do |part|
part = part.mb_chars
if part.length > max
part[0...max] + options[:with] + part[max..-1].to_s.split_after(max)
else
part
end
end * ' '
end
end


#ActionView text helpers
class String
%w[auto_link excerpt highlight sanitize simple_format strip_tags word_wrap].each do |method|
eval <<-EVAL
def #{method}(*args)
ActionController::Base.helpers.#{method}(self, *args)
end
EVAL

def truncate(*args)
#called with only a number
if args.size == 1 and args[0].to_s =~ /^\d+$/
ActionController::Base.helpers.truncate(self, :length=>args[0])
else
ActionController::Base.helpers.truncate(self, *args)
end
end
end
end


# user.name.blank_to_nil || 'default name'
class String
def blank_to_nil
blank? ? nil : self
end
end

class NilClass
def blank_to_nil
self
end
end

0 comments on commit fbf152e

Please sign in to comment.