Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 7
- Log:
Initial import of RForum 0.2 sources from a downloaded Tarball.
RForum is a Ruby On Rails based forum and mail gateway service.
- Author:
- adh
- Date:
- Sat Jul 22 18:43:13 +0100 2006
- Size:
- 2536 Bytes
1 | class ActiveRecord::Base |
2 | |
3 | def check_mandatory_atributes(*attributes) |
4 | attributes.each do |attr| |
5 | value = self.send(attr.to_sym) |
6 | if value.nil? or value.to_s.empty? |
7 | raise ArgumentError.new("Mandatory attribute #{attr} is not set") |
8 | end |
9 | end |
10 | end |
11 | |
12 | # Returns the first row retrieved by find_by_sql(query). |
13 | # Raises ActiveRecord::RecordNotFound if no rows were found |
14 | def self.find_first_by_sql(query) |
15 | result = find_by_sql(query) |
16 | if result.empty? |
17 | raise ActiveRecord::RecordNotFound.new("Couldn't find #{self} specified by query") |
18 | end |
19 | return result.first |
20 | end |
21 | |
22 | # Checks if a string may be a real email or not |
23 | def valid_email?(email) |
24 | email.size < 100 && email =~ /.@.+\../ && email.count('@') == 1 |
25 | end |
26 | |
27 | end |
28 | |
29 | class ActiveRecord::Errors |
30 | # Duplicate check |
31 | def add_on_duplicate(attributes, msg = 'with same value already exists') |
32 | # if this save is an update, not a create, then duplicate name/email check should obviously |
33 | # exclude the existing record that is being updated |
34 | not_same_id_clause = '' |
35 | not_same_id_clause = "AND id <> #{@base.id}" unless @base.new_record? |
36 | [attributes].flatten.each { |attr| |
37 | if @base.class.find_first ["#{attr} = '%s' #{not_same_id_clause}", @base[attr]] |
38 | add(attr, msg) |
39 | end |
40 | } |
41 | end |
42 | |
43 | end |
44 | |
45 | # All model classes must raise RForum::ValidationError on errors by including this |
46 | # mixin. If after_validation is overriden in the class, it should call the mixin |
47 | # version like this: |
48 | # |
49 | # alias :error_raising_after_validation :after_validation |
50 | # ... |
51 | # def after_validation |
52 | # ... |
53 | # ... validations, no return from the middle ... |
54 | # ... |
55 | # error_raising_before_validation_on_create |
56 | # end |
57 | |
58 | # Error class to raise on errors in validate |
59 | module RForum |
60 | class ValidationError < StandardError |
61 | attr_reader :entity |
62 | |
63 | def initialize(entity) |
64 | @entity = entity |
65 | end |
66 | |
67 | def errors |
68 | @entity.errors |
69 | end |
70 | |
71 | def message |
72 | s = "Problems with #{@entity.class} ##{@entity.id || 'nil'} :\n" |
73 | @entity.errors.each { |attr, msg| |
74 | s << " #{attr} : #{msg} (value: #{@entity.send(attr.to_sym) || 'nil'})\n" |
75 | } |
76 | s |
77 | end |
78 | |
79 | def to_s |
80 | "#{self.class} : #{message}" |
81 | end |
82 | |
83 | end |
84 | end |
85 | |
86 | # If this module is included in an AR class, RForum::ValidationError is raised |
87 | # after validation if there were any validation errors. |
88 | module ErrorRaising |
89 | def after_validation |
90 | raise RForum::ValidationError.new(self) unless self.errors.empty? |
91 | end |
92 | end |