Skip to content

Commit d92fb81

Browse files
committed
Pass configuration through the DOM rather than relying on ERB rendering, removing the coupling between rendered assets and the backend application configuration in place at time of rendering.
1 parent 82cfd2c commit d92fb81

File tree

9 files changed

+29
-23
lines changed

9 files changed

+29
-23
lines changed

app/assets/javascripts/backbone/broadcasters/faye.js.coffee.erb renamed to app/assets/javascripts/backbone/broadcasters/faye.js.coffee

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class Kandan.Broadcasters.FayeBroadcaster
22

33
constructor: ()->
4-
@fayeClient = new Faye.Client("<%= ENV['KANDAN_FAYE_URL'] %>/remote/faye")
4+
endpoint = $('body').data('kandan-config').broadcaster.config.endpoint
5+
@fayeClient = new Faye.Client(endpoint)
56

67
@fayeClient.disable('websocket')
78
authExtension = {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class Kandan.Helpers.Avatars
22
@urlFor: (a, options) ->
33
size = options.size || 30
4-
fallback = options.fallback || Kandan.options.avatarFallback || 'mm'
4+
fallback = options.fallback || Kandan.options().avatar_fallback || 'mm'
55
avatarHash = a.gravatar_hash || a.get('user').gravatar_hash || a.get('gravatarHash')
6-
Kandan.options.avatarUrl.replace(/%{hash}/, avatarHash).
6+
Kandan.options().avatar_url.replace(/%{hash}/, avatarHash).
77
replace(/%{size}/, size).
88
replace(/%{fallback}/, fallback)

app/assets/javascripts/backbone/helpers/channels.js.coffee

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ class Kandan.Helpers.Channels
22

33
@options:
44
autoScrollThreshold: 0.90
5-
maxActivities: Kandan.options.perPage
65

76
@pastAutoScrollThreshold: (channelId)->
87
currentPosition = @currentScrollPosition channelId
@@ -45,7 +44,7 @@ class Kandan.Helpers.Channels
4544

4645
@flushActivities: (channelId)->
4746
$channelActivities = $("#channel-activities-#{channelId}")
48-
if $channelActivities.children().length == @options.maxActivities + 1
47+
if $channelActivities.children().length == Kandan.options().per_page + 1
4948
$channelActivities.children().first().remove()
5049
oldest = $channelActivities.children().first().data("activity-id")
5150
$channelActivities.prev().data("oldest", oldest)

app/assets/javascripts/backbone/kandan.js.coffee.erb renamed to app/assets/javascripts/backbone/kandan.js.coffee

+12-13
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ window.Kandan =
1616
Data: {}
1717
Plugins: {}
1818

19-
options:
20-
broadcaster: "<%= Kandan::Config.options[:broadcaster][:name] %>"
21-
perPage : <%= Kandan::Config.options[:per_page] %>
22-
nowThreshold: 3000
23-
timestampRefreshInterval: 2000
24-
avatarUrl: "<%= Kandan::Config.options[:avatar_url] %>"
25-
avatarFallback: "<%= Kandan::Config.options[:avatar_fallback] %>"
19+
options: ->
20+
unless @_options?
21+
@_options = $('body').data('kandan-config')
22+
@_options.nowThreshold = 3000
23+
@_options.timestampRefreshInterval = 2000
24+
return @_options
2625

2726

2827
# TODO this is a helper method to register plugins
@@ -69,7 +68,7 @@ window.Kandan =
6968

7069

7170
initBroadcasterAndSubscribe: ()->
72-
Kandan.broadcaster = eval "new Kandan.Broadcasters.#{@options.broadcaster}Broadcaster()"
71+
Kandan.broadcaster = eval "new Kandan.Broadcasters.#{@options().broadcaster.name}Broadcaster()"
7372
Kandan.broadcaster.subscribe "/channels/*"
7473
@registerAppEvents()
7574

@@ -80,10 +79,10 @@ window.Kandan =
8079

8180
$(document).data('active-channel-id',
8281
Kandan.Helpers.Channels.getChannelIdByTabIndex(ui.index))
83-
84-
#the need for the delay feels hacky to me.
82+
83+
#the need for the delay feels hacky to me.
8584
#It is there because the chat area has to render before scrollHeight can be determined.
86-
theId = Kandan.Helpers.Channels.getActiveChannelId()
85+
theId = Kandan.Helpers.Channels.getActiveChannelId()
8786
delay = (ms, func) -> setTimeout func, ms
8887
delay 1, -> Kandan.Helpers.Channels.scrollToLatestMessage(theId)
8988
Kandan.Data.Channels.runCallbacks('change')
@@ -134,8 +133,8 @@ window.Kandan =
134133
registerUtilityEvents: ()->
135134
window.setInterval(=>
136135
for el in $(".posted_at")
137-
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options.nowThreshold)
138-
, @options.timestampRefreshInterval)
136+
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options().nowThreshold)
137+
, @options().timestampRefreshInterval)
139138

140139
init: ->
141140
channels = new Kandan.Collections.Channels()

app/assets/templates/activity_base.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<span class="posted_at">
2-
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
2+
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
33
</span>
44
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

app/assets/templates/message.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<span class="posted_at">
2-
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
2+
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
33
</span>
44
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

app/assets/templates/user_notification.jst.eco

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<span class="posted_at">
2-
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options.nowThreshold) %>
2+
<%= new Date(@activity.created_at).toRelativeTime(Kandan.options().nowThreshold) %>
33
</span>
44
<img class="avatar" src="<%= @activity.avatarUrl %>"/>
55

app/views/layouts/application.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<% end %>
1818
<%- end %>
1919
</head>
20-
<body>
20+
<body data-kandan-config="<%= Kandan::Config.to_json %>">
2121
<div id="kandan">
2222
<div class="header">
2323
<a href="/">

lib/kandan_config.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Kandan
22
class Config
3-
43
class << self
54
def options
65
@config ||= YAML.load_file "#{Rails.root}/config/kandan_settings.yml"
@@ -9,6 +8,14 @@ def options
98
def broadcaster
109
Broadcasters.const_get(Kandan::Config.options[:broadcaster][:name])
1110
end
11+
12+
def to_json
13+
@json_representation ||= options.dup.tap do |h|
14+
h[:broadcaster][:config] = {
15+
endpoint: "#{ENV['KANDAN_FAYE_URL']}/remote/faye"
16+
}
17+
end.to_json
18+
end
1219
end
1320

1421
end

0 commit comments

Comments
 (0)