Skip to content

Commit

Permalink
people maps (#1953)
Browse files Browse the repository at this point in the history
* people maps

* helper

* tests for people map

* small fix

* Update node_shared.rb

* Update node_shared.rb

* fixes!

* fixed!

* Update node_shared_test.rb

* add your own location link

* defined
  • Loading branch information
jywarren authored Jan 12, 2018
1 parent a63f102 commit 69ff570
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 37 deletions.
78 changes: 41 additions & 37 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,48 @@ def list
end

def profile
@user = DrupalUser.find_by(name: params[:id])
@profile_user = User.find_by(username: params[:id])
@title = @user.name
@notes = Node.research_notes
.page(params[:page])
.order("nid DESC")
.where(status: 1, uid: @user.uid)
@coauthored = @profile_user.coauthored_notes
.page(params[:page])
.order('node_revisions.timestamp DESC')
@questions = @user.user.questions
.order('node.nid DESC')
.paginate(:page => params[:page], per_page: 24)
questions = Node.questions
.where(status: 1)
.order('node.nid DESC')
@answered_questions = questions.select{|q| q.answers.collect(&:author).include?(@user)}
wikis = Revision.order("nid DESC")
.where('node.type' => 'page', 'node.status' => 1, uid: @user.uid)
.joins(:node)
.limit(20)
@wikis = wikis.collect(&:parent).uniq

# User's social links
@github = @profile_user.social_link("github")
@twitter = @profile_user.social_link("twitter")
@facebook = @profile_user.social_link("facebook")
@instagram = @profile_user.social_link("instagram")

if @user.status == 0
if current_user && (current_user.role == "admin" || current_user.role == "moderator")
flash.now[:error] = I18n.t('users_controller.user_has_been_banned')
else
flash[:error] = I18n.t('users_controller.user_has_been_banned')
redirect_to "/"
if current_user && params[:id].nil?
redirect_to "/profile/#{current_user.username}"
else
@user = DrupalUser.find_by(name: params[:id])
@profile_user = User.find_by(username: params[:id])
@title = @user.name
@notes = Node.research_notes
.page(params[:page])
.order("nid DESC")
.where(status: 1, uid: @user.uid)
@coauthored = @profile_user.coauthored_notes
.page(params[:page])
.order('node_revisions.timestamp DESC')
@questions = @user.user.questions
.order('node.nid DESC')
.paginate(:page => params[:page], :per_page => 24)
questions = Node.questions
.where(status: 1)
.order('node.nid DESC')
@answered_questions = questions.select{|q| q.answers.collect(&:author).include?(@user)}
wikis = Revision.order("nid DESC")
.where('node.type' => 'page', 'node.status' => 1, uid: @user.uid)
.joins(:node)
.limit(20)
@wikis = wikis.collect(&:parent).uniq

# User's social links
@github = @profile_user.social_link("github")
@twitter = @profile_user.social_link("twitter")
@facebook = @profile_user.social_link("facebook")
@instagram = @profile_user.social_link("instagram")

if @user.status == 0
if current_user && (current_user.role == "admin" || current_user.role == "moderator")
flash.now[:error] = I18n.t('users_controller.user_has_been_banned')
else
flash[:error] = I18n.t('users_controller.user_has_been_banned')
redirect_to "/"
end
elsif @user.status == 5
flash.now[:warning] = I18n.t('users_controller.user_has_been_moderated')
end
elsif @user.status == 5
flash.now[:warning] = I18n.t('users_controller.user_has_been_moderated')
end
end

Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def insert_extras(body)
body = NodeShared.notes_map(body)
body = NodeShared.notes_map_by_tag(body)
body = NodeShared.people_grid(body)
body = NodeShared.people_map(body)
body = NodeShared.graph_grid(body)
body = NodeShared.wikis_grid(body)
body
Expand Down
26 changes: 26 additions & 0 deletions app/models/concerns/node_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,32 @@ def self.notes_map_by_tag(body)
end
end

# in our interface, "users" are known as "people" because it's more human
def self.people_map(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[map\:people\:(\S+)\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
nids = nids || []
users = User.where(status: 1)
.includes(:user_tags)
.references(:user_tags)
.where('user_tags.value LIKE ?', 'lat:' + lat[0..lat.length - 2] + '%')
a = ActionController::Base.new()
output = a.render_to_string(template: "map/_leaflet",
layout: false,
locals: {
lat: lat,
lon: lon,
items: users,
people: true
}
)
output
end
end


# in our interface, "users" are known as "people" because it's more human
def self.people_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[people\:(\S+)\]/) do |_tagname|
Expand Down
1 change: 1 addition & 0 deletions app/views/map/_leaflet.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#map<%= unique_id %> { width:100%; height:300px; margin: 0; position: relative;}
</style>
<div class="leaflet-map" id="map<%= unique_id %>"></div>
<% if defined? people %><p><i><small>Share your own location on <a href='/profile'>your profile</a>.</small></i></p><% end %>
<script>
var map = L.map('map<%= unique_id %>').setView([<%= lat %>,<%= lon %>], <%= lat.to_s.length.to_i %> + 6);
L.tileLayer("//a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(map);
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
get 'profile/edit' => 'users#edit'
post 'profile/photo' => 'users#photo'
get 'profile/info/:id' => 'users#info', as: 'info'
get 'profile' => 'users#profile'
get 'profile/:id' => 'users#profile'
get 'profile/:id/edit' => 'users#edit'
get 'profile/:id/likes' => 'users#likes'
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/user_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ three:
id: 3
uid: 2
value: skill:rails

latitude:
id: 4
uid: 1
value: lat:41

longitude:
id: 5
uid: 1
value: lon:-90
6 changes: 6 additions & 0 deletions test/unit/node_shared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ class NodeSharedTest < ActiveSupport::TestCase
assert_equal 1, html.scan('L.marker').length
end

test 'that NodeShared can be used to convert short codes like [map:people:___:___] into maps which display peoples locations' do
before = "Here are some people in a map: \n\n[map:people:41.00:-90.01] \n\nThis is how you make it work:\n\n`[map:people:41:-90]`\n\nMake sense?"
html = NodeShared.people_map(before)
assert_equal 1, html.scan('<div class="leaflet-map"').length
end

test 'that NodeShared can be used to convert short codes like [people:organizer] into maps which display notes, but only those tagged with "organizer"' do
before = "Here are some people in a grid: \n\n[people:organizer] \n\nThis is how you make it work:\n\n`[people:organizer]`\n\nMake sense?"
html = NodeShared.people_grid(before)
Expand Down

0 comments on commit 69ff570

Please sign in to comment.