diff options
author | ankonym <ankonym@gmail.com> | 2015-08-21 17:49:36 +0200 |
---|---|---|
committer | ankonym <ankonym@gmail.com> | 2015-09-28 15:12:45 +0200 |
commit | 0543217b433a8f4809f08018c1a11c20119fa85d (patch) | |
tree | 2479c25010afcd5b9b8929979d8b0efb39f4087f | |
parent | 35494972411d4649ed8e81e24b6ec3f8734f5529 (diff) |
assign random invite code when creating new invite codes
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 2 | ||||
-rw-r--r-- | app/models/invite_code.rb | 11 | ||||
-rw-r--r-- | test/unit/invite_code_test.rb | 21 |
4 files changed, 34 insertions, 1 deletions
@@ -13,6 +13,7 @@ end ## AUTHENTICATION gem "ruby-srp", "~> 0.2.1" gem "rails_warden" +gem "coupon_code" ## LOCALIZATION gem 'http_accept_language' diff --git a/Gemfile.lock b/Gemfile.lock index b859e81..5ffb0c8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,6 +92,7 @@ GEM actionpack (~> 3.0) couchrest couchrest_model + coupon_code (0.0.1) cucumber (1.3.17) builder (>= 2.1.2) diff-lcs (>= 1.1.3) @@ -276,6 +277,7 @@ DEPENDENCIES couchrest (~> 1.1.3) couchrest_model (~> 2.0.0) couchrest_session_store (= 0.3.0) + coupon_code cucumber-rails debugger factory_girl_rails diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 751b28e..d52436f 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -1,10 +1,19 @@ +require 'coupon_code' + class InviteCode < CouchRest::Model::Base use_database 'invite_codes' - property :invite_code, String + property :invite_code, String, :read_only => true timestamps! design do view :by_invite_code end + + def initialize(attributes = {}, options = {}) + super(attributes, options) + write_attribute('invite_code', CouponCode.generate) if new? + end + end + diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb new file mode 100644 index 0000000..2684f8e --- /dev/null +++ b/test/unit/invite_code_test.rb @@ -0,0 +1,21 @@ +require 'test_helper' + +class InviteCodeTest < ActiveSupport::TestCase + + test "it is created with an invite code" do + code = InviteCode.new + assert_not_nil code.invite_code + end + + test "the invite code can be read from couch db correctly" do + code1 = InviteCode.new + code1.save + + code2 = InviteCode.find_by__id code1.id + + assert_equal code1.invite_code, code2.invite_code + + end + + +end
\ No newline at end of file |