Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebycliff/840c6a4a8a38f18e9e9b72a845c17a24 to your computer and use it in GitHub Desktop.
Save codebycliff/840c6a4a8a38f18e9e9b72a845c17a24 to your computer and use it in GitHub Desktop.
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end
gem "rails"
gem "pg"
gem "draper", github: 'showaltb/draper'
gem "pry" # just for debugging
end
require "active_record"
require "action_controller/railtie"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "draper_helper_bug")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :books, force: true do |t|
t.string :name
t.timestamps null: false
end
end
class Book < ActiveRecord::Base
end
class BookDecorator < Draper::Decorator
def summary
[h.app_ctrl_helper, h.my_helper].join " "
end
end
class TestApp < Rails::Application
secrets.secret_token = "secret_token"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
resources :books, only: :index
end
end
class ApplicationController < ActionController::Base
include Rails.application.routes.url_helpers
def app_ctrl_helper
"helper from ApplicationController"
end
helper_method :app_ctrl_helper
end
class BooksController < ApplicationController
def index
Rails.logger.info "The application controller helper method called from controller: " + app_ctrl_helper
Rails.logger.info "The helper method called from controller: " + my_helper
render text: BookDecorator.new(resource).summary
end
private
def resource
book = Book.new
book.name = "test"
book.save!
book
end
def my_helper
"summary!"
end
helper_method :my_helper
end
require "minitest/autorun"
class BookIndexTest < Minitest::Test
include Rack::Test::Methods
def test_index
get "/books"
assert last_response.ok?
assert_equal last_response.body, "helper from ApplicationController summary!"
end
private
def app
Rails.application
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment