Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krschacht committed Sep 13, 2010
0 parents commit 4c35934
Show file tree
Hide file tree
Showing 27 changed files with 397 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.rdoc
@@ -0,0 +1,4 @@
== Overview

Details...

21 changes: 21 additions & 0 deletions Rakefile
@@ -0,0 +1,21 @@
require 'rake/testtask'

Rake::TestTask.new do |test|
test.pattern = 'test/**/*_test.rb'
test.libs << 'test'
end


begin
require "jeweler"
Jeweler::Tasks.new do |gem|
gem.name = "cheese"
gem.summary = "Description of your gem"
gem.email = "you@email.com"
gem.authors = ["Your Name"]
gem.files = Dir["{lib}/**/*", "{app}/**/*", "{config}/**/*"]
end
Jeweler::GemcutterTasks.new
rescue
puts "Jeweler or dependency not available."
end
1 change: 1 addition & 0 deletions VERSION
@@ -0,0 +1 @@
0.2.2
15 changes: 15 additions & 0 deletions app/controllers/cheese/widgets_controller.rb
@@ -0,0 +1,15 @@
module Cheese
class WidgetsController < ApplicationController

unloadable

layout 'cheese' # this allows you to have a gem-wide layout

def index
end

def show
end

end
end
2 changes: 2 additions & 0 deletions app/helpers/application_helper.rb
@@ -0,0 +1,2 @@
## Look in lib/application_helper.rb
## I can't figure out how to get it included unless I put it that directory
9 changes: 9 additions & 0 deletions app/helpers/cheese/widgets_helper.rb
@@ -0,0 +1,9 @@
module Cheese
module WidgetsHelper

def helper_for_widgets_view
"this is coming from a helper method just for the widgets controller"
end

end
end
10 changes: 10 additions & 0 deletions app/models/cheese/widget.rb
@@ -0,0 +1,10 @@
module Cheese
class Widget < ActiveRecord::Base
set_table_name "cheese_widgets"

def make
puts "widget made"
end

end
end
5 changes: 5 additions & 0 deletions app/views/cheese/widgets/index.html.erb
@@ -0,0 +1,5 @@
widget#index<br>
<br>
<%= helper_for_widgets_view %><br>
<br>
<%= app_wide_helper_method %>
1 change: 1 addition & 0 deletions app/views/cheese/widgets/show.html.erb
@@ -0,0 +1 @@
widgets#show
9 changes: 9 additions & 0 deletions app/views/layouts/cheese.html.erb
@@ -0,0 +1,9 @@
<% content_for :content do %>
<%= stylesheet_link_tag("cheese") %>
<%= yield %>
<% end -%>
<%= render :file => 'layouts/application' %>
50 changes: 50 additions & 0 deletions cheese.gemspec
@@ -0,0 +1,50 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{cheese}
s.version = "0.2.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Your Name"]
s.date = %q{2010-09-11}
s.email = %q{you@email.com}
s.extra_rdoc_files = [
"README.rdoc"
]
s.files = [
"app/controllers/cheese/widgets_controller.rb",
"app/helpers/application_helper.rb",
"app/helpers/cheese/widgets_helper.rb",
"app/models/cheese/widget.rb",
"app/views/cheese/widgets/index.html.erb",
"app/views/layouts/forums.html.erb",
"config/routes.rb",
"lib/acts_as_widget/base.rb",
"lib/application_helper.rb",
"lib/cheese.rb",
"lib/engine.rb",
"lib/rails/generators/cheese/cheese_generator.rb",
"lib/rails/generators/cheese/templates/initializer.rb",
"lib/rails/generators/cheese/templates/migration.rb",
"lib/rails/generators/cheese/templates/schema.rb",
"lib/rails/railties/tasks.rake"
]
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Description of your gem}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
end

12 changes: 12 additions & 0 deletions config/routes.rb
@@ -0,0 +1,12 @@
Rails.application.routes.draw do |map|

mount_at = Cheese::Engine.config.mount_at

match mount_at => 'cheese/widgets#index'

map.resources :widgets, :only => [ :index, :show ],
:controller => "cheese/widgets",
:path_prefix => mount_at,
:name_prefix => "cheese_"

end
34 changes: 34 additions & 0 deletions lib/acts_as_widget/base.rb
@@ -0,0 +1,34 @@
module Cheese
module ActsAsWidget

module Base
def self.included(klass)
klass.class_eval do
extend Config
end
end

module Config
def acts_as_widget

# This is where arbitrary code goes that you want to
# add to the class that declared "acts_as_widget"

has_many :widgets, :class_name => 'Cheese::Widget'

include Cheese::ActsAsWidget::Base::InstanceMethods
end
end

module InstanceMethods

def factory_name
"this is an example instance method"
end

end # InstanceMethods
end
end
end

::ActiveRecord::Base.send :include, Cheese::ActsAsWidget::Base
7 changes: 7 additions & 0 deletions lib/application_helper.rb
@@ -0,0 +1,7 @@
module ApplicationHelper

def app_wide_helper_method
"this is an app-wide helper method that is declared within the gem"
end

end
4 changes: 4 additions & 0 deletions lib/cheese.rb
@@ -0,0 +1,4 @@
module Cheese
require 'engine' if defined?(Rails) && Rails::VERSION::MAJOR == 3
require 'acts_as_widget/base'
end
30 changes: 30 additions & 0 deletions lib/engine.rb
@@ -0,0 +1,30 @@
require 'cheese'
require 'rails'
require 'action_controller'
require 'application_helper'

module Cheese
class Engine < Rails::Engine

# Config defaults
config.widget_factory_name = "default factory name"
config.mount_at = '/'

# Load rake tasks
rake_tasks do
load File.join(File.dirname(__FILE__), 'rails/railties/tasks.rake')
end

# Check the gem config
initializer "check config" do |app|

# make sure mount_at ends with trailing slash
config.mount_at += '/' unless config.mount_at.last == '/'
end

initializer "static assets" do |app|
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end

end
end
75 changes: 75 additions & 0 deletions lib/rails/generators/cheese/cheese_generator.rb
@@ -0,0 +1,75 @@
require 'rails/generators'
require 'rails/generators/migration'

class CheeseGenerator < Rails::Generators::Base
include Rails::Generators::Migration

def self.source_root
File.join(File.dirname(__FILE__), 'templates')
end

def self.next_migration_number(dirname) #:nodoc:
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
else
"%.3d" % (current_migration_number(dirname) + 1)
end
end


# Every method that is declared below will be automatically executed when the generator is run

def create_migration_file
f = File.open File.join(File.dirname(__FILE__), 'templates', 'schema.rb')
schema = f.read; f.close

schema.gsub!(/ActiveRecord::Schema.*\n/, '')
schema.gsub!(/^end\n*$/, '')

f = File.open File.join(File.dirname(__FILE__), 'templates', 'migration.rb')
migration = f.read; f.close
migration.gsub!(/SCHEMA_AUTO_INSERTED_HERE/, schema)

tmp = File.open "tmp/~migration_ready.rb", "w"
tmp.write migration
tmp.close

migration_template '../../../tmp/~migration_ready.rb',
'db/migrate/create_cheese_tables.rb'
remove_file 'tmp/~migration_ready.rb'
end

def copy_initializer_file
copy_file 'initializer.rb', 'config/initializers/cheese.rb'
end

def update_application_template
f = File.open "app/views/layouts/application.html.erb"
layout = f.read; f.close

if layout =~ /<%=[ ]+yield[ ]+%>/
print " \e[1m\e[34mquestion\e[0m Your layouts/application.html.erb layout currently has the line <%= yield %>. This gem needs to change this line to <%= content_for?(:content) ? yield(:content) : yield %> to support its nested layouts. This change should not affect any of your existing layouts or views. Is this okay? [y/n] "
begin
answer = gets.chomp
end while not answer =~ /[yn]/i

if answer =~ /y/i

layout.gsub!(/<%=[ ]+yield[ ]+%>/, '<%= content_for?(:content) ? yield(:content) : yield %>')

tmp = File.open "tmp/~application.html.erb", "w"
tmp.write layout; tmp.close

remove_file 'app/views/layouts/application.html.erb'
copy_file '../../../tmp/~application.html.erb',
'app/views/layouts/application.html.erb'
remove_file 'tmp/~application.html.erb'
end
elsif layout =~ /<%=[ ]+content_for\?\(:content\) \? yield\(:content\) : yield[ ]+%>/
puts " \e[1m\e[33mskipping\e[0m layouts/application.html.erb modification is already done."
else
puts " \e[1m\e[31mconflict\e[0m The gem is confused by your layouts/application.html.erb. It does not contain the default line <%= yield %>, you may need to make manual changes to get this gem's nested layouts working. Visit ###### for details."
end
end

end
8 changes: 8 additions & 0 deletions lib/rails/generators/cheese/templates/initializer.rb
@@ -0,0 +1,8 @@
module Cheese
class Engine < Rails::Engine

config.mount_at = '/cheese'
config.widget_factory_name = 'Factory Name'

end
end
9 changes: 9 additions & 0 deletions lib/rails/generators/cheese/templates/migration.rb
@@ -0,0 +1,9 @@
class CreateCheeseTables < ActiveRecord::Migration
def self.up
SCHEMA_AUTO_INSERTED_HERE
end

def self.down
drop_table :cheese_widgets
end
end
11 changes: 11 additions & 0 deletions lib/rails/generators/cheese/templates/schema.rb
@@ -0,0 +1,11 @@
ActiveRecord::Schema.define(:version => 0) do

create_table :cheese_widgets, :force => true do |t|
t.string :title
t.datetime :created_at
t.datetime :updated_at
end

add_index :cheese_widgets, [:title]

end
8 changes: 8 additions & 0 deletions lib/rails/railties/tasks.rake
@@ -0,0 +1,8 @@
namespace :cheese do

desc "example gem rake task"
task :report => :environment do
puts "you just ran the example gem rake task"
end

end
Binary file added public/images/gradient_grey.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/stylesheets/cheese.css
@@ -0,0 +1,3 @@
.cheese {
color: red;
}
4 changes: 4 additions & 0 deletions test/database.yml
@@ -0,0 +1,4 @@
mysql:
adapter: mysql
host: localhost
database: cheese_plugin_test
2 changes: 2 additions & 0 deletions test/fixtures/cheese_widget.yml
@@ -0,0 +1,2 @@
first:
title: This is the title

0 comments on commit 4c35934

Please sign in to comment.