Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
damingo committed May 13, 2018
1 parent 629b3e4 commit e738011
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 72 deletions.
Expand Up @@ -2,7 +2,6 @@
#
class Administration::AnnotatorStore::AnnotationsController < Administration::ApplicationController


def index
scope = scoped_resource
scope = scope.where(post_id: ::Topic.find(params[:topic_id]).try(:post_ids)) if params[:topic_id].present?
Expand All @@ -26,9 +25,11 @@ def index
resources = resources.includes(*resource_includes) if resource_includes.any?
resources = order.apply(resources)
resources = resources.page(params[:page]).per(records_per_page)
# page = Administrate::Page::Collection.new(dashboard, order: order)
page = Administrate::Page::Collection.new(dashboard, order: order)

respond_to do |format|
format.html { render locals: {resources: resources, search_term: search_term, page: page, show_search_bar: show_search_bar?} }

format.json {
# Rename tag_id to code_id
r = resources.to_a.map(&:attributes).each { |a| a['code_id'] = a.delete('tag_id') }
Expand Down
@@ -1,10 +1,6 @@
class Administration::AnnotatorStore::CollectionsController < Administration::ApplicationController


def namespace
'administration_annotator_store'
end

def create
resource = resource_class.new(resource_params)
resource.creator = current_user
Expand Down
16 changes: 10 additions & 6 deletions app/controllers/administration/annotator_store/tags_controller.rb
Expand Up @@ -21,16 +21,16 @@ def index
page = Administrate::Page::Collection.new(dashboard)

respond_to do |format|
format.html { render locals: {resources: resources, search_term: search_term, page: page, show_search_bar: show_search_bar?} }
format.json { render json: JSON.pretty_generate(JSON.parse(resources.to_json)) }
format.html {render locals: {resources: resources, search_term: search_term, page: page, show_search_bar: show_search_bar?}}
format.json {render json: JSON.pretty_generate(JSON.parse(resources.to_json))}
end
end


def show
respond_to do |format|
format.html { render locals: {page: Administrate::Page::Show.new(dashboard, requested_resource)} }
format.json { render json: JSON.pretty_generate(JSON.parse(requested_resource.to_json)) }
format.html {render locals: {page: Administrate::Page::Show.new(dashboard, requested_resource)}}
format.json {render json: JSON.pretty_generate(JSON.parse(requested_resource.to_json))}
end
end

Expand All @@ -40,7 +40,7 @@ def create
resource.creator = current_user

if resource.save
redirect_to [namespace, :annotator_store, resource], notice: 'Code was successfully created.'
redirect_to [namespace, resource], notice: 'Code was successfully created.'
else
render :new, locals: {page: Administrate::Page::Form.new(dashboard, resource)}
end
Expand All @@ -49,7 +49,11 @@ def create

def update
if requested_resource.update(resource_params)
redirect_to [namespace, :annotator_store, requested_resource], notice: 'Code was successfully updated.'
if resource_params.include?(:merge_tag_id)
redirect_to administration_annotator_store_tags_path, notice: 'Codes were successfully merged.'
else
redirect_to [namespace, requested_resource], notice: 'Code was successfully updated.'
end
else
render :edit, locals: {page: Administrate::Page::Form.new(dashboard, requested_resource)}
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/administration/application_controller.rb
Expand Up @@ -13,6 +13,11 @@ class Administration::ApplicationController < Administrate::ApplicationControlle
before_filter :ensure_staff


def namespace
'administration_annotator_store'
end


private

def ensure_logged_in
Expand Down
26 changes: 13 additions & 13 deletions app/dashboards/annotator_store/annotation_dashboard.rb
Expand Up @@ -14,7 +14,7 @@ class AnnotationDashboard < Administrate::BaseDashboard
id: Field::Number,
uri: Field::String,
tag_id: Field::Number,
quote: Field::Text,
quote: Field::Text.with_options(truncate: 1000),
creator_id: Field::Number,
created_at: Field::DateTime,
updated_at: Field::DateTime,
Expand All @@ -27,22 +27,22 @@ class AnnotationDashboard < Administrate::BaseDashboard
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = [
:id,
# :name,
# :creator,
:quote,
:tag,
:creator,
].freeze

# # SHOW_PAGE_ATTRIBUTES
# # an array of attributes that will be displayed on the model's show page.
# SHOW_PAGE_ATTRIBUTES = [
# :id,
# :parent,
# :name,
# :description,
# :creator,
# :created_at,
# :updated_at,
# ].freeze
#
SHOW_PAGE_ATTRIBUTES = [
:id,
:quote,
:tag,
:creator,
:created_at,
:updated_at,
].freeze

# # FORM_ATTRIBUTES
# # an array of attributes that will be displayed
# # on the model's form (`new` and `edit`) pages.
Expand Down
2 changes: 2 additions & 0 deletions app/dashboards/annotator_store/tag_dashboard.rb
Expand Up @@ -11,6 +11,7 @@ class TagDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
creator: Administrate::UserField,
parent: Administrate::ParentTagField.with_options(class_name: 'AnnotatorStore::Tag'),
merge_tag: Administrate::MergeTagField.with_options(class_name: 'AnnotatorStore::Tag'),
id: Field::Number,
name: Field::String,
description: Field::Text,
Expand Down Expand Up @@ -50,6 +51,7 @@ class TagDashboard < Administrate::BaseDashboard
:parent,
:name,
:description,
:merge_tag,
# :creator,
# :parent,
# :creator_id,
Expand Down
8 changes: 8 additions & 0 deletions app/fields/administrate/merge_tag_field.rb
@@ -0,0 +1,8 @@
require "administrate/field/belongs_to"

module Administrate
class MergeTagField < Administrate::Field::BelongsTo


end
end
2 changes: 1 addition & 1 deletion app/helpers/administration_helper.rb
Expand Up @@ -10,7 +10,7 @@ module AdministrationHelper
def nested_dropdown(items)
result = []
items.map do |item, sub_items|
result << [('- ' * item.depth) + item.name, item.id]
result << [('- ' * item.depth) + item.name_with_count, item.id]
result += nested_dropdown(sub_items) unless sub_items.blank?
end
result
Expand Down
@@ -0,0 +1,78 @@
<%#
# Index
This view is the template for the index page.
It is responsible for rendering the search bar, header and pagination.
It renders the `_table` partial to display details about the resources.
## Local variables:
- `page`:
An instance of [Administrate::Page::Collection][1].
Contains helper methods to help display a table,
and knows which attributes should be displayed in the resource's table.
- `resources`:
An instance of `ActiveRecord::Relation` containing the resources
that match the user's search criteria.
By default, these resources are passed to the table partial to be displayed.
- `search_term`:
A string containing the term the user has searched for, if any.
- `show_search_bar`:
A boolean that determines if the search bar should be shown.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>
<% content_for(:title) do %>
Annotations
<% if params[:code_id].present? %>
> <%= AnnotatorStore::Tag.find_by(id: params[:code_id]).name %>
<% end %>
<% end %>


<header class="main-content__header" role="banner">
<h1 class="main-content__page-title" id="page-title">
<%= content_for(:title) %>
</h1>

<% if show_search_bar %>
<%= render(
'search',
search_term: search_term,
resource_name: 'Annotations'
) %>
<% end %>
</header>

<section class="main-content__body main-content__body--flush">
<% if params[:search].present? %>
<%= render("collection", collection_presenter: page, resources: resources, table_title: "page-title") %>
<% else %>
<style type="text/css">
.filter-nav {
margin: 0 0 20px 40px;
}

.filter-nav a {
margin-right: 10px;
font-size: 80%;
}

.filter-nav a.active {
color: #333;
text-decoration: none;
}
</style>

<div class="filter-nav">
<%= link_to 'Any Creator', params.except(:page).deep_merge(creator_id: nil), class: (params[:creator_id].present? ? '' : 'active') %>
<% User.where(id: AnnotatorStore::Annotation.select(:creator_id).uniq.pluck(:creator_id)).order(:name).each do |model| %>
<%= link_to "by #{model.username}", params.except(:page).deep_merge(creator_id: model.id), class: (params[:creator_id] == model.id.to_s ? 'active' : '') %>
<% end %>
</div>

<%= render("collection", collection_presenter: page, resources: resources, table_title: "page-title") %>
<% end %>
<%= paginate resources %>
</section>
Expand Up @@ -58,13 +58,13 @@ to display a collection of resources in an HTML table.
<tr class="js-table-row"
tabindex="0"
<% if valid_action? :show, collection_presenter.resource_name %>
<%= %(role=link data-url=#{polymorphic_path([namespace, :annotator_store, resource])}) %>
<%= %(role=link data-url=#{polymorphic_path([namespace, resource])}) %>
<% end %>
>
<% collection_presenter.attributes_for(resource).each do |attribute| %>
<td class="cell-data cell-data--<%= attribute.html_class %>">
<div style="<%= "padding-left:#{params[:search].blank? ? (20 * resource.depth) : 0 }px;" if attribute.name == 'name' %>">
<a href="<%= polymorphic_path([namespace, :annotator_store, resource]) -%>" class="action-show">
<a href="<%= polymorphic_path([namespace, resource]) -%>" class="action-show">
<%= render_field attribute %>
</a>
</div>
Expand All @@ -74,15 +74,15 @@ to display a collection of resources in an HTML table.
<% if valid_action? :edit, collection_presenter.resource_name %>
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, :annotator_store, resource],
[:edit, namespace, resource],
class: "action-edit",
) %></td>
<% end %>
<% if valid_action? :destroy, collection_presenter.resource_name %>
<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, :annotator_store, resource],
[namespace, resource],
class: "text-color-red",
method: :delete,
data: {confirm: t("administrate.actions.confirm")}
Expand Down
52 changes: 28 additions & 24 deletions app/views/administration/annotator_store/tags/_form.html.erb
Expand Up @@ -14,29 +14,33 @@ and renders all form fields for a resource's editable attributes.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
%>
<%= form_for([namespace, :annotator_store, page.resource], html: {class: "form"}) do |f| %>
<% if page.resource.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(page.resource.errors.count, "error") %>
prohibited this <%= page.resource_name %> from being saved:
</h2>

<ul>
<% page.resource.errors.full_messages.each do |message| %>
<li class="flash-error"><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% page.attributes.each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: f %>
</div>
<% end -%>

<div class="form-actions">
<%= f.submit 'Save' %>
<% content_for(:javascript) do %>
<% end %>
<%= form_for([namespace, page.resource], html: {class: "form"}) do |f| %>
<% if page.resource.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(page.resource.errors.count, "error") %>
prohibited this <%= page.resource_name %> from being saved:
</h2>

<ul>
<% page.resource.errors.full_messages.each do |message| %>
<li class="flash-error"><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% page.attributes.each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: f, resource: page.resource %>
</div>
<% end -%>

<div class="form-actions">
<%= f.submit 'Save' %>
</div>
<% end %>
21 changes: 17 additions & 4 deletions app/views/administration/annotator_store/tags/_tree_item.html.erb
Expand Up @@ -3,28 +3,41 @@
<% if tag.children.any? %>
<i class="fa fa-minus-circle" style="color:#aaa;"></i>
<% end %>
<strong class="item" title="<%= tag.annotations.count %> annotations use this tag"><%= tag.name %> (<%= tag.annotations.count %>)</strong>
<strong class="item" title="<%= tag.annotations.count %> annotations use this code"><%= tag.name_with_count %></strong>
</span>
<% unless params[:creator_id].present? %>
by <%= tag.creator.username %>&nbsp;&nbsp;
<% end %>
<%= link_to(
t('administrate.actions.show'),
[namespace, :annotator_store, tag],
[namespace, tag],
class: "action-edit",
) %> |

<%= link_to(
t('administrate.actions.edit'),
[:edit, namespace, :annotator_store, tag],
[:edit, namespace, tag],
class: "action-edit",
) %> |

<%= link_to(
'Annotations',
administration_annotator_store_annotations_path(code_id: tag.id),
class: "action-edit",
) %>

|
<%= link_to(
'Merge',
edit_administration_annotator_store_tag_path(tag.id, merge: true),
class: "action-edit",
) %>
<% if tag.is_childless? %>
|
<%= link_to(
t('administrate.actions.destroy'),
[namespace, :annotator_store, tag],
[namespace, tag],
class: "text-color-red",
method: :delete,
data: {confirm: "Are you sure? This tag has #{tag.annotations.count} annotations which will also be deleted."}
Expand Down

0 comments on commit e738011

Please sign in to comment.