Class: Usergroup

Inherits:
Object
  • Object
show all
Defined in:
app/models/usergroup.rb

Constant Summary collapse

DELIMITER_TIME =
':'
DELIMITER_DATE =
' '
NUMBERS =
%w(first second third fourth)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cocObject

Returns the value of attribute coc


6
7
8
# File 'app/models/usergroup.rb', line 6

def coc
  @coc
end

#custom_recurringObject

Returns the value of attribute custom_recurring


6
7
8
# File 'app/models/usergroup.rb', line 6

def custom_recurring
  @custom_recurring
end

#default_localeObject

Returns the value of attribute default_locale


6
7
8
# File 'app/models/usergroup.rb', line 6

def default_locale
  @default_locale
end

#default_time_zoneObject

Returns the value of attribute default_time_zone


6
7
8
# File 'app/models/usergroup.rb', line 6

def default_time_zone
  @default_time_zone
end

#domainsObject

Returns the value of attribute domains


6
7
8
# File 'app/models/usergroup.rb', line 6

def domains
  @domains
end

#emailObject

Returns the value of attribute email


6
7
8
# File 'app/models/usergroup.rb', line 6

def email
  @email
end

#google_groupObject

Returns the value of attribute google_group


6
7
8
# File 'app/models/usergroup.rb', line 6

def google_group
  @google_group
end

#imprintObject

Returns the value of attribute imprint


7
8
9
# File 'app/models/usergroup.rb', line 7

def imprint
  @imprint
end

#label_idObject

Returns the value of attribute label_id


6
7
8
# File 'app/models/usergroup.rb', line 6

def label_id
  @label_id
end

#locationObject

Returns the value of attribute location


7
8
9
# File 'app/models/usergroup.rb', line 7

def location
  @location
end

#organizersObject

Returns the value of attribute organizers


7
8
9
# File 'app/models/usergroup.rb', line 7

def organizers
  @organizers
end

#other_usergroupsObject

Returns the value of attribute other_usergroups


7
8
9
# File 'app/models/usergroup.rb', line 7

def other_usergroups
  @other_usergroups
end

#recurringObject

Returns the value of attribute recurring


6
7
8
# File 'app/models/usergroup.rb', line 6

def recurring
  @recurring
end

#sponsorsObject

Returns the value of attribute sponsors


7
8
9
# File 'app/models/usergroup.rb', line 7

def sponsors
  @sponsors
end

#tldObject

Returns the value of attribute tld


7
8
9
# File 'app/models/usergroup.rb', line 7

def tld
  @tld
end

#twitterObject

Returns the value of attribute twitter


7
8
9
# File 'app/models/usergroup.rb', line 7

def twitter
  @twitter
end

Class Method Details

.from_name(name) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/usergroup.rb', line 80

def self.from_name(name)
  raise "you need to provide a name that contains only word characters" unless name =~ /\A\w+\z/

  new.tap do |it|
    it.label_id = it.google_group = it.twitter = name.underscore
    it.default_locale   = 'de'
    it.tld              = 'de'
    it.domains          = ["#{name.parameterize}.de"]
    it.recurring        = 'second wednesday'
    it.email            = "[email protected]#{name.parameterize}.de"
    it.organizers       = ['your_twitter_handle']
    it.location         = {zoom: 14, lat: 53.079296, long: 8.801694}
    it.imprint          = {address: "YourStreet 1\n0815 YourTown", contributors: [{name: "Your Name", email: "[email protected]"}]}
    it.other_usergroups = [{name: "OtherUsergroupName", url: "http://some-domain.de/", twitter: "some_handle"}]
    it.coc              = 'http://example.com'
    it.sponsors         = []
  end
end

.initialize!Object


99
100
101
# File 'app/models/usergroup.rb', line 99

def self.initialize!
  Whitelabel.from_file Rails.root.join("config/whitelabel.yml")
end

.omniauth_keys(provider, request) ⇒ Object


64
65
66
67
68
# File 'app/models/usergroup.rb', line 64

def self.omniauth_keys(provider, request)
  tokens = ["omniauth", provider] + request.domain.gsub(/-/, '').split(".")
  name   = tokens.join("_").upcase
  [ENV["#{name}_KEY"], ENV["#{name}_SECRET"]]
end

.switch_by_request(request) ⇒ Object


70
71
72
73
74
75
76
77
78
# File 'app/models/usergroup.rb', line 70

def self.switch_by_request(request)
  return true if Whitelabel.label_for(request.subdomains.first)

  Whitelabel.label = Whitelabel.labels.find do |label|
    label.domains && label.domains.any? do |custom_domain|
      request.host =~ /#{custom_domain}/
    end
  end
end

Instance Method Details

#localized_custom_recurrenceObject


54
55
56
57
58
# File 'app/models/usergroup.rb', line 54

def localized_custom_recurrence
  recurrence_text = I18n.tw('custom_recurrence')
  recurrence_text = I18n.tw('custom_recurrence', locale: default_locale) if recurrence_text == 'n/a' # fall back to default locale
  recurrence_text == 'n/a' ? nil : recurrence_text
end

#localized_recurringObject


43
44
45
46
47
48
49
50
51
52
# File 'app/models/usergroup.rb', line 43

def localized_recurring
  return localized_custom_recurrence if custom_recurring

  number, day, _ = recurring.split(DELIMITER_DATE)

  ordinal = I18n.t("event.#{number}")
  day     = I18n.t('date.day_names')[Date::DAYS_INTO_WEEK[day.to_sym] + 1]

  I18n.t("event.recurring", ordinal: ordinal, day: day)
end

#next_event_dateObject


36
37
38
39
40
41
# File 'app/models/usergroup.rb', line 36

def next_event_date
  d = parse_recurring_date(Date.today)
  d = parse_recurring_date(Date.today.next_month) unless d.future?
  t = parse_recurring_time
  Time.new(d.year, d.month, d.day, t.hour, t.min)
end

#parse_recurring_date(date) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/usergroup.rb', line 9

def parse_recurring_date(date)
  number, day, _ = recurring.split(DELIMITER_DATE)
  day = Date::DAYS_INTO_WEEK[day.to_sym] + 1
  if number == 'last' then
    d = date.at_end_of_month.change(hour: 0, minute: 0, second: 0)
    d - ((d.wday - day) % 7).days
  else
    d = date.at_beginning_of_month
    num = NUMBERS.index(number)
    if d.wday > day
      d + ((num + 1) * 7 + day - d.wday).days
    else
      d + (num * 7 + day - d.wday)
    end
  end
end

#parse_recurring_timeObject


26
27
28
29
30
31
32
33
34
# File 'app/models/usergroup.rb', line 26

def parse_recurring_time
  _, _, time_string = recurring.split(DELIMITER_DATE)
  if time_string.present?
    hour, min = time_string.split(DELIMITER_TIME)
    t = Time.new(2012, 5, 1, hour.to_i, min.to_i)
  else
    t = Time.new(2012, 5, 1, 19, 0)
  end
end

#to_sObject


60
61
62
# File 'app/models/usergroup.rb', line 60

def to_s
  label_id
end