Skip to content

Commit

Permalink
Merge branch 'adding-navigation-feature'
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Aug 17, 2015
2 parents 52c481f + 7924309 commit 5f2e6e0
Show file tree
Hide file tree
Showing 37 changed files with 776 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class Dispatcher
action_name = path[1]

switch page
when 'orders:edit'
when 'orders:edit', 'pages:update'
new DynamicOrder
when 'pages:new', 'pages:show', 'pages:edit'
when 'pages:new', 'pages:show', 'pages:edit', 'pages:update', 'pages:create'
new Page
when 'navigations:new', 'navigations:show', 'navigations:edit', 'navigations:update', 'navigations:create'
new Navigation
34 changes: 34 additions & 0 deletions backend/app/assets/javascripts/comable/admin/navigations.coffee
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)

6 changes: 3 additions & 3 deletions backend/app/assets/javascripts/comable/admin/pages.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
class @Page
constructor: ->
$(document).ready(@ready)
@add_event_to_set_visibility()
@add_event_to_set_page_title()
@add_event_to_set_meta_description()

ready: =>
@radio_published = $('#page_published_at_published')
@radio_unpublished = $('#page_published_at_unpublished')
@published_at = $('#page_published_at')

@initialize_visibility()
@add_event_to_set_visibility()
@add_event_to_set_page_title()
@add_event_to_set_meta_description()

# 公開/非公開の制御
initialize_visibility: ->
Expand Down
11 changes: 11 additions & 0 deletions backend/app/assets/stylesheets/comable/admin/_navigations.scss
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
@import 'comable/admin/orders';
@import 'comable/admin/products';
@import 'comable/admin/pages';
@import 'comable/admin/navigations';
@import 'comable/admin/user_sessions';
@import 'comable/admin/themes';
84 changes: 84 additions & 0 deletions backend/app/controllers/comable/admin/navigations_controller.rb
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
19 changes: 19 additions & 0 deletions backend/app/helpers/comable/admin/navigations_helper.rb
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
4 changes: 4 additions & 0 deletions backend/app/navigations/comable/admin/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
link comable.admin_pages_path
end

item Comable.t('admin.nav.navigation') do
link comable.admin_navigations_path
end

divider

item Comable.t('admin.nav.shipment_method') do
Expand Down
28 changes: 28 additions & 0 deletions backend/app/views/comable/admin/navigations/_form.slim
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
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')
27 changes: 27 additions & 0 deletions backend/app/views/comable/admin/navigations/edit.slim
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'
24 changes: 24 additions & 0 deletions backend/app/views/comable/admin/navigations/index.slim
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)
16 changes: 16 additions & 0 deletions backend/app/views/comable/admin/navigations/new.slim
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'
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()
5 changes: 5 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@

resources :categories
resources :pages

resources :navigations do
post :search_linkable_ids, on: :collection
end

resources :users
resources :shipment_methods
resources :payment_methods
Expand Down
Loading

0 comments on commit 5f2e6e0

Please sign in to comment.