summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNavaL <ayoyo@thoughtworks.com>2016-07-14 15:44:07 +0200
committerNavaL <ayoyo@thoughtworks.com>2016-07-14 15:44:07 +0200
commite3c2cb91dfef5c39c608b967e702e9de977d1bd2 (patch)
tree154dc28dd986bd6e0a48e933c5da46994ffaa0cb /lib
parente2f19bcfb6dbce77746c2d61715340525b29a592 (diff)
parentf09e6ec1337962ab279f021a6a6d0ff30479ebe0 (diff)
Merge branch 'develop' of https://github.com/leapcode/leap_web into feature/expose_admin_in_api
Diffstat (limited to 'lib')
-rw-r--r--lib/email.rb31
-rw-r--r--lib/extensions/couchrest.rb4
-rw-r--r--lib/gemfile_tools.rb4
-rw-r--r--lib/local_email.rb67
-rw-r--r--lib/login_format_validation.rb21
-rw-r--r--lib/tasks/i18n.rake2
-rw-r--r--lib/tasks/test.rake15
7 files changed, 134 insertions, 10 deletions
diff --git a/lib/email.rb b/lib/email.rb
new file mode 100644
index 0000000..4090275
--- /dev/null
+++ b/lib/email.rb
@@ -0,0 +1,31 @@
+class Email < String
+ include ActiveModel::Validations
+
+ validates :email,
+ :format => {
+ :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, #local part of email is case-sensitive, so allow uppercase letter.
+ :message => "needs to be a valid email address"
+ }
+
+ # Make sure we can call Email.new(nil) and get an invalid email address
+ def initialize(s)
+ super(s.to_s)
+ end
+
+ def to_partial_path
+ "emails/email"
+ end
+
+ def to_param
+ to_s
+ end
+
+ def email
+ self
+ end
+
+ def handle
+ self.split('@').first
+ end
+
+end
diff --git a/lib/extensions/couchrest.rb b/lib/extensions/couchrest.rb
index cc041e0..4578926 100644
--- a/lib/extensions/couchrest.rb
+++ b/lib/extensions/couchrest.rb
@@ -69,7 +69,7 @@ module CouchRest
def prepare_directory(dir = '')
dir = Rails.root + 'tmp' + 'designs' + dir
- Dir.mkdir(dir) unless Dir.exists?(dir)
+ Dir.mkdir(dir) unless Dir.exist?(dir)
return dir
end
@@ -81,7 +81,7 @@ module CouchRest
class ModelRailtie
config.action_dispatch.rescue_responses.merge!(
'CouchRest::Model::DocumentNotFound' => :not_found,
- 'RestClient::ResourceNotFound' => :not_found
+ 'CouchRest::NotFound' => :not_found
)
end
end
diff --git a/lib/gemfile_tools.rb b/lib/gemfile_tools.rb
index dce2448..d1c00dc 100644
--- a/lib/gemfile_tools.rb
+++ b/lib/gemfile_tools.rb
@@ -68,7 +68,7 @@ def local_config
empty_hash.default_proc = proc{|h, k| h.key?(k.to_s) ? h[k.to_s] : nil}
["defaults.yml", "config.yml"].inject(empty_hash.dup) {|config, file|
filepath = File.join(File.expand_path("../../config", __FILE__), file)
- if File.exists?(filepath)
+ if File.exist?(filepath)
new_config = YAML.load_file(filepath)
['development', 'test','production'].each do |env|
config[env] ||= empty_hash.dup
@@ -87,7 +87,7 @@ end
# or nil if not actually a gem directory
#
def gem_info(gem_dir)
- if Dir.exists?(gem_dir)
+ if Dir.exist?(gem_dir)
gemspec = Dir["#{gem_dir}/*.gemspec"]
if gemspec.any?
gem_name = File.basename(gemspec.first).sub(/\.gemspec$/,'')
diff --git a/lib/local_email.rb b/lib/local_email.rb
new file mode 100644
index 0000000..7c592e1
--- /dev/null
+++ b/lib/local_email.rb
@@ -0,0 +1,67 @@
+require 'email'
+class LocalEmail < Email
+
+ BLACKLIST_FROM_RFC2142 = [
+ 'postmaster', 'hostmaster', 'domainadmin', 'webmaster', 'www',
+ 'abuse', 'noc', 'security', 'usenet', 'news', 'uucp',
+ 'ftp', 'sales', 'marketing', 'support', 'info'
+ ]
+
+ def self.domain
+ APP_CONFIG[:domain]
+ end
+
+ validates :email,
+ :format => {
+ :with => /@#{domain}\Z/i,
+ :message => "needs to end in @#{domain}"
+ }
+
+ validate :handle_allowed
+
+ def initialize(s)
+ super
+ append_domain_if_needed
+ end
+
+ def to_key
+ [handle]
+ end
+
+ def domain
+ LocalEmail.domain
+ end
+
+ protected
+
+ def append_domain_if_needed
+ unless self.index('@')
+ self << '@' + domain
+ end
+ end
+
+ def handle_allowed
+ errors.add(:handle, "is reserved.") if handle_reserved?
+ end
+
+ def handle_reserved?
+ # *ARRAY in a case statement tests if ARRAY includes the handle.
+ case handle
+ when *APP_CONFIG[:handle_blacklist]
+ true
+ when *APP_CONFIG[:handle_whitelist]
+ false
+ when *BLACKLIST_FROM_RFC2142
+ true
+ else
+ handle_in_passwd?
+ end
+ end
+
+ def handle_in_passwd?
+ Etc.getpwnam(handle).present?
+ rescue ArgumentError
+ # handle was not found
+ return false
+ end
+end
diff --git a/lib/login_format_validation.rb b/lib/login_format_validation.rb
new file mode 100644
index 0000000..c1fcf70
--- /dev/null
+++ b/lib/login_format_validation.rb
@@ -0,0 +1,21 @@
+module LoginFormatValidation
+ extend ActiveSupport::Concern
+
+ #TODO: Probably will replace this. Playing with using it for aliases too, but won't want it connected to login field.
+
+ included do
+ # Have multiple regular expression validations so we can get specific error messages:
+ validates :login,
+ :format => { :with => /\A.{2,}\z/,
+ :message => "Must have at least two characters"}
+ validates :login,
+ :format => { :with => /\A[a-z\d_\.-]+\z/,
+ :message => "Only lowercase letters, digits, . - and _ allowed."}
+ validates :login,
+ :format => { :with => /\A[a-z].*\z/,
+ :message => "Must begin with a lowercase letter"}
+ validates :login,
+ :format => { :with => /\A.*[a-z\d]\z/,
+ :message => "Must end with a letter or digit"}
+ end
+end
diff --git a/lib/tasks/i18n.rake b/lib/tasks/i18n.rake
index 6ffbb23..1034211 100644
--- a/lib/tasks/i18n.rake
+++ b/lib/tasks/i18n.rake
@@ -63,7 +63,7 @@ namespace :i18n do
desc "pull translations from transifex"
task :download do
Dir.chdir('config/') do
- if !File.exists?('transifex.netrc')
+ if !File.exist?('transifex.netrc')
puts "In order to download translations, you need a config/transifex.netrc file."
puts "For example:"
puts "machine www.transifex.com login yourusername password yourpassword"
diff --git a/lib/tasks/test.rake b/lib/tasks/test.rake
index d96b625..9859729 100644
--- a/lib/tasks/test.rake
+++ b/lib/tasks/test.rake
@@ -1,10 +1,15 @@
namespace :test do
- [:units, :functionals, :integration].each do |type|
- Rails::SubTestTask.new(type => "test:prepare") do |t|
- t.libs << "test"
- subdir = type.to_s.singularize
- t.pattern = "engines/*/test/#{subdir}/**/*_test.rb"
+ namespace :engines do
+ [:units, :functionals, :integration].each do |type|
+ desc "Test engine #{type}"
+ Rails::TestTask.new(type => "test:prepare") do |t|
+ t.libs << "test"
+ subdir = type.to_s.singularize
+ t.pattern = "engines/*/test/#{subdir}/**/*_test.rb"
+ end
+ Rake::Task["test:#{type}"].enhance ["test:engines:#{type}"]
+ Rake::Task["test"].enhance ["test:engines:#{type}"]
end
end