rughetto-rear_views 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Generators ADDED
@@ -0,0 +1,3 @@
1
+ scope 'merb-gen' do
2
+ Merb.add_generators( File.join( File.dirname(__FILE__), 'lib', 'generators', 'rear_views_generator' ) )
3
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Rue The Ghetto
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,48 @@
1
+ rear_views
2
+ ==========
3
+
4
+ A plugin for the Merb framework that provides modular REST views for a resource. That way complex views and forms can be easily built from highly conventioned parts. Currently this only supports the ActiveRecord ORM with ERB templates. Chip in and add some others if you think this a laudable project.
5
+
6
+
7
+ Motivation
8
+
9
+ So, what's wrong with the existing views. Other than the fact that Merb's generators don't actually add any code related to a model's attributes (which can slow a person down), there is a bigger modularity issue. When you are working with complex views and forms, a little _more_ convention over configuration goes a long way. When you have little partials to build into the views of association models, things are much easier.
10
+
11
+ This plugin breaks the standard views into some additional partials that make it really easy to build complex views using convention. It also, does this ass-backwards to the Merb/Rails way, allowing you to build your schema and relationships first. After the schema is established, you can scaffold your views.
12
+
13
+
14
+ Install It:
15
+
16
+ 1. Add the gem to your dependency.rb file (and thor:gem:install in into your project)
17
+ dependency "rughetto-merb_rear_views", :require_as => "rear_views"
18
+ 2. Do the command line merb-gen thing, with your model name as the only argument
19
+ merb-gen rear_views User
20
+
21
+
22
+ What It Does:
23
+
24
+ Using it will build the following view files in the usual place:
25
+ _form.html.erb
26
+ _full_view.html.erb
27
+ _list_view.html.erb
28
+ delete.html.erb
29
+ edit.html.erb
30
+ index.html.erb
31
+ new.html.erb
32
+ show.html.erb
33
+
34
+ The _form partial is included in both the new and edit templates. It only includes the fields and not the form tags. That means that if you are building a form for a related model you can grab this partial and use it whole hog in a fields_for block. How much easier is that!
35
+
36
+ The _full_view and _list_view partials offer the same modular flexibility. The only caveat is that there is no guess work about what fields might be relevant for the list view. So you have to fix this one up yourself.
37
+
38
+
39
+ Warings
40
+
41
+ This isn't quite production ready and may be irrelevant since Merb is going away.
42
+ Known issues:
43
+ * merb-gen doesn't load the app prior to running the generators. Currently there is a hack that connects the database and loads the classes, but dependencies in the models won't currently be loaded unless explicitly declared in the file. I am still experimenting with how best to do this.
44
+ * Only supports AR with ERB.
45
+
46
+ Future Work
47
+
48
+ I put this together to DRY up the repetitive work that I do building applications with more complex data relations/interfaces. Rails has better support for scaffolding out your views, but it doesn't have these micro-partial conventions for building your views faster. When Rails and Merb merge, I will make sure to bring this along too ... unless I am really busy or just generally distracted.
data/Rakefile ADDED
@@ -0,0 +1,72 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+ require 'spec/rake/spectask'
7
+
8
+ GEM_NAME = "rear_views"
9
+ GEM_VERSION = "0.0.2"
10
+ AUTHOR = "Rue The Ghetto"
11
+ EMAIL = "ru_ghetto@rubyghetto.com"
12
+ HOMEPAGE = "http://github.com/rughetto/rear_views"
13
+ SUMMARY = "Generators REST views for a resource, so that complex views and forms can be easily built. Currently only AR introspection and ERB templates."
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.rubyforge_project = 'merb'
17
+ s.name = GEM_NAME
18
+ s.version = GEM_VERSION
19
+ s.platform = Gem::Platform::RUBY
20
+ s.has_rdoc = true
21
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
22
+ s.summary = SUMMARY
23
+ s.description = s.summary
24
+ s.author = AUTHOR
25
+ s.email = EMAIL
26
+ s.homepage = HOMEPAGE
27
+ s.add_dependency('merb-core')
28
+ s.require_path = 'lib'
29
+ s.files = %w(LICENSE README Rakefile TODO Generators) + Dir.glob("{lib,spec}/**/*")
30
+ end
31
+
32
+ Rake::GemPackageTask.new(spec) do |pkg|
33
+ pkg.gem_spec = spec
34
+ end
35
+
36
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|bccwin|cygwin/) rescue nil
37
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
38
+ desc "Install #{GEM_NAME}"
39
+ if WIN32
40
+ task :install => :gem do
41
+ system %{gem install --no-rdoc --no-ri -l pkg/#{GEM_NAME}-#{GEM_VERSION}.gem}
42
+ end
43
+ namespace :dev do
44
+ desc 'Install for development (for windows)'
45
+ task :winstall => :gem do
46
+ warn "You can now call 'rake install' instead of 'rake dev:winstall'."
47
+ system %{gem install --no-rdoc --no-ri -l pkg/#{GEM_NAME}-#{GEM_VERSION}.gem}
48
+ end
49
+ end
50
+ else
51
+ task :install => :package do
52
+ sh %{#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION}.gem}
53
+ end
54
+ end
55
+
56
+ desc "Uninstall the gem"
57
+ task :uninstall do
58
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
59
+ end
60
+
61
+ desc "Create a gemspec file"
62
+ task :gemspec do
63
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
64
+ file.puts spec.to_ruby
65
+ end
66
+ end
67
+
68
+ Spec::Rake::SpecTask.new do |t|
69
+ t.warning = true
70
+ t.spec_opts = ["--format", "specdoc", "--colour"]
71
+ t.spec_files = Dir['spec/**/*_spec.rb'].sort
72
+ end
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ TODO:
2
+
3
+ Test in the wild!
@@ -0,0 +1,78 @@
1
+ require 'merb_activerecord'
2
+ require 'activerecord'
3
+
4
+ module Merb::Generators
5
+ class RearViewsGenerator < NamespacedGenerator
6
+
7
+ def self.source_root
8
+ File.dirname(__FILE__) / 'templates'
9
+ end
10
+
11
+ desc <<-DESC
12
+ Generators modular REST views for a resource, so that complex views and forms can be easily built
13
+ DESC
14
+
15
+ first_argument :name, :required => true, :desc => "model name"
16
+
17
+ # all the view templates
18
+ [:index, :show, :edit, :new, :delete, :_form, :_list_view, :_full_view ].each do |view|
19
+ template "#{view}".to_sym do |template|
20
+ template.source = "%file_name%/#{view}.html.erb"
21
+ template.destination = "app/views" / base_path / "#{file_name.pluralize}/#{view}.html.erb"
22
+ end
23
+ end
24
+
25
+ # methods available to the views ---------------
26
+ def klass
27
+ if @klass.blank?
28
+ begin
29
+ Object.full_const_get( class_name ) # only works for testing
30
+ rescue
31
+ # for regular merb-gen use the database has not yet connected, so ...
32
+ Merb::Config.session_stores = []
33
+ Merb::Orms::ActiveRecord::Connect.run unless Merb.disabled? :initfile
34
+ # also the model classes haven't been loaded ...
35
+ Merb::BootLoader::LoadClasses.run
36
+ end
37
+ @klass ||= Object.full_const_get( class_name )
38
+ end
39
+ @klass
40
+ end
41
+
42
+ def columns
43
+ if @columns.blank?
44
+ @columns ||= {}
45
+ klass.columns.each do |col|
46
+ @columns[col.name] = col.type unless ['id', 'created_at', 'updated_at'].include? col.name
47
+ end
48
+ end
49
+ @columns
50
+ end
51
+
52
+ def relationships
53
+ @relationships ||= klass.reflections.keys
54
+ end
55
+
56
+ def singular_name
57
+ symbol_name.singularize
58
+ end
59
+
60
+ def plural_name
61
+ singular_name.pluralize
62
+ end
63
+
64
+ def instance_var
65
+ "@#{singular_name}"
66
+ end
67
+
68
+ def resource_sym
69
+ ":#{singular_name}"
70
+ end
71
+
72
+ def resources_sym
73
+ ":#{symbol_name}"
74
+ end
75
+ end
76
+
77
+ add :rear_views, RearViewsGenerator
78
+ end
@@ -0,0 +1,28 @@
1
+ <%
2
+ columns.each do |key, val| -%>
3
+ <% key = key.to_s
4
+ val = val.to_sym
5
+ if key.match(/_id$/)
6
+ %>
7
+ <%%= select :<%= key %>,
8
+ :collection => <%= key.classify %>.all,
9
+ :text_method => :id, # change this to something appropriate for the model
10
+ :value_method => :id,
11
+ :selected => <%= instance_var %>.<%= key %>.to_s,
12
+ :label => <%= key.humanize %> %>
13
+ <%
14
+ elsif val == :text
15
+ -%>
16
+ <%%= text_area :<%= key %>, :label => <%= key.humanize %> %>
17
+ <%
18
+ elsif val == :boolean
19
+ -%>
20
+ <%%= check_box :<%= key %>, :label => <%= key.humanize %> %>
21
+ <%
22
+ else
23
+ -%>
24
+ <%%= text_field :<%= key %>, :label => <%= key.humanize %> %>
25
+ <%
26
+ end
27
+ end
28
+ -%>
@@ -0,0 +1,7 @@
1
+ <h1><%= class_name.humanize %> - #<%%= <%= instance_var %>.id %></h1>
2
+ <% columns.each do |key, val| -%>
3
+ <p>
4
+ <b><%= key %></b>:
5
+ <%%= <%= instance_var %>.<%= key %> %>
6
+ </p>
7
+ <% end -%>
@@ -0,0 +1,2 @@
1
+ <h4><%= class_name.humanize %> - #<%%= <%= instance_var %>.id %></h4>
2
+ <p>Pull out your list view attributes and put them here: /app/views/<%= symbol_name %>/<%= file_name %>.html.erb</p>
@@ -0,0 +1,4 @@
1
+ <h1>Destroy This Record?</h1>
2
+ <%%= form_tag <%= instance_var %>, resource(<%= resource_sym %>, <%= instance_var %>, :destroy) do %>
3
+ <%%= submit 'Delete Record' %>
4
+ <%% end =%>
@@ -0,0 +1,6 @@
1
+ <h1>Edit <%= class_name %></h1>
2
+
3
+ <%%= form_tag <%= instance_var %>, resource(<%= resources_sym %>, <%= instance_var %>) do %>
4
+ <%%= partial :form %>
5
+ <%%= submit 'Save' %>
6
+ <%% end =%>
@@ -0,0 +1,7 @@
1
+ <h1><%= class_name.pluralize.humanize %></h1>
2
+
3
+ <ul>
4
+ <%% for <%= instance_var %> in @<%= plural_name %> -%>
5
+ <li><%%= partial :list_view %></li>
6
+ <%% end -%>
7
+ </ul>
@@ -0,0 +1,16 @@
1
+ <h1>New <%= class_name %></h1>
2
+
3
+ <%%= form_tag <%= instance_var %>, resource(<%= resource_sym %>, :new) do %>
4
+ <%%= partial :form %>
5
+ <p>Complexify your forms by adding a field_set:
6
+ <pre>
7
+ &lt;% @resource_relationship = <%= instance_var %>.resource_relationship -%&gt;
8
+ &lt;%= fields_for @resource_relationship do %&gt;
9
+ &lt;%= hidden_field :<%= singular_name %>_id, :value => <%= instance_var %>.id %&gt;
10
+ &lt;%= partial 'resource_relationship/form' %&gt;
11
+ &lt;% end =%&gt;
12
+ </pre>
13
+ </p>
14
+ <%%= submit 'Create' %>
15
+ <%% end =%>
16
+
@@ -0,0 +1,3 @@
1
+ <h1>Show <%= class_name.humanize %></h1>
2
+
3
+ <%%= partial :full_view %>
data/lib/rear_views.rb ADDED
@@ -0,0 +1,12 @@
1
+ # make sure we're running inside Merb
2
+ if defined?(Merb::Plugins)
3
+
4
+ Merb::BootLoader.before_app_loads do
5
+ end
6
+
7
+ Merb::BootLoader.after_app_loads do
8
+ end
9
+
10
+ generators = File.join(File.dirname(__FILE__), 'generators')
11
+ Merb.add_generators( generators / 'rear_views_generator')
12
+ end
data/spec/console ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This script was inspired by one written by Erik Kastner,
4
+ # and a blog describing it's needs and usage can be found at
5
+ # http://metaatem.net/2007/04/20/fun-with-active_record-and-sqlite3
6
+ #
7
+ # It can be used for hands on quick and dirty testing to see how the library
8
+ # is working in the IRB console. It will open an IRB console with
9
+ # with a Templater::Generator object instantiated: @generator.
10
+ # You can render a template in the console to see what is going on.
11
+ # @user_gen.invoke!
12
+ #
13
+ # This file must be executable to work: [sudo] chmod 775 spec/console
14
+ #
15
+
16
+ require 'rubygems'
17
+ require 'merb-core'
18
+ require 'merb-gen'
19
+ require 'templater/spec/helpers'
20
+ require 'activerecord'
21
+ require 'irb'
22
+
23
+ Merb.disable(:initfile)
24
+
25
+ require File.dirname(__FILE__) + '/../lib/generators/rear_views_generator'
26
+ require File.dirname(__FILE__) + "/database_setup"
27
+ include DatabaseSetup
28
+
29
+
30
+ @user_gen = Merb::Generators::RearViewsGenerator.new( 'spec/tmp', {}, 'User' )
31
+ @project_gen = Merb::Generators::RearViewsGenerator.new( 'spec/tmp', {}, 'Project' )
32
+ @task_gen = Merb::Generators::RearViewsGenerator.new( 'spec/tmp', {}, 'Task' )
33
+
34
+
35
+ # start the conole with these variables
36
+ IRB.start if __FILE__ == $0
@@ -0,0 +1,52 @@
1
+ module DatabaseSetup
2
+ database_name = 'rear_views.db'
3
+
4
+ # remove the old database
5
+ File.delete(database_name) if File.file?(database_name)
6
+
7
+ # grab the files
8
+ $TESTING = true unless defined?($TESTING) && $TESTING == true
9
+ require File.dirname(__FILE__) + '/../lib/generators/rear_views_generator'
10
+
11
+ # establish a connection to the sqlite database
12
+ ActiveRecord::Base.establish_connection(
13
+ :adapter => 'sqlite3',
14
+ :database => database_name
15
+ )
16
+
17
+ # define a schema
18
+ ActiveRecord::Schema.define do
19
+ create_table :users, :force => true do |t|
20
+ t.column :username, :string
21
+ t.column :hobbies, :text
22
+ t.column :birthday, :date
23
+ t.column :admin, :boolean
24
+ end
25
+
26
+ create_table :project, :force => true do |t|
27
+ t.column :user_id, :integer
28
+ t.column :name, :string
29
+ t.column :description, :text
30
+ end
31
+
32
+ create_table :tasks, :force => true do |t|
33
+ t.column :project_id, :integer
34
+ t.column :name, :string
35
+ t.column :description, :text
36
+ end
37
+ end
38
+
39
+ # Related classes
40
+ class User < ActiveRecord::Base
41
+ has_many :projects
42
+ end
43
+
44
+ class Project < ActiveRecord::Base
45
+ belongs_to :user
46
+ has_many :tasks
47
+ end
48
+
49
+ class Taks < ActiveRecord::Base
50
+ belongs_to :project
51
+ end
52
+ end
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'rubygems'
3
+ require 'merb-core'
4
+ require 'merb-gen'
5
+ require 'templater/spec/helpers'
6
+ require 'activerecord'
7
+
8
+ Merb.disable(:initfile)
9
+ Spec::Runner.configure do |config|
10
+ config.include Templater::Spec::Helpers
11
+ end
12
+
13
+ require File.dirname(__FILE__) + '/../lib/generators/rear_views_generator'
14
+ require File.dirname(__FILE__) + "/database_setup"
15
+ include DatabaseSetup
16
+
17
+ describe "rear_views" do
18
+ before(:all) do
19
+ @path = "tmp"
20
+ end
21
+
22
+
23
+ describe 'test setup' do
24
+ it "should have all dependencies and rake tasks needed to run" do
25
+ true.should == true
26
+ end
27
+
28
+ it 'should be able to instantiate a RearViewGenerator' do
29
+ lambda do
30
+ Merb::Generators::RearViewsGenerator.new( @path, {}, 'Test' )
31
+ end.should_not raise_error
32
+ end
33
+ end
34
+
35
+ describe 'inclusion in merb-gen' do
36
+
37
+
38
+ end
39
+
40
+ describe 'file generation' do
41
+ before(:all) do
42
+ @generator = Merb::Generators::RearViewsGenerator.new( 'spec/tmp', {}, 'User' )
43
+ end
44
+
45
+ it "should create views when invoke! is called" do
46
+ @generator.invoke!
47
+ File.exists?("tmp/app/views/users/_form.html.erb")
48
+ File.exists?("tmp/app/views/users/_full_view.html.erb")
49
+ File.exists?("tmp/app/views/users/_list_view.html.erb")
50
+ File.exists?("tmp/app/views/users/delete.html.erb")
51
+ File.exists?("tmp/app/views/users/edit.html.erb")
52
+ File.exists?("tmp/app/views/users/index.html.erb")
53
+ File.exists?("tmp/app/views/users/new.html.erb")
54
+ File.exists?("tmp/app/views/users/show.html.erb")
55
+ end
56
+
57
+ end
58
+
59
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
@@ -0,0 +1 @@
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
@@ -0,0 +1 @@
1
+ yup! it works!
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rughetto-rear_views
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rue The Ghetto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-21 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: merb-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Generators REST views for a resource, so that complex views and forms can be easily built. Currently only AR introspection and ERB templates.
25
+ email: ru_ghetto@rubyghetto.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - LICENSE
36
+ - README
37
+ - Rakefile
38
+ - TODO
39
+ - Generators
40
+ - lib/generators
41
+ - lib/generators/rear_views_generator.rb
42
+ - lib/generators/templates
43
+ - lib/generators/templates/%file_name%
44
+ - lib/generators/templates/%file_name%/_form.html.erb
45
+ - lib/generators/templates/%file_name%/_full_view.html.erb
46
+ - lib/generators/templates/%file_name%/_list_view.html.erb
47
+ - lib/generators/templates/%file_name%/delete.html.erb
48
+ - lib/generators/templates/%file_name%/edit.html.erb
49
+ - lib/generators/templates/%file_name%/index.html.erb
50
+ - lib/generators/templates/%file_name%/new.html.erb
51
+ - lib/generators/templates/%file_name%/show.html.erb
52
+ - lib/rear_views.rb
53
+ - spec/console
54
+ - spec/database_setup.rb
55
+ - spec/rear_views_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ - spec/test_templates
59
+ - spec/test_templates/test.html.erb
60
+ - spec/tmp
61
+ - spec/tmp/app
62
+ - spec/tmp/app/views
63
+ - spec/tmp/app/views/user
64
+ - spec/tmp/app/views/user/_form.html.erb
65
+ - spec/tmp/app/views/user/_full_view.html.erb
66
+ - spec/tmp/app/views/user/_list_view.html.erb
67
+ - spec/tmp/app/views/user/delete.html.erb
68
+ - spec/tmp/app/views/user/edit.html.erb
69
+ - spec/tmp/app/views/user/index.html.erb
70
+ - spec/tmp/app/views/user/new.html.erb
71
+ - spec/tmp/app/views/user/show.html.erb
72
+ has_rdoc: true
73
+ homepage: http://github.com/rughetto/rear_views
74
+ post_install_message:
75
+ rdoc_options: []
76
+
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ requirements: []
92
+
93
+ rubyforge_project: merb
94
+ rubygems_version: 1.2.0
95
+ signing_key:
96
+ specification_version: 2
97
+ summary: Generators REST views for a resource, so that complex views and forms can be easily built. Currently only AR introspection and ERB templates.
98
+ test_files: []
99
+