Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions app/models/views/channel_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ class ChannelList < ListView
fetches :filter_ids, proc { Channel.filter_ids(site, query, current_user) }
fetches :recent_channels, proc { Channel.recent_channels(site, current_user, page, per_page, last_update_date, filter_ids) }, [:filter_ids]
fetches :channels_read, proc {
recent_channels.each do |channel|
channel.read = channel.has_posts?(current_user, channel.last_post)
end
recent_channels.each { |channel| channel.read = channel.has_posts?(current_user, channel.last_post) }
nil
}, [:recent_channels]
fetches :highlight_query, proc {
recent_channels.each do |channel|
channel.query = query
end
recent_channels.each { |channel| channel.query = query }
nil
}
}, [:recent_channels]

fetches :count, proc { recent_channels.count }
fetches :count, proc { recent_channels.count }, [:recent_channels]
fetches :start_index, proc { (page - 1) * per_page }
fetches :end_index, proc { (page - 1) * per_page + recent_channels.size }, [:recent_channels]
fetches :last_update, proc { (recent_channels.map(&:created_at) + recent_channels.map(&:updated_at) + recent_channels.map(&:last_post_date)).map(&:utc).max.to_i }, [:recent_channels]
Expand Down
2 changes: 1 addition & 1 deletion app/models/views/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Search < ListView
end
end
nil
}
}, [:results]

fetches :last_update, proc { Time.now }
fetches :count, proc { results[:result_count] }, [:results]
Expand Down
1 change: 1 addition & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@

config.logger = Logger::Syslog.new("local0", Syslog::LOG_LOCAL5)

config.active_record.raise_in_transactional_callbacks = true
end
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# Allow pass debug_assets=true as a query parameter to load pages with unpackaged assets
config.assets.allow_debugging = true

config.eager_load = false
config.eager_load = true

config.active_support.test_order = :random

Expand Down
34 changes: 29 additions & 5 deletions lib/view_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,47 @@ def initialize(attrs={})
end

def finalize
t = Time.now.to_f
dependencies = []
threads = []
self.class.fetch_args.each do |arg|
if !send("fetch_#{arg}")
dependencies << arg
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
Rails.logger.info "view model thread #{arg} start"
t1 = Time.now.to_f
if !send("fetch_#{arg}")
dependencies << arg
end
Rails.logger.info "view model thread #{arg} finished (#{"%.1f" % ((Time.now.to_f - t1) * 1000)}ms)"
end
end
end
threads.each do |t|
t.join
end
threads = []
while dependencies.size > 0
d = []
dependencies.each do |arg|
if !send("fetch_#{arg}")
d << arg
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
Rails.logger.info "view model thread #{arg} start"
t1 = Time.now.to_f
if !send("fetch_#{arg}")
d << arg
end
Rails.logger.info "view model thread #{arg} finished (#{"%.1f" % ((Time.now.to_f - t1) * 1000)}ms)"
end
end
end
threads.each do |t|
t.join
end
threads = []
dependencies = d
end

Rails.logger.info "view model finalized"
Rails.logger.info "view model finalized (#{"%.1f" % ((Time.now.to_f - t) * 1000)}ms)"
end

end
3 changes: 3 additions & 0 deletions test/integration/api/api_channels_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApiChannelsTest < ActionDispatch::IntegrationTest
c = create_channel(title)
get '/api/channels.json'
j = json_body
puts j.inspect
jc = j['channels'].first
assert_equal c.id, jc['id']
assert_equal c.title, jc['title']
Expand All @@ -26,6 +27,7 @@ class ApiChannelsTest < ActionDispatch::IntegrationTest
title = "Test Channel #{Time.now.to_f}"
post '/api/channels.json', {channel: {title: title, body: "Testing"}}
j = json_body
puts j.inspect
assert_nil json_body['errors']
jc = j['channel']
assert_not_nil jc['id']
Expand All @@ -46,6 +48,7 @@ class ApiChannelsTest < ActionDispatch::IntegrationTest
title2 = "FooTest Channel #{Time.now.to_f}"
put "/api/channels/#{c.id}.json", {channel: {title: title2, text: "Channel text"}}
j = json_body
puts j.inspect
jc = j['channel']
assert_equal title2, jc['title']
assert_equal "Channel text", jc['text']
Expand Down
1 change: 1 addition & 0 deletions test/integration/api/api_posts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ApiPostsTest < ActionDispatch::IntegrationTest
p = create_post("Post")
get "/api/channels/#{c.id}/posts.json"
j = json_body
puts j.inspect
jc = j['channel']
assert_equal c.id, jc['id']
jp = j['posts'].last
Expand Down
1 change: 1 addition & 0 deletions test/unit/view_model_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'

class ViewModelTest < ActiveSupport::TestCase
self.use_transactional_fixtures = false

def setup
create_user
Expand Down