Skip to content

Commit

Permalink
Merge pull request #24 from public-accountability/list-creator
Browse files Browse the repository at this point in the history
List creator
  • Loading branch information
aepyornis committed Mar 20, 2017
2 parents 8b3c440 + 1d87077 commit add4f89
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Ignore database configuration
/config/database.yml
/db/structure.sql

# Ignore secret configuration
/config/lilsis.yml
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def groups
def dashboard
@maps = current_user.network_maps.order("created_at DESC, id DESC")
@groups = current_user.groups.includes(:campaign).order(:name)
@lists = current_user.lists.order("created_at DESC, id DESC")
@recent_updates = current_user.edited_entities.includes(last_user: :user).order("updated_at DESC").limit(10)
end

Expand All @@ -53,6 +54,18 @@ def maps
render 'maps/index'
end

def lists
@lists = current_user.lists
.select("ls_list.*, COUNT(DISTINCT(ls_list_entity.entity_id)) AS entity_count")
.joins(:list_entities)
.where(is_network: false, is_admin: false)
.group("ls_list.id")
.order("entity_count DESC")
.page(params[:page]).per(20)

render 'lists/index'
end

def index
redirect_to_dashboard_if_signed_in unless request.env['PATH_INFO'] == '/home'
@dots_connected = dots_connected
Expand Down
13 changes: 11 additions & 2 deletions app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def self.get_lists(page)

# GET /lists
def index
@lists = self.class.get_lists(params[:page])
lists = self.class.get_lists(params[:page])

if current_user.present?
@lists = lists.where('ls_list.is_private = ? OR ls_list.creator_user_id = ?', false, current_user.id)
else
@lists = lists.public_scope
end

if params[:q].present?
is_admin = (current_user and current_user.has_legacy_permission('admin')) ? [0, 1] : 0
Expand Down Expand Up @@ -44,6 +50,9 @@ def edit
# POST /lists
def create
@list = List.new(list_params)
@list.creator_user_id = current_user.id
@list.last_user_id = current_user.sf_guard_user_id

if params[:ref][:source].blank?
@list.errors.add_on_blank(:name)
@list.errors[:base] << "A source URL is required"
Expand Down Expand Up @@ -251,7 +260,7 @@ def set_list

# Only allow a trusted parameter "white list" through.
def list_params
params.require(:list).permit(:name, :description, :is_ranked, :is_admin, :is_featured, :custom_field_name)
params.require(:list).permit(:name, :description, :is_ranked, :is_admin, :is_featured, :is_private, :custom_field_name)
end

def interlocks_results(options)
Expand Down
5 changes: 5 additions & 0 deletions app/models/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class List < ActiveRecord::Base

has_paper_trail

belongs_to :user, foreign_key: "creator_user_id", inverse_of: :lists

has_many :list_entities, inverse_of: :list, dependent: :destroy
has_many :entities, through: :list_entities
has_many :images, through: :entities
Expand All @@ -33,6 +35,9 @@ class List < ActiveRecord::Base

validates_presence_of :name

scope :public_scope, -> { where(is_private: false) }
scope :private_scope, -> { where(is_private: true) }

def destroy
soft_delete
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class User < ActiveRecord::Base
has_many :received_notes, class_name: "Note", through: :note_users, source: :note, inverse_of: :recipients
has_many :network_maps, primary_key: "sf_guard_user_id"

has_many :lists, foreign_key: "creator_user_id", inverse_of: :user

has_one :api_token

def to_param
Expand Down
19 changes: 19 additions & 0 deletions app/views/home/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@
</div>
</div> <!-- end-row -->

<div class="row">
<div class="col-sm-10 col-md-8">

<h3>
Your Lists
<%= header_action('details', home_lists_path) %>
</h3>

<% if @lists.present? %>
<% @lists.each do |list| %>
<%= list_link(list) %><br />
<% end %>
<% end %>

</div>
</div> <!-- end-row -->

<div class="row">
<div class="col-sm-10 col-md-8">

Expand All @@ -108,3 +125,5 @@

</div>
</div> <!-- end-row -->


9 changes: 9 additions & 0 deletions app/views/lists/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
</div>
</div>

<% if @list.creator_user_id == current_user.id %>
<div class="row form-group">
<div class="col-sm-8 col-md-6">
<%= f.check_box :is_private %>
<%= f.label :is_private %>
</div>
</div>
<% end %>
<% if current_user.has_legacy_permission("admin") %>
<div class="row form-group">
<div class="col-sm-8 col-md-6">
Expand Down
5 changes: 5 additions & 0 deletions app/views/lists/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@

<%= f.label(:is_featured, "is featured") %>
<%= f.check_box :is_featured %>

<br />

<%= f.label(:is_private, "is private") %>
<%= f.check_box :is_private %>
</div>

<%= fields_for :ref do |ref_fields| %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
get "/home/notes" => "home#notes"
get "/home/groups" => "home#groups"
get "/home/maps" => "home#maps"
get "/home/lists" => "home#lists"
get "/home/dashboard" => "home#dashboard"
post "/home/dismiss",
controller: 'home',
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170227163755_add_private_option_to_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPrivateOptionToLists < ActiveRecord::Migration
def change
add_column :ls_list, :is_private, :boolean, default: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170227190903_add_creator_user_id_to_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCreatorUserIdToLists < ActiveRecord::Migration
def change
add_column :ls_list, :creator_user_id, :integer
end
end
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,8 @@
t.boolean "is_deleted", default: false, null: false
t.string "custom_field_name", limit: 100
t.boolean "delta", default: true, null: false
t.boolean "is_private", default: false
t.integer "creator_user_id", limit: 4
end

add_index "ls_list", ["delta"], name: "index_ls_list_on_delta", using: :btree
Expand Down
Loading

0 comments on commit add4f89

Please sign in to comment.