rails_config_model_generator 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,14 @@
1
+ == 1.1.0 / 2007-07-16
2
+
3
+ * Specify model name and migration fields via generator
4
+ * Updated tests
5
+ * fixed syntax error in the conroller template
6
+ * Rewrote the generator
7
+ * Refactored configuration creation on load
8
+ * updated tests
9
+
10
+ == 1.0.0 / 2007-07-16
11
+
12
+ * Initial release - generating configuration model
13
+ * Birthday!
14
+
data/Manifest.txt ADDED
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/rails_config_model.rb
6
+ rails_config_model_generator.rb
7
+ templates/configuration.rb
8
+ templates/configuration_controller.rb
9
+ templates/edit.rhtml
10
+ templates/fixtures.yml
11
+ templates/functional_test.rb
12
+ templates/migration.rb
13
+ templates/unit_test.rb
14
+
data/README.txt ADDED
@@ -0,0 +1,91 @@
1
+ == rails_config_model
2
+
3
+ by Brian Hogan
4
+ http://rconfig.rubyforge.org/
5
+ http://www.napcs.com/products/rails/rails_config_model_generator
6
+
7
+ == DESCRIPTION:
8
+ Creates a configuration controller and model that can be used to quickly create configuration table for your system so you can store system-wide variables that you'd like the user to be able to set.
9
+
10
+ This gem contains a generator to create a simple configuration model, migration, and interface for your application, complete with working tests.
11
+
12
+ == FEATURES
13
+
14
+ * Generates the controller, model, and the associated files.
15
+ * You can specify the model name and set the fields for the migrations via the generator.
16
+
17
+ == SYNOPSIS:
18
+
19
+ === Setup and overview
20
+ Generate a new configuration system for your application by executing the generator from the root of your application.
21
+
22
+ ruby script\generate rails_config_model Configuration
23
+
24
+ You can also specify the model fields much like the scaffold_resource generator
25
+
26
+ ruby script/generate rails_config_model Configuration contact_email:string site_name:string welcome_message:text max_number_of_events:integer
27
+
28
+ Once installed, you modify the generated migration to include the fields you want to configure. There are a few defaults there to give you an idea.
29
+
30
+ The generator will create a controller mounted at /configuration so you can edit your configurations. Modify this as needed to provide for security.
31
+
32
+ === The Edit form
33
+ The application's edit form uses the *form* helper method to auto-generate the fields. This may not be optimal and you may wish to actually write your own view instead. See app/views/configuration/edit.rhtml for more details.
34
+
35
+ === Usage
36
+ Configuration is simply a model for this table. It is designed to handle a single row of a table, and so additional rows cannot be created.
37
+ If you have a table that looks like this:
38
+
39
+ id
40
+ contact_email
41
+ site_name
42
+ welcome_message
43
+ max_number_of_events
44
+
45
+ You simply grab the row from the table
46
+
47
+ @configuration = Configuration.load
48
+
49
+ and then grab the values out.
50
+
51
+ email = @configuration.contact_email
52
+
53
+ Or save new values
54
+
55
+ @configuration = Configuration.load
56
+ @configuration.welcome_message = "This is the default message."
57
+ @configuraiton.save
58
+
59
+
60
+ == REQUIREMENTS:
61
+
62
+ * Rails, a database, and a reason to use this.
63
+
64
+ == INSTALL:
65
+
66
+ Download and sudo gem install rails_config_model
67
+
68
+ == LICENSE:
69
+
70
+ (The MIT License)
71
+
72
+ Copyright (c) 2007 Brian P. Hogan
73
+
74
+ Permission is hereby granted, free of charge, to any person obtaining
75
+ a copy of this software and associated documentation files (the
76
+ 'Software'), to deal in the Software without restriction, including
77
+ without limitation the rights to use, copy, modify, merge, publish,
78
+ distribute, sublicense, and/or sell copies of the Software, and to
79
+ permit persons to whom the Software is furnished to do so, subject to
80
+ the following conditions:
81
+
82
+ The above copyright notice and this permission notice shall be
83
+ included in all copies or substantial portions of the Software.
84
+
85
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
88
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
89
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
90
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
91
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/rails_config_model.rb'
6
+
7
+
8
+ class Hoe
9
+ def extra_deps
10
+ @extra_deps.reject do |x|
11
+ Array(x).first == 'hoe'
12
+ end
13
+ end
14
+ end
15
+
16
+ Hoe.new('rails_config_model_generator', RailsConfigModel::VERSION) do |p|
17
+ p.rubyforge_name = 'rconfig'
18
+ p.name = "rails_config_model_generator"
19
+ p.author = ["Brian Hogan"]
20
+ #p.remote_rdoc_dir = ''
21
+ # p.summary = 'FIX'
22
+ p.description = p.paragraphs_of('README.txt', 2..10).join("\n\n")
23
+ p.url = "http://rconfig.rubyforge.org/"
24
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
25
+ p.remote_rdoc_dir = ""
26
+ end
27
+
28
+ # vim: syntax=Ruby
@@ -0,0 +1,3 @@
1
+ class RailsConfigModel
2
+ VERSION = '1.1.0'
3
+ end
@@ -0,0 +1,103 @@
1
+ # This generator borrows heavily from the scaffold_resource generator in Rails 1.2.3
2
+ class RailsConfigModelGenerator < Rails::Generator::NamedBase
3
+ attr_reader :controller_name,
4
+ :controller_class_path,
5
+ :controller_file_path,
6
+ :controller_class_nesting,
7
+ :controller_class_nesting_depth,
8
+ :controller_class_name,
9
+ :controller_singular_name,
10
+ :controller_plural_name
11
+ alias_method :controller_file_name, :controller_singular_name
12
+ alias_method :controller_table_name, :controller_plural_name
13
+
14
+ def initialize(runtime_args, runtime_options = {})
15
+ super
16
+
17
+ @controller_name = @name
18
+
19
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
20
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
21
+
22
+ if @controller_class_nesting.empty?
23
+ @controller_class_name = @controller_class_name_without_nesting
24
+ else
25
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
26
+ end
27
+ end
28
+
29
+
30
+
31
+
32
+ def manifest
33
+ recorded_session = record do |m|
34
+
35
+ m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
36
+ m.class_collisions(class_path, "#{class_name}")
37
+
38
+
39
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
40
+
41
+ m.template('configuration.rb', File.join('app/models', class_path, "#{file_name}.rb"))
42
+
43
+ m.template(
44
+ 'configuration_controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
45
+ )
46
+
47
+ m.template("edit.rhtml", File.join("app/views", controller_class_path, controller_file_name, "edit.rhtml"))
48
+ m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
49
+ m.template('unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb"))
50
+ m.template('fixtures.yml', File.join('test/fixtures', "#{table_name}.yml"))
51
+
52
+ unless options[:skip_migration]
53
+ m.migration_template(
54
+ 'migration.rb', 'db/migrate',
55
+ :assigns => {
56
+ :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
57
+ :attributes => attributes
58
+ },
59
+ :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
60
+ )
61
+ end
62
+
63
+
64
+
65
+ end
66
+
67
+ action = nil
68
+ action = $0.split("/")[2]
69
+
70
+
71
+ case action
72
+ when "generate"
73
+ puts
74
+ puts ("-" * 70)
75
+ puts "Your app's configuration is all ready to go! There's just a couple things you might want to do:"
76
+ puts
77
+ puts " 1. Modify db/migrate/create_#{file_path.gsub(/\//, '_').pluralize} and add any additional columns."
78
+ puts " 2. Open up #{File.join('app/models', class_path, "#{file_name}.rb")} and add any custom validations."
79
+ puts " 3. Migrate your database (rake db:migrate) and then run your unit tests to make sure this generator didn't break anything!"
80
+ puts " 4. Modify #{File.join("app/views", controller_class_path, controller_file_name, "edit.rhtml")} and customize it for your application."
81
+ puts
82
+ puts ("-" * 70)
83
+ puts
84
+ when "destroy"
85
+ puts
86
+ puts ("-" * 70)
87
+ puts
88
+ puts "Thanks for using the rails_config_model generator. If you added any additional files that depend on this, be sure to remove them!"
89
+ puts
90
+ puts ("-" * 70)
91
+ puts
92
+ else
93
+ puts
94
+ end
95
+ recorded_session
96
+ end
97
+
98
+ protected
99
+ # Override with your own usage banner.
100
+ def banner
101
+ "Run this to create the files you need for a configuration system. Usage: rails_config_model ModelName [field:type, field:type]"
102
+ end
103
+ end
@@ -0,0 +1,34 @@
1
+ # The <%=class_name %> model acts as the interface to the <%=table_name %> table.
2
+ # The <%=table_name %> table should consist of only one row with one column for each configuration field in your system.
3
+ #
4
+ # Callbacks prevent rows from being added to or removed from the table.
5
+ #
6
+ class <%=class_name %> < ActiveRecord::Base
7
+
8
+ before_create :check_for_existing
9
+ before_destroy :check_for_existing
10
+
11
+ # class methods
12
+
13
+ # Returns the system configuration record. You should use this instead of doing an explicit #find on this object, as this
14
+ # method will retrieve only the first row from the table.
15
+ #
16
+ # If no configuration record exists, one will be created with blank fields.
17
+ def self.load
18
+
19
+
20
+ <%=singular_name %> = <%=class_name %>.find :first
21
+ if <%=singular_name %>.nil?
22
+ <%=singular_name %> = <%=class_name %>.create()
23
+ end
24
+ <%=singular_name %>
25
+ end
26
+
27
+
28
+ protected
29
+
30
+ # Prevents the destruction or creation of more than one record.
31
+ def check_for_existing
32
+ return false if <%=class_name %>.find(:all).size >= 1
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ # This configuration controller was generated by the rails_configu_model generator
2
+ #
3
+ # This controller provides an interface to quickly edit the values in the <%=table_name %> table.
4
+ # See <%=class_name %> for more details on the system configuration.
5
+ #
6
+
7
+ class <%=controller_class_name %>Controller < ApplicationController
8
+
9
+ before_filter :find_<%=singular_name %>
10
+
11
+
12
+ # Simply redirect to the edit action
13
+ def index
14
+ edit
15
+ render :action=> "edit"
16
+ end
17
+
18
+
19
+ # Shows the page to edit the <%=singular_name %> variables.
20
+ def edit
21
+ end
22
+
23
+
24
+ # Saves the posted data to the <%=table_name %> table via the standard
25
+ # Rails mass-assignment update approach.
26
+ def update
27
+
28
+ if @<%=singular_name %>.update_attributes(params[:<%=singular_name %>])
29
+ flash[:notice] = 'Your system configuration was successfully updated.'
30
+ redirect_to :action => 'edit'
31
+ else
32
+ render :action=>"edit"
33
+ end
34
+ end
35
+
36
+
37
+ protected
38
+
39
+ # Before filter to load the configuration data.
40
+ def find_<%=singular_name %>
41
+ @<%=singular_name %> = <%=class_name %>.load
42
+ end
43
+
44
+ end
@@ -0,0 +1,5 @@
1
+ <h2>System configuration</h2>
2
+ <hr />
3
+ <%%= error_messages_for "<%= singular_name %>" %>
4
+ <%%= form("<%= singular_name %>", :action => "update") %>
5
+
@@ -0,0 +1,6 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ one:
3
+ id: 1
4
+ <% for attribute in attributes -%>
5
+ <%= attribute.name %>: <%= attribute.default %>
6
+ <% end -%>
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%=controller_file_name %>_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class <%=controller_class_name %>Controller; def rescue_action(e) raise e end; end
6
+
7
+ class <%=controller_class_name %>ControllerTest < Test::Unit::TestCase
8
+
9
+ fixtures :<%=table_name %>
10
+
11
+ def setup
12
+ @controller = <%=controller_class_name %>Controller.new
13
+ @request = ActionController::TestRequest.new
14
+ @response = ActionController::TestResponse.new
15
+ end
16
+
17
+ def test_index
18
+ get "index"
19
+ assert_response :success
20
+ assert_template "edit"
21
+
22
+ end
23
+
24
+
25
+ def test_edit
26
+ get "edit"
27
+ assert_response :success
28
+ assert_template "edit"
29
+ end
30
+
31
+ def test_update
32
+ post "update", {:id=>1}
33
+ assert_response :redirect
34
+ end
35
+
36
+ def test_create_new_<%=singular_name %>_when_none_exists
37
+ <%=class_name %>.delete_all
38
+ get "edit"
39
+ assert_response :success
40
+ assert_template "edit"
41
+ end
42
+
43
+ end
@@ -0,0 +1,13 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ <% for attribute in attributes -%>
5
+ t.column :<%= attribute.name %>, :<%= attribute.type %>
6
+ <% end -%>
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :<%= table_name %>
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class <%=class_name %>Test < Test::Unit::TestCase
4
+
5
+ fixtures :<%=table_name %>
6
+
7
+ # The assumption here is that the record was created already.
8
+ # Once there is a record, it should not allow the creation of an existing one.
9
+ def test_should_not_create
10
+ c = <%=class_name %>.create({})
11
+ assert !c.save
12
+ end
13
+
14
+ # Should not be able to remove the last configuration in the system.
15
+ def test_should_not_delete
16
+ c = <%=class_name %>.find :first
17
+ assert !c.destroy
18
+ end
19
+
20
+ def test_load_method_should_only_find_one
21
+ assert_kind_of(<%=class_name %>, <%=class_name %>.load)
22
+ end
23
+
24
+ def test_should_create_new_record_on_load_if_none_found
25
+ <%=class_name %>.delete_all
26
+ assert_not_nil <%=class_name %>.load
27
+ end
28
+
29
+
30
+
31
+ end
File without changes
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: rails_config_model_generator
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.1.0
7
+ date: 2007-10-25 00:00:00 -05:00
8
+ summary: The author was too lazy to write a summary
9
+ require_paths:
10
+ - lib
11
+ email: ryand-ruby@zenspider.com
12
+ homepage: http://rconfig.rubyforge.org/
13
+ rubyforge_project: rconfig
14
+ description: "== DESCRIPTION: Creates a configuration controller and model that can be used to quickly create configuration table for your system so you can store system-wide variables that you'd like the user to be able to set. This gem contains a generator to create a simple configuration model, migration, and interface for your application, complete with working tests. == FEATURES * Generates the controller, model, and the associated files. * You can specify the model name and set the fields for the migrations via the generator. == SYNOPSIS: === Setup and overview Generate a new configuration system for your application by executing the generator from the root of your application. ruby script\\generate rails_config_model Configuration You can also specify the model fields much like the scaffold_resource generator ruby script/generate rails_config_model Configuration contact_email:string site_name:string welcome_message:text max_number_of_events:integer Once installed, you modify the generated migration to include the fields you want to configure. There are a few defaults there to give you an idea. The generator will create a controller mounted at /configuration so you can edit your configurations. Modify this as needed to provide for security. === The Edit form The application's edit form uses the *form* helper method to auto-generate the fields. This may not be optimal and you may wish to actually write your own view instead. See app/views/configuration/edit.rhtml for more details. === Usage Configuration is simply a model for this table. It is designed to handle a single row of a table, and so additional rows cannot be created. If you have a table that looks like this: id contact_email site_name welcome_message max_number_of_events You simply grab the row from the table @configuration = Configuration.load and then grab the values out. email = @configuration.contact_email Or save new values @configuration = Configuration.load @configuration.welcome_message = \"This is the default message.\" @configuraiton.save"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Brian Hogan
31
+ files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ - Rakefile
36
+ - lib/rails_config_model.rb
37
+ - rails_config_model_generator.rb
38
+ - templates/configuration.rb
39
+ - templates/configuration_controller.rb
40
+ - templates/edit.rhtml
41
+ - templates/fixtures.yml
42
+ - templates/functional_test.rb
43
+ - templates/migration.rb
44
+ - templates/unit_test.rb
45
+ test_files:
46
+ - test/test_rails_config_model.rb
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ extra_rdoc_files:
51
+ - History.txt
52
+ - Manifest.txt
53
+ - README.txt
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies: []
61
+