Skip to content

Commit

Permalink
Merge branch 'master' into improve-devise-routing
Browse files Browse the repository at this point in the history
Conflicts:
    core/spec/helpers/comable/application_helper_spec.rb
  • Loading branch information
YOSHIDA Hiroki committed Aug 28, 2015
2 parents 86fa976 + 8718828 commit 48e36ec
Show file tree
Hide file tree
Showing 46 changed files with 944 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ 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 'products:new', 'products:show', 'products:edit', 'products:update', 'products:create'
new Product
when 'navigations:new', 'navigations:show', 'navigations:edit', 'navigations:update', 'navigations:create'
new Navigation
40 changes: 40 additions & 0 deletions backend/app/assets/javascripts/comable/admin/navigations.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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) # 置換予定文字を添字に置換する
$field = $(field_tags)
$field.addClass('js-new-record')
$('#navigation-items').append($field) # タグを追加

# アイテムの削除
remove_navigation_item_field: ->
$navigation_item = $(this).closest('.navigation-item')
if $navigation_item.hasClass('js-new-record')
$navigation_item.remove()
else
$navigation_item.find('.destroy').val(true)
$navigation_item.addClass('hidden')

# イベント設定
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
39 changes: 39 additions & 0 deletions backend/app/assets/javascripts/comable/admin/products.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,42 @@ initializa_comable_tagit = ->
$(document).ready(->
initializa_comable_tagit()
)

class @Product
constructor: ->
$(document).ready(@ready)

ready: =>
@radio_published = $('#product_published_at_published')
@radio_unpublished = $('#product_published_at_unpublished')
@published_at = $('#product_published_at')

@initialize_visibility()
@add_event_to_set_visibility()

# 公開/非公開の制御
initialize_visibility: ->
if @radio_published.is(':checked')
@published()
if @radio_unpublished.is(':checked')
@unpublished()

# 公開の際の制御
published: =>
@published_at.show()
@published_at.val(moment().format('YYYY-MM-DD HH:mm')) unless @published_at.val()

# 非公開の際の制御
unpublished: =>
@published_at.hide()
@published_at.val('')

add_event_to_set_visibility: ->
@radio_published.click(@published)
@radio_unpublished.click(@unpublished)

# 日付を空にされたら非公開にする
@published_at.blur( =>
@radio_unpublished.click() unless @published_at.val()
)

13 changes: 13 additions & 0 deletions backend/app/assets/stylesheets/comable/admin/_navigations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#comable-navigation {
.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
4 changes: 4 additions & 0 deletions backend/app/controllers/comable/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def show
end

def new
@product.published_at = Date.today
end

def create
Expand Down Expand Up @@ -65,18 +66,21 @@ def import

private

# rubocop:disable Metrics/MethodLength
def product_params
params.require(:product).permit(
:name,
:code,
:caption,
:price,
:published_at,
:sku_h_item_name,
:sku_v_item_name,
category_path_names: [],
images_attributes: [:id, :file, :_destroy]
)
end
# rubocop:enable Metrics/MethodLength
end
end
end
23 changes: 23 additions & 0 deletions backend/app/helpers/comable/admin/navigations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Comable
module Admin
module NavigationsHelper
def linkable_type_options
Comable::NavigationItem.linkable_params_lists.map { |attr| attr.slice(:name, :type).values }
end

def linkable_id_options(navigation_item)
navigation_item.linkable_class.try(:linkable_id_options) || [[]]
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.delete("\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
40 changes: 40 additions & 0 deletions backend/app/views/comable/admin/navigations/_form.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
= 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-md-3
legend
= Comable.t('admin.nav.navigations.general')

.col-md-9
.form-group
= f.label :name
= f.text_field :name

hr

fieldset
.col-md-3
legend
= Comable.t('admin.nav.navigations.navigation_items')

.col-md-9
#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 @@
- linkable_class = f.object.linkable_class
- link_selectable = linkable_class.try(:linkable_exists?)

fieldset.navigation-item class="#{'hidden' if f.object._destroy}"
.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 class="#{'hidden' if link_selectable}"
= f.label :url
= f.text_field :url

.col-sm-3.linkable_id class="#{'hidden' unless link_selectable}"
= f.select :linkable_id, linkable_id_options(f.object)

.col-sm-3
= f.hidden_field :_destroy, class: :destroy
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'
Loading

0 comments on commit 48e36ec

Please sign in to comment.