kaminari--kaminari/spec/fake_app.rb

81 lines
2.2 KiB
Ruby
Raw Normal View History

2011-02-08 01:05:19 +00:00
require 'active_record'
require 'action_controller/railtie'
require 'action_view/railtie'
2011-02-11 15:07:26 +00:00
# database
2011-02-08 01:05:19 +00:00
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
ActiveRecord::Base.establish_connection('test')
2011-02-11 15:07:26 +00:00
# config
2011-02-08 01:05:19 +00:00
app = Class.new(Rails::Application)
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
app.config.session_store :cookie_store, :key => "_myapp_session"
app.config.active_support.deprecation = :log
app.initialize!
2011-02-11 15:07:26 +00:00
# routes
2011-02-08 01:05:19 +00:00
app.routes.draw do
resources :users
end
2011-02-11 15:07:26 +00:00
# models
class User < ActiveRecord::Base
has_many :authorships
has_many :readerships
has_many :books_authored, :through => :authorships, :source => :book
has_many :books_read, :through => :readerships, :source => :book
def readers
User.joins(:books_read => :authors).where(:authors_books => {:id => self})
end
scope :by_name, order(:name)
scope :by_read_count, lambda {
cols = if connection.adapter_name == "PostgreSQL"
column_names.map { |column| %{"users"."#{column}"} }.join(", ")
else
'"users"."id"'
end
group(cols).select("count(readerships.id) AS read_count, #{cols}").order('read_count DESC')
}
end
class Authorship < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
class Readership < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
class Book < ActiveRecord::Base
has_many :authorships
has_many :readerships
has_many :authors, :through => :authorships, :source => :user
has_many :readers, :through => :readerships, :source => :user
2011-02-11 15:07:26 +00:00
end
# controllers
2011-02-08 01:05:19 +00:00
class ApplicationController < ActionController::Base; end
class UsersController < ApplicationController
def index
@users = User.page params[:page]
render :inline => <<-ERB
<%= @users.map(&:name).join("\n") %>
<%= paginate @users %>
ERB
end
end
2011-02-11 15:07:26 +00:00
# helpers
2011-02-08 01:05:19 +00:00
Object.const_set(:ApplicationHelper, Module.new)
2011-02-11 15:07:26 +00:00
#migrations
class CreateAllTables < ActiveRecord::Migration
def self.up
2011-02-22 15:20:44 +00:00
create_table(:users) {|t| t.string :name; t.integer :age}
create_table(:books) {|t| t.string :title}
create_table(:readerships) {|t| t.integer :user_id; t.integer :book_id }
create_table(:authorships) {|t| t.integer :user_id; t.integer :book_id }
2011-02-11 15:07:26 +00:00
end
end