Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rf- committed Oct 15, 2012
0 parents commit 63080ae
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
@@ -0,0 +1,19 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
log
gemfiles
20 changes: 20 additions & 0 deletions Appraisals
@@ -0,0 +1,20 @@
appraise "rails30" do
gem "rails", "3.0.15"
end

appraise "rails31" do
gem "rails", "3.1.6"
end

appraise "rails32" do
gem "rails", "3.2.6"
end

appraise "rails4" do
gem "rails",
:git => "git@github.com:rails/rails.git"
gem "activerecord-deprecated_finders",
:git => "git@github.com:rails/activerecord-deprecated_finders"
gem "journey",
:git => "git@github.com:rails/journey"
end
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in keynote.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 Ryan Fitzgerald

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
## Keynote

Presenters for Rails.

### TODO

* Generators.
* Documentation.
11 changes: 11 additions & 0 deletions Rakefile
@@ -0,0 +1,11 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rake/testtask"
require "appraisal"

Rake::TestTask.new do |t|
t.libs.concat %w(keynote spec)
t.pattern = "spec/*_spec.rb"
end

task :default => [:test]
23 changes: 23 additions & 0 deletions keynote.gemspec
@@ -0,0 +1,23 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/keynote/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Ryan Fitzgerald"]
gem.email = ["rwfitzge@gmail.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "keynote"
gem.require_paths = ["lib"]
gem.version = Keynote::VERSION

gem.add_dependency 'rails', '>= 3.0.0'
gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'minitest'
gem.add_development_dependency 'mocha'
gem.add_development_dependency 'pry'
end
33 changes: 33 additions & 0 deletions lib/keynote.rb
@@ -0,0 +1,33 @@
# encoding: UTF-8

require "keynote/version"

require "keynote/controller"
require "keynote/helper"
require "keynote/presenter"

require "keynote/railtie"

module Keynote
class << self
def present(view, *objects)
if objects[0].is_a?(Symbol)
name = objects.shift
else
name = presenter_name_from_object(objects[0])
end

presenter_from_name(name).new(view, *objects)
end

private

def presenter_name_from_object(object)
object.class.to_s.underscore
end

def presenter_from_name(name)
"#{name.to_s.camelize}Presenter".constantize
end
end
end
9 changes: 9 additions & 0 deletions lib/keynote/controller.rb
@@ -0,0 +1,9 @@
# encoding: UTF-8

module Keynote
module Controller
def present(*args)
Keynote.present(view_context, *args)
end
end
end
10 changes: 10 additions & 0 deletions lib/keynote/helper.rb
@@ -0,0 +1,10 @@
# encoding: UTF-8

module Keynote
module Helper
def present(*args)
Keynote.present(self, *args)
end
alias p present
end
end
42 changes: 42 additions & 0 deletions lib/keynote/presenter.rb
@@ -0,0 +1,42 @@
# encoding: UTF-8

module Keynote
class Presenter
class << self
def presents(*objects)
objects.unshift :view
attr_reader *objects

param_list = objects.join(', ')
ivar_list = objects.map { |o| "@#{o}" }.join(', ')

class_eval <<-RUBY
def initialize(#{param_list}) # def initialize(view, foo)
#{ivar_list} = #{param_list} # @view, @foo = view, foo
end # end
RUBY
end
end

def initialize(view)
@view = view
end

def present(*args)
Keynote.present(@view, *args)
end
alias p present

def respond_to_missing?(method_name, include_private = true)
@view.respond_to?(method_name, true)
end

def method_missing(method_name, *args, &block)
if @view.respond_to?(method_name, true)
@view.send(method_name, *args, &block)
else
super
end
end
end
end
23 changes: 23 additions & 0 deletions lib/keynote/railtie.rb
@@ -0,0 +1,23 @@
# encoding: UTF-8

require 'rails/railtie'

module Keynote
class Railtie < Rails::Railtie
config.after_initialize do |app|
app.config.paths.add 'app/presenters', :eager_load => true
end

ActiveSupport.on_load(:action_view) do
include Keynote::Helper
end

ActiveSupport.on_load(:action_controller) do
include Keynote::Controller
end

ActiveSupport.on_load(:action_mailer) do
include Keynote::Controller
end
end
end
5 changes: 5 additions & 0 deletions lib/keynote/version.rb
@@ -0,0 +1,5 @@
# encoding: UTF-8

module Keynote
VERSION = "0.0.1"
end
32 changes: 32 additions & 0 deletions spec/helper.rb
@@ -0,0 +1,32 @@
# encoding: UTF-8

require 'minitest/spec'
require 'minitest/autorun'
require 'mocha'

require 'pry'
require 'rails'
require 'rails/all'

require 'keynote'

# Initialize our test app (by Jose Valim: https://gist.github.com/1942658)

class TestApp < Rails::Application
routes.draw do
get "/hello/world" => "hello#world"
end

config.active_support.deprecation = :log
config.eager_load = false

config.secret_token = 'a' * 100
end

class HelloController < ActionController::Base
def world
render :text => "Hello world!", :layout => false
end
end

TestApp.initialize!
69 changes: 69 additions & 0 deletions spec/keynote_spec.rb
@@ -0,0 +1,69 @@
# encoding: UTF-8

require 'helper'

class NormalPresenter < Keynote::Presenter
presents :model
end

class Normal
end

module Keynote
class NestedPresenter < Keynote::Presenter
presents :model
end

class Nested
end
end

describe Keynote do
describe "with a normal presenter" do
let(:model) { Normal.new }

it "should find and instantiate implicitly" do
p = Keynote.present(:view, model)

p.wont_be_nil
p.must_be_instance_of NormalPresenter

p.view.must_equal :view
p.model.must_equal model
end

it "should find and instantiate explicitly" do
p = Keynote.present(:view, :normal, 'hello')

p.wont_be_nil
p.must_be_instance_of NormalPresenter

p.view.must_equal :view
p.model.must_equal 'hello'
end
end

describe "with a nested presenter" do
let(:model) { Keynote::Nested.new }

it "should find and instantiate implicitly" do
p = Keynote.present(:view, model)

p.wont_be_nil
p.must_be_instance_of Keynote::NestedPresenter

p.view.must_equal :view
p.model.must_equal model
end

it "should find and instantiate explicitly" do
p = Keynote.present(:view, :"keynote/nested", 'hello')

p.wont_be_nil
p.must_be_instance_of Keynote::NestedPresenter

p.view.must_equal :view
p.model.must_equal 'hello'
end
end
end

0 comments on commit 63080ae

Please sign in to comment.