balinterdi-acts_as_trivia 0.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1 +1,2 @@
1
- v0.1 Initial release.
1
+ v0.1.3 acts_as_trivia generator does not automatically create a trivia instance. There is acts_as_trivia_record for that.
2
+ v0.1 Initial release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Balint Erdi (balint@bucionrails.com)
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/Manifest CHANGED
@@ -1,3 +1,4 @@
1
+ acts_as_trivia.gemspec
1
2
  CHANGELOG
2
3
  generators/acts_as_trivia/acts_as_trivia_generator.rb
3
4
  generators/acts_as_trivia/templates/answers_controller.rb
@@ -8,12 +9,14 @@ generators/acts_as_trivia/USAGE
8
9
  generators/acts_as_trivia_record/acts_as_trivia_record_generator.rb
9
10
  generators/acts_as_trivia_record/templates/migration.rb
10
11
  generators/acts_as_trivia_record/USAGE
12
+ generators/lib/acts_as_trivia_commands.rb
11
13
  lib/acts_as_trivia/trivia.rb
12
14
  lib/acts_as_trivia/trivia_answer.rb
13
15
  lib/acts_as_trivia/trivia_answers_controller.rb
14
16
  lib/acts_as_trivia/trivias_helper.rb
15
17
  lib/acts_as_trivia/user.rb
16
18
  lib/acts_as_trivia.rb
19
+ LICENSE
17
20
  Manifest
18
21
  Rakefile
19
22
  README.markdown
@@ -1,97 +1,62 @@
1
- * NoMethodError (undefined method `acts\_as\_trivia' for #<Class:0x253e9fc>):
1
+ # Acts As Trivia
2
2
 
3
- Make sure you have the config.gem "acts\_as\_trivia" line in your environment.rb
3
+ This gem adds a full-fletched trivia game to your Rails application. It only needs a (or more) model class(es) to operate on (the instances of which can be the subjects of the trivia) and an attribute of this class which is comparable.
4
4
 
5
- * Trivia will have its own model:
6
-
7
- create_table :trivias, :force => true do |t|
8
- t.string :on, :null => false
9
- t.string :about, :null => false
10
- end
11
-
12
- e.g
13
-
14
- Trivia.new(:on => "country", :about => "hdi")
15
-
16
- class Trivia < ActionRecord::Base
17
-
18
- def to\_param
19
- #{on}-#{about} # e.g country-hdi
20
- end
5
+ ## Installation
21
6
 
22
- And requests to trivias will have a routing like this:
23
-
24
- /trivias/1, or which is the same
25
- /trivias/country-hdi (and that latter will be generated)
26
-
7
+ Put the following line in your environment.rb:
27
8
 
28
- * The User class will have the following added associations:
29
-
30
- class User < ActiveRecord::Base
31
-
32
- has_many :trivia_answers
33
- has_many :trivias, :through => :trivia_answers
34
- has_many :correct_trivia_answers, :through => :trivia_answers, :condition => { "state = ?", 'correct' }
9
+ config.gem "balinterdi-acts_as_trivia", :source => http://gems.github.com, :lib => "acts_as_trivia"
35
10
 
36
- * And the join model will be the TriviaAnswers class:
11
+ And then install it systemwide:
37
12
 
38
- create_table :trivia_answers, :force => true do |t|
39
- t.string :trivia_id, :null => false
40
- t.string :user_id, :null => false
41
- t.integer :points # how many points the user received for his answer
42
- end
13
+ rake gems:install
14
+
15
+ Or bundled with your app:
43
16
 
44
- * when the geneation script is called with the following:
17
+ rake gems:unpack
45
18
 
46
- ./script/generate acts_as_trivia country hdi
19
+ On Rails < 2.1 you can install it like this, too:
47
20
 
48
- then the following things will be generated:
21
+ gem install balinterdi-acts_as_trivia --source http://gems.github.com
22
+
23
+ However, I have run into some issues this way so I recommend using the config.gem way.
49
24
 
50
- * a new trivia model OK
25
+ ### Make your Rails app a trivia game
51
26
 
52
- class Trivia < ActiveRecord::Base
53
- def to\_param
54
- #{on}-#{about} # e.g country-hdi
55
- end
56
- end
27
+ Once you have the gem installed you should run the **acts\_as\_trivia** generator:
57
28
 
58
- * a new controller (if it does not exist yet) OK
29
+ ./script/generate acts_as_trivia
59
30
 
60
- class TriviaController
61
- end
31
+ This will "trivialize" your application, creating migrations for the new trivia-related classes, stub out the empty controllers and generate routes for them. Use _rake db:migrate_ to create the trivia-related classes in the database.
62
32
 
63
- * new resource routes for trivia OK
33
+ Now you can start adding trivia with the help of the **acts\_as\_trivia\_record** generator:
64
34
 
65
- map.resources :trivias
35
+ ./script/generate acts_as_trivia_record country area name
36
+
37
+ This generator will add an acts\_as\_trivia call into your Country class (acts_as_trivia :area) and create migration for the actual trivia instance (_Trivia.create(:on => "country", :about => "area", :displayed => "name")_). To have the new trivia available in your app, don't forget to _rake db:migrate_
66
38
 
67
- * in the model (country.rb) OK:
39
+ Create the TriviaAnswers _new_ and _create_ actions and some simple views for them. A quick way is to use the ones provided [in the rails application template](http://gist.github.com/raw/107361/f31caad451f0cca699288700aa3d98291a259fd1/gistfile1.rb).
40
+ (note: I am planning to modify the template so it can be applied just to create these actions and views)
68
41
 
69
- class Country < ActiveRecord::Base
70
- acts_as_trivia :hdi
71
- ...
72
- end
42
+ Now go to
73
43
 
74
- * added associations in the user model OK
44
+ /users/:user_id/trivias/1/trivia_answers/new
45
+
46
+ to test your knowledge!
75
47
 
76
- has_many :trivia_answers
77
- has_many :trivias, :through => :trivia_answers
78
- <!-- has_many :correct_trivia_answers, :through => :trivia_answers, :condition => { "state = ?", 'correct' } -->
48
+ ### Rails application template
79
49
 
80
- * db migrations
50
+ If you want a "quick-start" course or got to like the gem so much you would like to have it in your app from start, fear not. I assembled an application template just for this task (this will work starting from Rails 2.3):
81
51
 
82
- * for the trivia class itself OK
52
+ rails my_shiny_new_app -m http://gist.github.com/raw/107361/f31caad451f0cca699288700aa3d98291a259fd1/gistfile1.rb
53
+
54
+ ## How does it work
83
55
 
84
- create_table :trivias, :force => true do |t|
85
- t.string :on, :null => false
86
- t.string :about, :null => false
87
- t.timestamps
88
- end
56
+ Acts\_as\_trivia uses instances of the Trivia and TriviaAnswer classes to assess the knowledge of users. It only supposes that you have a User class of which the instances are the users of your application.
89
57
 
90
- * for the join model between the user and the trivia OK
58
+ It tries to mingle with your application as little as possible and provide a clean interface to work with. (if you feel this is not so, please let me know) It adds a couple of helper methods you can use in your views to create a trivia answer (trivia\_user\_panel and trivia_dropdown) and to show the correct answer (with\_each\_solution\_value).
91
59
 
92
- create_table :trivia_answers, :force => true do |t|
93
- t.string :trivia_id, :null => false
94
- t.string :user_id, :null => false
95
- t.integer :points # how many points the user received for his answer
96
- end
60
+ ## Disclaimer
97
61
 
62
+ Please consider this an early release and thus count on some bumps on the way. If you find one, let me know (<balint@bucionrails.com>).
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('acts_as_trivia', '0.1') do |p|
5
+ Echoe.new('acts_as_trivia', '0.1.3') do |p|
6
6
  p.description = <<-EOS
7
7
  This gem will add the possibility to turn your Rails application into a trivia game with the ease of generating a migration. The only thing you need is a model class with a sortable attribute and this gem.
8
8
  EOS
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{acts_as_trivia}
5
- s.version = "0.1"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Balint Erdi"]
9
- s.date = %q{2009-05-04}
9
+ s.date = %q{2009-05-06}
10
10
  s.description = %q{This gem will add the possibility to turn your Rails application into a trivia game with the ease of generating a migration. The only thing you need is a model class with a sortable attribute and this gem.}
11
11
  s.email = %q{balint.erdi@gmail.com}
12
- s.extra_rdoc_files = ["CHANGELOG", "lib/acts_as_trivia/trivia.rb", "lib/acts_as_trivia/trivia_answer.rb", "lib/acts_as_trivia/trivia_answers_controller.rb", "lib/acts_as_trivia/trivias_helper.rb", "lib/acts_as_trivia/user.rb", "lib/acts_as_trivia.rb", "README.markdown", "tasks/acts_as_trivia.rake"]
13
- s.files = ["CHANGELOG", "generators/acts_as_trivia/acts_as_trivia_generator.rb", "generators/acts_as_trivia/templates/answers_controller.rb", "generators/acts_as_trivia/templates/controller.rb", "generators/acts_as_trivia/templates/trivias_migration.rb", "generators/acts_as_trivia/templates/view.html.erb", "generators/acts_as_trivia/USAGE", "generators/acts_as_trivia_record/acts_as_trivia_record_generator.rb", "generators/acts_as_trivia_record/templates/migration.rb", "generators/acts_as_trivia_record/USAGE", "lib/acts_as_trivia/trivia.rb", "lib/acts_as_trivia/trivia_answer.rb", "lib/acts_as_trivia/trivia_answers_controller.rb", "lib/acts_as_trivia/trivias_helper.rb", "lib/acts_as_trivia/user.rb", "lib/acts_as_trivia.rb", "Manifest", "Rakefile", "README.markdown", "spec/acts_as_trivia_spec.rb", "spec/database.yml", "spec/spec_helper.rb", "tasks/acts_as_trivia.rake", "todos.markdown", "acts_as_trivia.gemspec"]
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/acts_as_trivia/trivia.rb", "lib/acts_as_trivia/trivia_answer.rb", "lib/acts_as_trivia/trivia_answers_controller.rb", "lib/acts_as_trivia/trivias_helper.rb", "lib/acts_as_trivia/user.rb", "lib/acts_as_trivia.rb", "LICENSE", "README.markdown", "tasks/acts_as_trivia.rake"]
13
+ s.files = ["acts_as_trivia.gemspec", "CHANGELOG", "generators/acts_as_trivia/acts_as_trivia_generator.rb", "generators/acts_as_trivia/templates/answers_controller.rb", "generators/acts_as_trivia/templates/controller.rb", "generators/acts_as_trivia/templates/trivias_migration.rb", "generators/acts_as_trivia/templates/view.html.erb", "generators/acts_as_trivia/USAGE", "generators/acts_as_trivia_record/acts_as_trivia_record_generator.rb", "generators/acts_as_trivia_record/templates/migration.rb", "generators/acts_as_trivia_record/USAGE", "generators/lib/acts_as_trivia_commands.rb", "lib/acts_as_trivia/trivia.rb", "lib/acts_as_trivia/trivia_answer.rb", "lib/acts_as_trivia/trivia_answers_controller.rb", "lib/acts_as_trivia/trivias_helper.rb", "lib/acts_as_trivia/user.rb", "lib/acts_as_trivia.rb", "LICENSE", "Manifest", "Rakefile", "README.markdown", "spec/acts_as_trivia_spec.rb", "spec/database.yml", "spec/spec_helper.rb", "tasks/acts_as_trivia.rake", "todos.markdown"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{http://github.com/balinterdi/acts_as_trivia}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Acts_as_trivia", "--main", "README.markdown"]
@@ -10,13 +10,4 @@ Example:
10
10
  create app/views/country_trivia/population.html.erb
11
11
  create app/views/country_trivia/gdp.html.erb
12
12
  route map.with_options(:controller => 'country_trivia') do ...
13
-
14
- In routes.rb it will add the following:
15
-
16
- ActionController::Routing::Routes.draw do |map|
17
-
18
- map.with_options(:controller => 'country_trivia') do |trivia|
19
- trivia.country_hdi_trivia '/countries/trivia/hdi', :action => "hdi"
20
- trivia.country_hdi_trivia '/countries/trivia/population', :action => "population"
21
- trivia.country_hdi_trivia '/countries/trivia/hdi', :action => "gdp"
22
- end
13
+
@@ -1,75 +1,11 @@
1
1
  require 'rails_generator'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'acts_as_trivia_commands'))
2
3
 
3
- module ActsAsTriviaAddedContent
4
-
5
- def route_trivia_answers_as_nested_resource
6
- sentinel = "ActionController::Routing::Routes.draw do |map|"
7
- unless options[:pretend]
8
- gsub_file "config/routes.rb", /(#{Regexp.escape(sentinel)})/mi do |match|
9
- <<-EOS
10
- #{match}
11
- map.resources :users do |users|
12
- users.resources :trivias do |trivias|
13
- trivias.resources :trivia_answers
14
- end
15
- end
16
-
17
- EOS
18
- end
19
- end
20
-
21
- end
22
-
23
- def model_acts_as_trivia_includes(questions)
24
- acts_as_trivia_calls = questions.map do |question|
25
- "acts_as_trivia :#{question}"
26
- end
27
- logger.modify acts_as_trivia_calls.join(", ")
28
-
29
- sentinel = "class #{class_name} < ActiveRecord::Base"
30
- unless options[:pretend]
31
- gsub_file File.join("app/models", class_path, "#{singular_name}.rb"), /(#{Regexp.escape(sentinel)})/mi do |match|
32
- <<-EOS
33
- #{match}
34
- #{acts_as_trivia_calls.join("\n")}
35
- EOS
36
- end
37
- end
38
- end
39
-
40
- end
41
-
42
- class ActsAsTriviaGenerator < Rails::Generator::NamedBase # ControllerGenerator
4
+ class ActsAsTriviaGenerator < Rails::Generator::Base # ControllerGenerator
43
5
 
44
6
  # this is snatched from the resource generator in the Rails source
45
7
  default_options :skip_timestamps => false, :skip_migration => false
46
8
 
47
- attr_reader :controller_name,
48
- :controller_class_path,
49
- :controller_file_path,
50
- :controller_class_nesting,
51
- :controller_class_nesting_depth,
52
- :controller_class_name,
53
- :controller_singular_name,
54
- :controller_plural_name
55
- alias_method :controller_file_name, :controller_singular_name
56
- alias_method :controller_table_name, :controller_plural_name
57
-
58
- def initialize(runtime_args, runtime_options = {})
59
- super
60
-
61
- @controller_name = @name.pluralize
62
-
63
- base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
64
- @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
65
-
66
- if @controller_class_nesting.empty?
67
- @controller_class_name = @controller_class_name_without_nesting
68
- else
69
- @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
70
- end
71
- end
72
-
73
9
  def manifest
74
10
  record do |m|
75
11
  # Check whether the given class names are already taken by
@@ -90,14 +26,9 @@ class ActsAsTriviaGenerator < Rails::Generator::NamedBase # ControllerGenerator
90
26
  m.route_resources "trivias"
91
27
  m.route_trivia_answers_as_nested_resource
92
28
 
93
- m.model_acts_as_trivia_includes actions
94
-
95
29
  unless options[:skip_migration]
96
30
  m.migration_template 'trivias_migration.rb', 'db/migrate', :assigns => {
97
31
  :migration_name => "CreateTrivias",
98
- :trivia_on => singular_name,
99
- :trivia_about => actions.first,
100
- :trivia_displayed => actions.last,
101
32
  }, :migration_file_name => "create_trivias"
102
33
  end
103
34
 
@@ -107,4 +38,4 @@ class ActsAsTriviaGenerator < Rails::Generator::NamedBase # ControllerGenerator
107
38
  end
108
39
  end
109
40
 
110
- Rails::Generator::Commands::Base.send(:include, ActsAsTriviaAddedContent)
41
+ Rails::Generator::Commands::Base.send(:include, ActsAsTriviaCommands)
@@ -19,7 +19,6 @@ class <%= migration_name %> < ActiveRecord::Migration
19
19
  <% end -%>
20
20
  end
21
21
 
22
- Trivia.create(:on => <%= %("#{trivia_on}") %>, :about => <%= %("#{trivia_about}") %>, :displayed => <%= %("#{trivia_displayed}") %>)
23
22
  end
24
23
 
25
24
  def self.down
@@ -1,6 +1,13 @@
1
+ require 'rails_generator'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'acts_as_trivia_commands'))
3
+
1
4
  class ActsAsTriviaRecordGenerator < Rails::Generator::NamedBase
5
+
2
6
  def manifest
3
7
  record do |m|
8
+ trivia_about = actions.first
9
+ m.model_acts_as_trivia_includes trivia_about
10
+
4
11
  m.migration_template 'migration.rb', 'db/migrate', :assigns => {
5
12
  :migration_name => "CreateTriviaFor#{plural_name.capitalize}#{actions.first.capitalize}",
6
13
  :trivia_on => singular_name,
@@ -9,4 +16,6 @@ class ActsAsTriviaRecordGenerator < Rails::Generator::NamedBase
9
16
  }, :migration_file_name => "create_trivia_for_#{plural_name}_#{actions.first}"
10
17
  end
11
18
  end
12
- end
19
+ end
20
+
21
+ Rails::Generator::Commands::Base.send(:include, ActsAsTriviaCommands)
@@ -0,0 +1,36 @@
1
+ module ActsAsTriviaCommands
2
+
3
+ def route_trivia_answers_as_nested_resource
4
+ sentinel = "ActionController::Routing::Routes.draw do |map|"
5
+ unless options[:pretend]
6
+ gsub_file "config/routes.rb", /(#{Regexp.escape(sentinel)})/mi do |match|
7
+ <<-EOS
8
+ #{match}
9
+ map.resources :users do |users|
10
+ users.resources :trivias do |trivias|
11
+ trivias.resources :trivia_answers
12
+ end
13
+ end
14
+
15
+ EOS
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ def model_acts_as_trivia_includes(question)
22
+ acts_as_trivia_call = "acts_as_trivia :#{question}"
23
+ logger.modify acts_as_trivia_call
24
+
25
+ sentinel = "class #{class_name} < ActiveRecord::Base"
26
+ unless options[:pretend]
27
+ gsub_file File.join("app/models", class_path, "#{singular_name}.rb"), /(#{Regexp.escape(sentinel)})/mi do |match|
28
+ <<-EOS
29
+ #{match}
30
+ #{acts_as_trivia_call}
31
+ EOS
32
+ end
33
+ end
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: balinterdi-acts_as_trivia
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Balint Erdi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-04 00:00:00 -07:00
12
+ date: 2009-05-06 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -27,9 +27,11 @@ extra_rdoc_files:
27
27
  - lib/acts_as_trivia/trivias_helper.rb
28
28
  - lib/acts_as_trivia/user.rb
29
29
  - lib/acts_as_trivia.rb
30
+ - LICENSE
30
31
  - README.markdown
31
32
  - tasks/acts_as_trivia.rake
32
33
  files:
34
+ - acts_as_trivia.gemspec
33
35
  - CHANGELOG
34
36
  - generators/acts_as_trivia/acts_as_trivia_generator.rb
35
37
  - generators/acts_as_trivia/templates/answers_controller.rb
@@ -40,12 +42,14 @@ files:
40
42
  - generators/acts_as_trivia_record/acts_as_trivia_record_generator.rb
41
43
  - generators/acts_as_trivia_record/templates/migration.rb
42
44
  - generators/acts_as_trivia_record/USAGE
45
+ - generators/lib/acts_as_trivia_commands.rb
43
46
  - lib/acts_as_trivia/trivia.rb
44
47
  - lib/acts_as_trivia/trivia_answer.rb
45
48
  - lib/acts_as_trivia/trivia_answers_controller.rb
46
49
  - lib/acts_as_trivia/trivias_helper.rb
47
50
  - lib/acts_as_trivia/user.rb
48
51
  - lib/acts_as_trivia.rb
52
+ - LICENSE
49
53
  - Manifest
50
54
  - Rakefile
51
55
  - README.markdown
@@ -54,7 +58,6 @@ files:
54
58
  - spec/spec_helper.rb
55
59
  - tasks/acts_as_trivia.rake
56
60
  - todos.markdown
57
- - acts_as_trivia.gemspec
58
61
  has_rdoc: true
59
62
  homepage: http://github.com/balinterdi/acts_as_trivia
60
63
  post_install_message: