-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'adding-navigation-feature'
- Loading branch information
Showing
37 changed files
with
776 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
backend/app/assets/javascripts/comable/admin/navigations.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class @Navigation | ||
constructor: -> | ||
$(document).ready(@ready) | ||
@add_event() | ||
|
||
ready: => | ||
@navigation_items = $('#navigation-items') | ||
@add_fields = $('.add_fields') | ||
|
||
# linkable_idの検索 | ||
search_linkable_ids: -> | ||
$linkable_type = $('#linkable_type') | ||
$position = $('#position') | ||
$linkable_type.val($(this).val()) | ||
$position.val($('.linkable_type').index(this)) | ||
$linkable_type.closest('form').submit() | ||
|
||
# アイテムの追加 | ||
adding_navigation_item_field: -> | ||
regexp = new RegExp($(this).data('index'), 'g') | ||
field_tags = $(this).data('fields').replace(regexp, $('.navigation-item').length) # 置換予定文字を添字に置換する | ||
$('#navigation-items').append(field_tags) # タグを追加 | ||
|
||
# アイテムの削除 | ||
remove_navigation_item_field: -> | ||
$(this).parent().prev('.destroy').val(true) | ||
$(this).closest('fieldset').hide() | ||
|
||
# イベント設定 | ||
add_event: -> | ||
@navigation_items.on('change', '.linkable_type', @search_linkable_ids) | ||
@navigation_items.on('click', '.remove_fields', @remove_navigation_item_field) | ||
@add_fields.click(@adding_navigation_item_field) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
backend/app/assets/stylesheets/comable/admin/_navigations.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.add_fields { | ||
margin-right: 30px; | ||
} | ||
|
||
.remove_fields { | ||
margin-top: 25px; | ||
} | ||
|
||
.linkable_id select { | ||
margin-top: 25px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
backend/app/controllers/comable/admin/navigations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require_dependency 'comable/admin/application_controller' | ||
|
||
module Comable | ||
module Admin | ||
class NavigationsController < Comable::Admin::ApplicationController | ||
load_and_authorize_resource class: Comable::Navigation.name, except: :index | ||
|
||
def index | ||
@q = Comable::Navigation.ransack(params[:q]) | ||
@navigations = @q.result.accessible_by(current_ability) | ||
end | ||
|
||
def show | ||
edit | ||
render :edit | ||
end | ||
|
||
def new | ||
@navigation.navigation_items.build | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@navigation = Comable::Navigation.new(navigation_params) | ||
if @navigation.save | ||
redirect_to comable.admin_navigation_path(@navigation), notice: Comable.t('successful') | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def update | ||
@navigation.attributes = navigation_params | ||
if @navigation.save | ||
redirect_to comable.admin_navigation_path(@navigation), notice: Comable.t('successful') | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@navigation.destroy | ||
redirect_to comable.admin_navigations_path, notice: Comable.t('successful') | ||
end | ||
|
||
def search_linkable_ids | ||
@linkable_id_options = linkable_id_options | ||
render layout: false | ||
end | ||
|
||
private | ||
|
||
def linkable_type | ||
return if params[:linkable_type].blank? | ||
params[:linkable_type] if Comable.const_defined?(params[:linkable_type].demodulize) | ||
end | ||
|
||
def linkable_id_options | ||
linkable_type ? linkable_type.constantize.linkable_id_options : [[]] | ||
end | ||
|
||
def navigation_params | ||
params.require(:navigation).permit( | ||
:name, | ||
navigation_items_attributes: navigation_items_attributes_keys | ||
) | ||
end | ||
|
||
def navigation_items_attributes_keys | ||
[ | ||
:id, | ||
:position, | ||
:linkable_id, | ||
:linkable_type, | ||
:name, | ||
:url, | ||
:_destroy | ||
] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Comable | ||
module Admin | ||
module NavigationsHelper | ||
def linkable_type_options | ||
Comable::NavigationItem.linkable_params_lists.map { |attr| attr.slice(:name, :type).values } | ||
end | ||
|
||
# アイテム追加ボタン設置 | ||
def add_fields_button_tag(name, f, association) | ||
new_object = f.object.send(association).klass.new | ||
index = new_object.object_id # 後で置換するために必要な文字を入れる | ||
fields = f.fields_for(association, new_object, child_index: index) do |builder| | ||
render(association.to_s.singularize + '_fields', f: builder) | ||
end | ||
button_tag(name, type: :button, class: 'add_fields btn btn-default pull-right', data: { index: index, fields: fields.gsub("\n", '') }) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
= error_messages_for @navigation | ||
|
||
- url = @navigation.new_record? ? comable.admin_navigations_path : comable.admin_navigation_path(@navigation) | ||
|
||
= form_for @navigation, url: url do |f| | ||
.hidden | ||
= f.submit | ||
|
||
fieldset | ||
.col-sm-12 | ||
.form-group | ||
= f.label :name | ||
= f.text_field :name | ||
hr | ||
|
||
#navigation-items= f.fields_for :navigation_items do |navigation_items_form| | ||
= render 'navigation_item_fields', f: navigation_items_form | ||
|
||
hr | ||
|
||
.row | ||
= add_fields_button_tag Comable.t('admin.nav.navigation_items.add_link'), f, :navigation_items | ||
|
||
.hidden | ||
= form_tag(comable.search_linkable_ids_admin_navigations_path, remote: true) do | ||
= text_field_tag :linkable_type | ||
= text_field_tag :position | ||
= submit_tag |
24 changes: 24 additions & 0 deletions
24
backend/app/views/comable/admin/navigations/_navigation_item_fields.slim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
fieldset.navigation-item class="#{'hidden' if f.object._destroy }" | ||
.col-sm-12 | ||
.form-group | ||
.row | ||
.col-sm-3 | ||
= f.label :name | ||
= f.text_field :name | ||
|
||
.col-sm-3 | ||
= f.label :linkable_type | ||
= f.select :linkable_type, linkable_type_options, {}, class: :linkable_type | ||
|
||
.col-sm-3.url style="#{ 'display: none;' if f.object.linkable_type? && f.object.linkable_type.constantize.linkable_exists?}" | ||
= f.label :url | ||
= f.text_field :url | ||
|
||
.col-sm-3.linkable_id style="#{ 'display: none;' unless f.object.linkable_type? && f.object.linkable_type.constantize.linkable_exists?}" | ||
= f.select :linkable_id, f.object.linkable_type? ? f.object.linkable_type.constantize.linkable_id_options : [[]] | ||
|
||
= f.hidden_field :_destroy, class: :destroy | ||
|
||
.col-sm-3 | ||
button.btn.btn-default.pull-right.remove_fields type="button" | ||
= Comable.t('actions.destroy') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
.comable-page | ||
.comable-main-fixed-top | ||
.comable-page-heading | ||
ul.pull-right.list-inline | ||
li | ||
= link_to_save | ||
|
||
h1.page-header | ||
ol.breadcrumb | ||
li> | ||
= link_to Comable.t('admin.nav.navigation'), comable.admin_navigations_path | ||
li.active | ||
= @navigation.name | ||
|
||
.comable-page-body | ||
= render 'form' | ||
hr | ||
.panel.panel-danger | ||
.panel-heading type="button" data-toggle="collapse" data-target="#comable-danger" | ||
strong | ||
span.fa.fa-angle-down> | ||
= Comable.t('admin.actions.destroy') | ||
#comable-danger.collapse | ||
.panel-body | ||
p | ||
= Comable.t('admin.confirmation_to_remove_navigation') | ||
= link_to Comable.t('admin.actions.destroy'), comable.admin_navigation_path(@navigation), method: :delete, class: 'btn btn-danger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.comable-page | ||
.comable-page-heading | ||
ul.pull-right.list-inline | ||
li | ||
= link_to Comable.t('admin.actions.new'), comable.new_admin_navigation_path, class: 'btn btn-default' | ||
h1.page-header | ||
= Comable.t('admin.nav.navigation') | ||
small< | ||
| #{@navigations.size} #{Comable.t('admin.results')} | ||
|
||
section | ||
- if @navigations.empty? | ||
= Comable.t('admin.not_found') | ||
- else | ||
table.table.table-striped | ||
thead | ||
th | ||
= sort_link [:comable, @q], :name | ||
|
||
tbody | ||
- @navigations.each do |navigation| | ||
tr | ||
td | ||
= link_to navigation.name, comable.admin_navigation_path(navigation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.comable-page | ||
.comable-main-fixed-top | ||
.comable-page-heading | ||
ul.pull-right.list-inline | ||
li | ||
= link_to_save | ||
|
||
h1.page-header | ||
ol.breadcrumb | ||
li> | ||
= link_to Comable.t('admin.nav.navigation'), comable.admin_navigations_path | ||
li.active | ||
= Comable.t('admin.actions.new') | ||
|
||
.comable-page-body | ||
= render 'form' |
15 changes: 15 additions & 0 deletions
15
backend/app/views/comable/admin/navigations/search_linkable_ids.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
$url = $('#navigation_navigation_items_attributes_<%= params[:position] %>_url') | ||
$linkable_id = $('#navigation_navigation_items_attributes_<%= params[:position] %>_linkable_id') | ||
|
||
$linkable_id.html('<%= options_for_select(@linkable_id_options) %>') | ||
|
||
if <%= params[:linkable_type].blank? %> | ||
# アドレスを入力する場合 | ||
$linkable_id.closest('.linkable_id').hide() | ||
$linkable_id.val('') | ||
$url.closest('.url').show() | ||
|
||
else | ||
$url.closest('.url').hide() | ||
$url.val('') | ||
$linkable_id.closest('.linkable_id').show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.