metajp 0.1.0 → 0.2.0
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/lib/metajp.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
|
3
|
+
# super_crud
|
2
4
|
require File.dirname(__FILE__) + '/metajp/shared/super_crud/controller.rb'
|
3
5
|
require File.dirname(__FILE__) + '/metajp/shared/super_crud/model.rb'
|
4
6
|
require File.dirname(__FILE__) + '/metajp/shared/super_crud/helper.rb'
|
5
7
|
|
8
|
+
# acts_as_invitable
|
9
|
+
require File.dirname(__FILE__) + '/metajp/shared/acts_as_invitable/model.rb'
|
10
|
+
|
6
11
|
module Metajp
|
7
12
|
|
8
|
-
VERSION = '0.
|
13
|
+
VERSION = '0.2.0'
|
9
14
|
|
10
15
|
#----------------------------------------------------------------
|
11
16
|
# extensions for the controller
|
@@ -19,6 +24,8 @@ module Metajp
|
|
19
24
|
module ClassMethods
|
20
25
|
def super_controller
|
21
26
|
send :include, Metajp::Shared::SuperCrud::Controller
|
27
|
+
before_filter :require_user
|
28
|
+
before_filter :is_admin?
|
22
29
|
end
|
23
30
|
end
|
24
31
|
end
|
@@ -35,4 +42,7 @@ end
|
|
35
42
|
|
36
43
|
if defined?(Rails) && defined?(ActionController)
|
37
44
|
ActionController::Base.send :include, Metajp::Controller
|
38
|
-
|
45
|
+
ActiveRecord::Base.send(:include, Metajp::ActsAsInvitable)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Metajp
|
2
|
+
|
3
|
+
#----------------------------------------------------------------
|
4
|
+
# This module allows you to add invite codes to your application
|
5
|
+
# It extends your model with the invitation class and instance level variables
|
6
|
+
#
|
7
|
+
# Usage:
|
8
|
+
#
|
9
|
+
# require 'metajp'
|
10
|
+
# class Invitation
|
11
|
+
# acts_as_invitable
|
12
|
+
# end
|
13
|
+
#----------------------------------------------------------------
|
14
|
+
|
15
|
+
module ActsAsInvitable
|
16
|
+
|
17
|
+
def self.included(base)
|
18
|
+
base.extend ClassMethods
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def acts_as_invitable
|
23
|
+
validates_uniqueness_of :code
|
24
|
+
validates_presence_of :code
|
25
|
+
belongs_to :user
|
26
|
+
named_scope :search, lambda { |search| { :conditions => ['code like ? OR sent_to like ?', "%#{search}%", "%#{search}%"] } }
|
27
|
+
include Metajp::ActsAsInvitable::InstanceMethods
|
28
|
+
extend Metajp::ActsAsInvitable::SingletonMethods
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# This module contains class methods
|
33
|
+
module SingletonMethods
|
34
|
+
def find_valid(code)
|
35
|
+
find(:first, :conditions => ["code = ? AND quantity > 0", code])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module InstanceMethods
|
40
|
+
def redeem!
|
41
|
+
self.quantity -= 1
|
42
|
+
self.save
|
43
|
+
end
|
44
|
+
|
45
|
+
def before_validation
|
46
|
+
self.generate_unique_code if self.code.blank?
|
47
|
+
end
|
48
|
+
|
49
|
+
# Generates an alphanumeric code using an MD5 hash
|
50
|
+
# * +code_length+ - number of characters to return
|
51
|
+
def generate_code(code_length=6)
|
52
|
+
chars = ("a".."z").to_a + ("1".."9").to_a
|
53
|
+
new_code = Array.new(code_length, '').collect{chars[rand(chars.size)]}.join
|
54
|
+
Digest::MD5.hexdigest(new_code)[0..(code_length-1)].upcase
|
55
|
+
end
|
56
|
+
|
57
|
+
# Generates unique code based on +generate_code+ method
|
58
|
+
def generate_unique_code
|
59
|
+
begin
|
60
|
+
self.code = generate_code(10)
|
61
|
+
end until !self.active_code?
|
62
|
+
end
|
63
|
+
|
64
|
+
# makes sure the code is unique
|
65
|
+
def active_code?
|
66
|
+
Invitation.first(:conditions => { :code => self.code })
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -67,6 +67,12 @@ module Metajp
|
|
67
67
|
# helper functions
|
68
68
|
#----------------------------------------------------------------
|
69
69
|
|
70
|
+
# determines if the user is an admin
|
71
|
+
def is_admin?
|
72
|
+
flash[:notice] = 'You do not have access to this area.'
|
73
|
+
redirect_to '/' unless current_user.admin?
|
74
|
+
end
|
75
|
+
|
70
76
|
# used to set the path in the #_update and #_create methods
|
71
77
|
def set_path(object, path)
|
72
78
|
path.blank? ? "#{@_path}/#{object.id}" : path
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metajp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Paul Narowski
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-31 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -29,6 +29,7 @@ files:
|
|
29
29
|
- README.rdoc
|
30
30
|
- Rakefile
|
31
31
|
- lib/metajp.rb
|
32
|
+
- lib/metajp/shared/acts_as_invitable/model.rb
|
32
33
|
- lib/metajp/shared/super_crud/controller.rb
|
33
34
|
- lib/metajp/shared/super_crud/helper.rb
|
34
35
|
- lib/metajp/shared/super_crud/model.rb
|