trivia 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.2-p290@trivia_gem --create
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem "activerecord"
7
+
8
+ # Add dependencies to develop your gem here.
9
+ # Include everything needed to run rake, tests, features, etc.
10
+ group :development do
11
+ gem "guard"
12
+ gem "guard-rspec"
13
+ gem "rb-inotify"
14
+ gem "libnotify"
15
+
16
+ gem "rspec", "~> 2.5.0"
17
+ gem "generator_spec"
18
+ gem "bundler", "~> 1.0.0"
19
+ gem "jeweler", "~> 1.6.4"
20
+ gem "rcov", ">= 0"
21
+ gem "sqlite3"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,129 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.1.1)
5
+ actionpack (= 3.1.1)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.1)
8
+ activemodel (= 3.1.1)
9
+ activesupport (= 3.1.1)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.2)
14
+ rack-cache (~> 1.1)
15
+ rack-mount (~> 0.8.2)
16
+ rack-test (~> 0.6.1)
17
+ sprockets (~> 2.0.2)
18
+ activemodel (3.1.1)
19
+ activesupport (= 3.1.1)
20
+ builder (~> 3.0.0)
21
+ i18n (~> 0.6)
22
+ activerecord (3.1.1)
23
+ activemodel (= 3.1.1)
24
+ activesupport (= 3.1.1)
25
+ arel (~> 2.2.1)
26
+ tzinfo (~> 0.3.29)
27
+ activeresource (3.1.1)
28
+ activemodel (= 3.1.1)
29
+ activesupport (= 3.1.1)
30
+ activesupport (3.1.1)
31
+ multi_json (~> 1.0)
32
+ arel (2.2.1)
33
+ builder (3.0.0)
34
+ diff-lcs (1.1.3)
35
+ erubis (2.7.0)
36
+ ffi (1.0.10)
37
+ generator_spec (0.8.4)
38
+ rails (>= 3.0, < 4.0)
39
+ rspec-rails
40
+ git (1.2.5)
41
+ guard (0.8.8)
42
+ thor (~> 0.14.6)
43
+ guard-rspec (0.5.0)
44
+ guard (>= 0.8.4)
45
+ hike (1.2.1)
46
+ i18n (0.6.0)
47
+ jeweler (1.6.4)
48
+ bundler (~> 1.0)
49
+ git (>= 1.2.5)
50
+ rake
51
+ json (1.6.1)
52
+ libnotify (0.5.9)
53
+ mail (2.3.0)
54
+ i18n (>= 0.4.0)
55
+ mime-types (~> 1.16)
56
+ treetop (~> 1.4.8)
57
+ mime-types (1.17.1)
58
+ multi_json (1.0.3)
59
+ polyglot (0.3.2)
60
+ rack (1.3.5)
61
+ rack-cache (1.1)
62
+ rack (>= 0.4)
63
+ rack-mount (0.8.3)
64
+ rack (>= 1.0.0)
65
+ rack-ssl (1.3.2)
66
+ rack
67
+ rack-test (0.6.1)
68
+ rack (>= 1.0)
69
+ rails (3.1.1)
70
+ actionmailer (= 3.1.1)
71
+ actionpack (= 3.1.1)
72
+ activerecord (= 3.1.1)
73
+ activeresource (= 3.1.1)
74
+ activesupport (= 3.1.1)
75
+ bundler (~> 1.0)
76
+ railties (= 3.1.1)
77
+ railties (3.1.1)
78
+ actionpack (= 3.1.1)
79
+ activesupport (= 3.1.1)
80
+ rack-ssl (~> 1.3.2)
81
+ rake (>= 0.8.7)
82
+ rdoc (~> 3.4)
83
+ thor (~> 0.14.6)
84
+ rake (0.9.2.2)
85
+ rb-inotify (0.8.8)
86
+ ffi (>= 0.5.0)
87
+ rcov (0.9.11)
88
+ rdoc (3.11)
89
+ json (~> 1.4)
90
+ rspec (2.5.0)
91
+ rspec-core (~> 2.5.0)
92
+ rspec-expectations (~> 2.5.0)
93
+ rspec-mocks (~> 2.5.0)
94
+ rspec-core (2.5.2)
95
+ rspec-expectations (2.5.0)
96
+ diff-lcs (~> 1.1.2)
97
+ rspec-mocks (2.5.0)
98
+ rspec-rails (2.5.0)
99
+ actionpack (~> 3.0)
100
+ activesupport (~> 3.0)
101
+ railties (~> 3.0)
102
+ rspec (~> 2.5.0)
103
+ sprockets (2.0.3)
104
+ hike (~> 1.2)
105
+ rack (~> 1.0)
106
+ tilt (~> 1.1, != 1.3.0)
107
+ sqlite3 (1.3.4)
108
+ thor (0.14.6)
109
+ tilt (1.3.3)
110
+ treetop (1.4.10)
111
+ polyglot
112
+ polyglot (>= 0.3.1)
113
+ tzinfo (0.3.30)
114
+
115
+ PLATFORMS
116
+ ruby
117
+
118
+ DEPENDENCIES
119
+ activerecord
120
+ bundler (~> 1.0.0)
121
+ generator_spec
122
+ guard
123
+ guard-rspec
124
+ jeweler (~> 1.6.4)
125
+ libnotify
126
+ rb-inotify
127
+ rcov
128
+ rspec (~> 2.5.0)
129
+ sqlite3
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jan Dupal
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.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = trivia
2
+
3
+ Models for simple quiz
4
+
5
+ == Contributing to trivia
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Jan Dupal. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "trivia"
18
+ gem.homepage = "http://github.com/SmartMedia/trivia"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Models for simple quiz}
21
+ gem.description = %Q{Add simple quiz to your app and let users earn points}
22
+ gem.email = "dupal.j@gmail.com"
23
+ gem.authors = ["Jan Dupal"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "trivia #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,32 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+
4
+ module Trivia
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ desc 'Generates trivia modesl and migrations'
10
+
11
+ def self.source_root
12
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
13
+ end
14
+
15
+ def self.next_migration_number(path)
16
+ ActiveRecord::Generators::Base.next_migration_number(path)
17
+ end
18
+
19
+ def generate_migration
20
+ migration_template 'db/migrate/create_trivia_tables.rb', 'db/migrate/create_trivia_tables.rb'
21
+ end
22
+
23
+ def generate_models
24
+ ['answer.rb', 'chosen_answer.rb', 'question.rb'].map { |f| "app/models/#{f}" }.each { |f| template f, f }
25
+ end
26
+
27
+ def add_acts_as_trivia_user
28
+ say "Please add 'acts_as_trivia_user' to your User model", :green
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ class Answer < ActiveRecord::Base
2
+ include Trivia::Answer
3
+ end
@@ -0,0 +1,14 @@
1
+ class ChosenAnswer < ActiveRecord::Base
2
+ include Trivia::ChosenAnswer
3
+
4
+ after_save :process_answer
5
+
6
+ def process_answer
7
+ if self.answer.right?
8
+ # Add points to user, etc.
9
+ #
10
+ # Reward models and DSL:
11
+ # https://github.com/SmartMedia/treasure_hunt
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class Question < ActiveRecord::Base
2
+ include Trivia::Question
3
+ end
@@ -0,0 +1,33 @@
1
+ class CreateTriviaTables < ActiveRecord::Migration
2
+ def change
3
+ create_table :chosen_answers, :force => true do |t|
4
+ t.references :user
5
+ t.references :answer
6
+ t.references :question
7
+ t.integer :points
8
+
9
+ t.timestamps
10
+ end
11
+ add_index :chosen_answers, :user_id
12
+ add_index :chosen_answers, :answer_id
13
+ add_index :chosen_answers, :question_id
14
+
15
+ create_table :answers, :force => true do |t|
16
+ t.string :answer
17
+ t.boolean :right, :default => false
18
+ t.references :question
19
+
20
+ t.timestamps
21
+ end
22
+ add_index :answers, :question_id
23
+
24
+ create_table :questions, :force => true do |t|
25
+ t.string :question, :null => false
26
+ t.integer :points, :default => 0, :null => false
27
+ t.datetime :published_from
28
+ t.datetime :published_to
29
+
30
+ t.timestamps
31
+ end
32
+ end
33
+ end
data/lib/trivia.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'trivia/trivia_user'
2
+ ActiveRecord::Base.send :extend, Trivia::TriviaUser
3
+
4
+ require 'trivia/answer'
5
+ require 'trivia/chosen_answer'
6
+ require 'trivia/question'
@@ -0,0 +1,23 @@
1
+ module Trivia
2
+ module Answer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ belongs_to :question
7
+
8
+ validates_presence_of :answer, :question
9
+ validates_associated :question
10
+
11
+ scope :right, where(arel_table[:right].eq(true))
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ module Trivia
2
+ module ChosenAnswer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ belongs_to :answer
7
+ belongs_to :user
8
+ belongs_to :question
9
+
10
+ validates_associated :answer, :user
11
+ validates_presence_of :answer, :user
12
+
13
+ validates :question_id, :uniqueness => { :scope => :user_id }
14
+
15
+ before_save :fill_points, :fill_question
16
+ end
17
+
18
+ module ClassMethods
19
+
20
+ end
21
+
22
+ module InstanceMethods
23
+ def fill_points
24
+ self.points = self.answer.right ? self.answer.question.points : 0 unless self.points
25
+ end
26
+
27
+ def fill_question
28
+ self.question = self.answer.question
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ module Trivia
2
+ module Question
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :answers, :dependent => :destroy
7
+
8
+ validates_presence_of :question
9
+ validate :validate_published_to
10
+
11
+ t = self.arel_table
12
+ scope :active, where(t[:published_from].lt(Time.now.to_s(:db)).or(t[:published_from].eq(nil)))
13
+ .where(t[:published_to].gt(Time.now.to_s(:db)).or(t[:published_to].eq(nil)))
14
+
15
+ scope :inactive, where('published_to < ?', Time.now.to_s(:db))
16
+ end
17
+
18
+ module ClassMethods
19
+
20
+ end
21
+
22
+ module InstanceMethods
23
+ def validate_published_to
24
+ if self.published_from && self.published_to
25
+ errors.add(:published_to, 'is before published from') if self.published_to < self.published_from
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ module Trivia
2
+ module TriviaUser
3
+
4
+ def acts_as_trivia_user
5
+ include Trivia::TriviaUser::Model
6
+ end
7
+
8
+ module Model
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ has_many :chosen_answers, :dependent => :destroy
13
+ has_many :answers, :through => :chosen_answers
14
+ has_many :questions, :through => :chosen_answers
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ end
20
+
21
+ module InstanceMethods
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+ require 'action_controller'
3
+ require 'generator_spec/test_case'
4
+
5
+ require 'generators/trivia/install/install_generator'
6
+
7
+ describe Trivia::Generators::InstallGenerator do
8
+ include GeneratorSpec::TestCase
9
+ destination File.expand_path('../../../../../tmp', __FILE__)
10
+
11
+ before do
12
+ prepare_destination
13
+ run_generator
14
+ end
15
+
16
+ specify do
17
+ destination_root.should have_structure {
18
+ directory 'app' do
19
+ directory 'models' do
20
+ file 'answer.rb' do
21
+ contains 'class Answer'
22
+ contains 'Trivia::Answer'
23
+ end
24
+
25
+ file 'chosen_answer.rb' do
26
+ contains 'class ChosenAnswer'
27
+ contains 'Trivia::ChosenAnswer'
28
+ contains 'after_save :process_answer'
29
+ contains 'def process_answer'
30
+ end
31
+
32
+ file 'question.rb' do
33
+ contains 'class Question'
34
+ contains 'Trivia::Question'
35
+ end
36
+ end
37
+ end
38
+ directory 'db' do
39
+ directory 'migrate' do
40
+ migration 'create_trivia_tables' do
41
+ contains 'class CreateTriviaTables'
42
+ contains 'create_table :answers'
43
+ contains 'create_table :chosen_answers'
44
+ contains 'create_table :questions'
45
+ end
46
+ end
47
+ end
48
+ }
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+
5
+ require 'active_record'
6
+
7
+ require 'trivia'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+
15
+ end
@@ -0,0 +1,48 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => "sqlite3",
3
+ :database => ":memory:"
4
+ )
5
+
6
+ ActiveRecord::Migration.verbose = false
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table :users, :force => true do |t|
10
+ t.string :name
11
+ end
12
+
13
+ create_table :chosen_answers, :force => true do |t|
14
+ t.references :user
15
+ t.references :answer
16
+ t.references :question
17
+ t.integer :points
18
+ end
19
+
20
+ create_table :answers, :force => true do |t|
21
+ t.string :answer
22
+ t.boolean :right, :default => false
23
+ t.references :question
24
+ end
25
+
26
+ create_table :questions, :force => true do |t|
27
+ t.string :question, :null => false
28
+ t.integer :points, :default => 0, :null => false
29
+ t.datetime :published_from
30
+ t.datetime :published_to
31
+ end
32
+ end
33
+
34
+ class User < ActiveRecord::Base
35
+ acts_as_trivia_user
36
+ end
37
+
38
+ class ChosenAnswer < ActiveRecord::Base
39
+ include Trivia::ChosenAnswer
40
+ end
41
+
42
+ class Answer < ActiveRecord::Base
43
+ include Trivia::Answer
44
+ end
45
+
46
+ class Question < ActiveRecord::Base
47
+ include Trivia::Question
48
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trivia::Answer do
4
+ before do
5
+ Question.destroy_all
6
+ @question = Question.create(:question => 'What is the meaning of life?', :points => 10)
7
+ end
8
+
9
+ context "when creating answer" do
10
+ it "should validate answer attribute and question" do
11
+ answer = Answer.new
12
+ answer.valid?.should be_false
13
+ answer.errors.should include(:answer)
14
+
15
+ answer.answer = 42
16
+ answer.errors.should include(:question)
17
+ answer.valid?.should be_false
18
+
19
+ answer.question = @question
20
+ answer.valid?.should be_true
21
+ end
22
+
23
+ it "should be wrong default" do
24
+ answer = Answer.new
25
+ answer.right.should be_false
26
+ end
27
+
28
+ it "should be in 'right' scope if right" do
29
+ Answer.right.count.should == 0
30
+
31
+ Answer.create(:question => @question, :answer => 'Your job')
32
+ Answer.right.count.should == 0
33
+
34
+ Answer.create(:question => @question, :answer => 42, :right => true)
35
+ Answer.right.count.should == 1
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trivia::ChosenAnswer do
4
+ before do
5
+ User.delete_all
6
+ Question.delete_all
7
+ @joe = User.create(:name => 'Joe')
8
+ @question = Question.create(:question => 'Is the cake lie?', :points => 42)
9
+ @yes = Answer.create(:answer => 'Yes', :right => true, :question => @question)
10
+ @no = Answer.create(:answer => 'No', :right => false, :question => @question)
11
+ end
12
+
13
+ context "when creating chosen answer" do
14
+ before do
15
+ ChosenAnswer.delete_all
16
+ end
17
+
18
+ it "should validate foreign keys" do
19
+ chosen = ChosenAnswer.new
20
+ chosen.valid?.should be_false
21
+ chosen.errors.should include(:user)
22
+ chosen.errors.should include(:answer)
23
+ chosen.errors.size.should == 2
24
+
25
+ chosen.user = @joe
26
+ chosen.valid?.should be_false
27
+ chosen.errors.should include(:answer)
28
+ chosen.errors.size.should == 1
29
+
30
+ chosen.answer = @yes
31
+ chosen.valid?.should be_true
32
+ end
33
+
34
+ it "should not allow answering same question again" do
35
+ first = ChosenAnswer.create(:user => @joe, :answer => @yes)
36
+ first.valid?.should be_true
37
+
38
+ # Same answer
39
+ second = ChosenAnswer.create(:user => @joe, :answer => @yes)
40
+ second.valid?.should be_false
41
+
42
+ # Different answer
43
+ third = ChosenAnswer.create(:user => @joe, :answer => @no)
44
+ second.valid?.should be_false
45
+ end
46
+
47
+ it "should have same points as question if right" do
48
+ chosen = ChosenAnswer.create(:user => @joe, :answer => @yes)
49
+ chosen.points.should == 42
50
+ end
51
+
52
+ it "should have 0 points if wrong" do
53
+ chosen = ChosenAnswer.create(:user => @joe, :answer => @no)
54
+ chosen.new_record?.should be_false
55
+ chosen.points.should == 0
56
+ end
57
+
58
+ it "should not change points if set when creating" do
59
+ chosen = ChosenAnswer.create(:user => @joe, :answer => @yes, :points => 13)
60
+ chosen.new_record?.should be_false
61
+ chosen.points.should == 13
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trivia::Question do
4
+ before do
5
+ Question.destroy_all
6
+ end
7
+
8
+ context "when creating question" do
9
+ it "should validate question attribute" do
10
+ question = Question.new
11
+ question.valid?.should be_false
12
+ question.errors.should include(:question)
13
+
14
+ question.question= 'What is the Ultimate Answer to the Ultimate Question of Life?'
15
+ question.valid?.should be_true
16
+ end
17
+
18
+ it "activity should depend on datetime range" do
19
+ Question.active.count.should == 0
20
+
21
+ # Inactive
22
+ # Published from tomorrow
23
+ Question.create(:question => 'Inactive', :published_from => Time.now + 1.day)
24
+ Question.active.count.should == 0
25
+
26
+ # Published until yesterday
27
+ Question.create(:question => 'Inactive', :published_to => 1.day.ago)
28
+ Question.active.count.should == 0
29
+
30
+ # Active
31
+ # Default is active
32
+ Question.create(:question => 'Active')
33
+ Question.active.count.should == 1
34
+
35
+ # Published since yesterday
36
+ Question.create(:question => 'Active', :published_from => 1.day.ago)
37
+ Question.active.count.should == 2
38
+
39
+ # Published until tomorrow
40
+ Question.create(:question => 'Active', :published_to => Time.now + 1.day)
41
+ Question.active.count.should == 3
42
+ end
43
+
44
+ it "should validate published_from to published_to range" do
45
+ question = Question.new(:question => 'Is this the real life? Is this just fantasy?')
46
+ question.valid?.should be_true
47
+
48
+ # Possible combination
49
+ question.published_from = DateTime.now
50
+ question.published_to = DateTime.now + 1.day
51
+ question.valid?.should be_true
52
+
53
+ # Impossible combination, dates swapped
54
+ question.published_from, question.published_to = question.published_to, question.published_from
55
+ question.valid?.should be_false
56
+
57
+ # Possible combination - one is nil
58
+ question.published_from = nil
59
+ question.valid?.should be_true
60
+
61
+ # Possible combination - one is nil
62
+ question.published_from, question.published_to = question.published_to, question.published_from
63
+ question.valid?.should be_true
64
+ end
65
+ end
66
+
67
+ context "when adding answers" do
68
+ it "should have answers" do
69
+ question = Question.create(:question => 'Is this the real life? Is this just fantasy?')
70
+ first = Answer.create(:answer => 'Caught in the landslide', :question => question)
71
+ second = Answer.create(:answer => 'No escape from reality', :question => question)
72
+
73
+ question.answers.should =~ [first, second]
74
+ end
75
+ end
76
+
77
+ context "when destroyed" do
78
+ it "should destroy all it's answers" do
79
+ question = Question.create(:question => 'Can you tell a green field?')
80
+ first = Answer.create(:answer => 'From a cold steel rail?', :question => question)
81
+ second = Answer.create(:answer => 'A smile from a veil?', :question => question)
82
+ third = Answer.create(:answer => 'Wish you were here', :question => question, :right => true)
83
+
84
+ Answer.find_all_by_id([first, second, third].map(&:id)).count.should == 3
85
+
86
+ question.destroy
87
+
88
+ Answer.find_all_by_id([first, second, third].map(&:id)).count.should == 0
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Trivia::TriviaUser do
4
+ before do
5
+ User.destroy_all
6
+ @jane = User.create(:name => 'Jane')
7
+ end
8
+
9
+ context "when answered something" do
10
+ before do
11
+ Question.destroy_all
12
+ @question = Question.create(:question => 'Is the cake lie?', :points => 42)
13
+ @yes = @question.answers.create(:answer => 'Yes', :right => true)
14
+ @no = @question.answers.create(:answer => 'No', :right => false)
15
+ end
16
+
17
+ it "should have answer and question" do
18
+ chosen = ChosenAnswer.new(:answer => @no)
19
+ @jane.chosen_answers << chosen
20
+
21
+ @jane.answers.should =~ [@no]
22
+ @jane.questions.should =~ [@question]
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trivia
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Dupal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &87160720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *87160720
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &87160410 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *87160410
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-rspec
38
+ requirement: &87159710 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *87159710
47
+ - !ruby/object:Gem::Dependency
48
+ name: rb-inotify
49
+ requirement: &87158770 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *87158770
58
+ - !ruby/object:Gem::Dependency
59
+ name: libnotify
60
+ requirement: &87158190 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *87158190
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &87157820 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 2.5.0
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *87157820
80
+ - !ruby/object:Gem::Dependency
81
+ name: generator_spec
82
+ requirement: &87157470 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *87157470
91
+ - !ruby/object:Gem::Dependency
92
+ name: bundler
93
+ requirement: &87157040 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: 1.0.0
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *87157040
102
+ - !ruby/object:Gem::Dependency
103
+ name: jeweler
104
+ requirement: &87156650 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.6.4
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *87156650
113
+ - !ruby/object:Gem::Dependency
114
+ name: rcov
115
+ requirement: &87156310 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *87156310
124
+ - !ruby/object:Gem::Dependency
125
+ name: sqlite3
126
+ requirement: &87156010 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *87156010
135
+ description: Add simple quiz to your app and let users earn points
136
+ email: dupal.j@gmail.com
137
+ executables: []
138
+ extensions: []
139
+ extra_rdoc_files:
140
+ - LICENSE.txt
141
+ - README.rdoc
142
+ files:
143
+ - .document
144
+ - .rspec
145
+ - .rvmrc
146
+ - Gemfile
147
+ - Gemfile.lock
148
+ - Guardfile
149
+ - LICENSE.txt
150
+ - README.rdoc
151
+ - Rakefile
152
+ - VERSION
153
+ - lib/generators/trivia/install/install_generator.rb
154
+ - lib/generators/trivia/install/templates/app/models/answer.rb
155
+ - lib/generators/trivia/install/templates/app/models/chosen_answer.rb
156
+ - lib/generators/trivia/install/templates/app/models/question.rb
157
+ - lib/generators/trivia/install/templates/db/migrate/create_trivia_tables.rb
158
+ - lib/trivia.rb
159
+ - lib/trivia/answer.rb
160
+ - lib/trivia/chosen_answer.rb
161
+ - lib/trivia/question.rb
162
+ - lib/trivia/trivia_user.rb
163
+ - spec/generators/trivia/install/install_generator_spec.rb
164
+ - spec/spec_helper.rb
165
+ - spec/support/active_record.rb
166
+ - spec/trivia/answer_spec.rb
167
+ - spec/trivia/chosen_answer_spec.rb
168
+ - spec/trivia/question_spec.rb
169
+ - spec/trivia/trivia_user_spec.rb
170
+ homepage: http://github.com/SmartMedia/trivia
171
+ licenses:
172
+ - MIT
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ! '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ segments:
184
+ - 0
185
+ hash: 892929181
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ none: false
188
+ requirements:
189
+ - - ! '>='
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 1.8.6
195
+ signing_key:
196
+ specification_version: 3
197
+ summary: Models for simple quiz
198
+ test_files: []