Skip to content

Commit

Permalink
serialist
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenezech committed Aug 1, 2009
1 parent 7acab45 commit b5878e4
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 51 deletions.
File renamed without changes.
108 changes: 108 additions & 0 deletions example-template.rb
@@ -0,0 +1,108 @@
#run "echo TODO > README"

plugin "serialist", :git => "http://github.com/bbenezech/serialist.git"
generate :scaffold, "article title:string"
generate :serialist, "SerialistMigration", :articles, :slug
rake "db:migrate"

git :init
file ".gitignore", <<-END
.DS_Store
log/*.log
tmp/**/*
config/database.yml
db/*.sqlite3
END

file "app/views/articles/new.html.erb", <<-END
<h1>New article</h1>
<% form_for(@article) do |f| %>
<%= f.error_messages %>
<%= render :partial => "form"
<% end %>
<%= link_to 'Back', articles_path %>
END

file "app/views/articles/edit.html.erb", <<-END
<h1>Editing article</h1>
<% form_for(@article) do |f| %>
<%= f.error_messages %>
<%= render :partial => "form"
<% end %>
<%= link_to 'Show', @article %> |
<%= link_to 'Back', articles_path %>
END


file "app/views/articles/_form.html.erb", <<-END
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :string_test %><br />
<%= f.text_field :string_test %>
</p>
<p>
<%= f.label :text_test %><br />
<%= f.text_area :text_test %>
</p>
<p>
<%= f.label :select_test %><br />
<%= f.select :select_test %>
</p>
<p>
<%= f.label :checkbox_test %><br />
<%= f.checkbox :checkbox_test %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
END


file "app/views/articles/show.html.erb", <<-END
<p>
<b>Title:</b>
<%=h @article.title %>
</p>
<p>
<b>string_test:</b>
<%=h @article.string_test %>
</p>
<p>
<b>text_test:</b>
<%=h @article.text_test %>
</p>
<p>
<b>select_test:</b>
<%=h @article.select_test %>
</p>
<p>
<b>checkbox_test:</b>
<%=h @article.checkbox_test %>
</p>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
END



file "app/models/article.rb", <<-END
class Article < ActiveRecord::Base
serialist :slug
end
END
run "rm public/index.html"
route "map.root :controller => 'articles'"

git :add => ".", :commit => "-m 'initial commit'"

run "echo Application ready"

4 changes: 2 additions & 2 deletions init.rb
@@ -1,3 +1,3 @@
# Include hook code here
require 'acts_as_serializable'
ActiveRecord::Base.send(:include, ActsAsSerializable)
require 'serialist'
ActiveRecord::Base.send(:include, Serialist)
49 changes: 0 additions & 49 deletions lib/acts_as_serializable.rb

This file was deleted.

48 changes: 48 additions & 0 deletions lib/serialist.rb
@@ -0,0 +1,48 @@
# ActsAsSerializable
module Serialist

def self.included(base)
base.extend ClassMethods
end

module ClassMethods

attr_accessor :serialization_field

def serialist(field)

@serialization_field = field

include ActsAsSerializable::InstanceMethods
end
def inherited(subclass)
super
subclass.instance_variable_set("@serialization_field", @serialization_field)
end
end

module InstanceMethods

def initialize
super
update_attribute(self.class.serialization_field, Hash.new)
end

def method_missing(method, *args, &block)
begin
super
rescue NoMethodError
case method.to_s.last
when "?"
self.send(self.class.serialization_field)[method.to_s[0..-2].to_sym] == (args && args.first || "true")
when "="
self.send(self.class.serialization_field)[method.to_s[0..-2].to_sym] = args.first if args
else
self.send(self.class.serialization_field)[method]
end
end
end
end
end


0 comments on commit b5878e4

Please sign in to comment.