first-floor 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :build:
5
+ :patch: 2
@@ -0,0 +1,118 @@
1
+ class ActionController::FirstFloor < ActionController::Base
2
+ def index
3
+ obj_class = controller_name.to_s.singularize.titleize
4
+ view_obj = controller_name.to_s
5
+ eval( "@#{view_obj} = obj_class.constantize.find(:all)" )
6
+ render_response( eval( "@#{view_obj}" ), 'index' )
7
+ end
8
+
9
+ def show
10
+ obj_class = controller_name.to_s.singularize.titleize
11
+ view_obj = controller_name.to_s.singularize
12
+ eval( "@#{view_obj} = obj_class.constantize.find(params[:id])" )
13
+ render_response( eval( "@#{view_obj}" ) , 'show' )
14
+ end
15
+
16
+ def new
17
+ obj_class = controller_name.to_s.singularize.titleize
18
+ view_obj = controller_name.to_s.singularize
19
+ eval( "@#{view_obj} = obj_class.constantize.new" )
20
+ end
21
+
22
+ def create
23
+ obj_class = controller_name.to_s.singularize.titleize
24
+ obj_name = controller_name.to_s.singularize
25
+ @obj = obj_class.constantize.new(params[obj_name.to_sym])
26
+ if @obj.save
27
+ flash[:notice] = "#{obj_class} was successfully created"
28
+ flash[:status] = 201
29
+ redirect_to :action => :index
30
+ else
31
+ eval( "@#{obj_name} = @obj" )
32
+
33
+ flash[:notice] = "Cannot create this #{obj_class}. There were some errors."
34
+ flash[:status] = 400
35
+ render_response( eval( "@#{obj_name}" ), 'new', 400 )
36
+ end
37
+ end
38
+
39
+ def edit
40
+ obj_class = controller_name.to_s.singularize.titleize
41
+ view_obj = controller_name.to_s.singularize
42
+
43
+ eval( "@#{view_obj} = obj_class.constantize.find(params[:id])" )
44
+ end
45
+
46
+ def update
47
+ obj_class = controller_name.to_s.singularize.titleize
48
+ obj_name = controller_name.to_s.singularize
49
+ @obj = obj_class.constantize.find(params[:id])
50
+
51
+ if @obj.update_attributes( params[obj_name.to_sym] )
52
+ check_validation(@obj)
53
+
54
+ flash[:notice] = "#{obj_class} was successfully updated."
55
+ flash[:status] = 200
56
+ eval( "@#{obj_name} = @obj" )
57
+ redirect_to :action => :show, :id => eval( "@#{obj_name}" )
58
+ else
59
+ eval( "@#{obj_name} = @obj" )
60
+
61
+ flash[:notice] = "#{obj_class} had some errors and was not updated."
62
+ flash[:status] = 400
63
+ render_response( eval( "@#{obj_name}" ), 'edit', 400 )
64
+ end
65
+ end
66
+
67
+ def destroy
68
+ obj_class = controller_name.to_s.singularize.titleize
69
+ obj = obj_class.constantize.find(params[:id])
70
+ obj_info = nil
71
+
72
+ if obj.respond_to?(:name)
73
+ obj_info = "#{obj.id}:#{obj.name}"
74
+ else
75
+ obj_info = "#{obj.id}:#{obj.to_s}"
76
+ end
77
+ obj_class.constantize.delete(params[:id])
78
+ flash[:notice] = "#{obj_class} '#{obj_info} successfully deleted.'"
79
+ redirect_to :action => 'index'
80
+ end
81
+
82
+ def render_response(obj, tmpl, status = :ok)
83
+ klass = nil
84
+ if obj.class == Array
85
+ klass = obj[0].class
86
+
87
+ ## Make sure we're not dealing with a NilClass, which we don't have
88
+ ## a template for. Default to the controller class.
89
+ if klass == NilClass
90
+ klass = controller_name.to_s.singularize.titleize
91
+ end
92
+ else
93
+ klass = obj.class
94
+ end
95
+
96
+ namespace = klass.to_s.downcase.pluralize
97
+
98
+ if /^Admin/.match( self.class.to_s )
99
+ namespace = "admin/#{namespace}"
100
+ end
101
+
102
+ respond_to do |format|
103
+ format.html { render :template => "#{namespace}/#{tmpl}.html.erb",
104
+ :status => status }
105
+ format.xml { render :xml => obj.to_xml, :status => status }
106
+ format.json { render :json => obj.to_json, :status => status }
107
+ format.yaml { render :text => obj.to_yaml, :status => status }
108
+ end
109
+ end
110
+
111
+ protected
112
+ def check_validation(obj)
113
+ unless obj.valid?
114
+ raise ActiveRecord::ActiveRecordError, "This record is invalid."
115
+ end
116
+ end
117
+
118
+ end
@@ -0,0 +1,7 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class FirstFloorTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'first_floor'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: first-floor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Nathan L. Walls
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-22 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A gem to DRY out controller code. It lives just a bit above 'ActionController::Base'
17
+ email: nathan@rexluther.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/action_controller/first_floor.rb
27
+ - test/first_floor_test.rb
28
+ - test/test_helper.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/base10/first-floor
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: A little bit above ActionController::Base
58
+ test_files: []
59
+