Skip to content

Commit

Permalink
Merge 3727eaa into 8718828
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Cake committed Aug 28, 2015
2 parents 8718828 + 3727eaa commit 22ae064
Show file tree
Hide file tree
Showing 68 changed files with 1,298 additions and 357 deletions.
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ language:
- ruby
rvm:
- 2.1.5
- 2.2.0
- 2.2.3
- ruby-head
gemfile:
- gemfiles/active_record_32.gemfile
- gemfiles/active_record_40.gemfile
- gemfiles/active_record_41.gemfile
- gemfiles/active_record_42.gemfile
Expand All @@ -16,10 +15,8 @@ before_script:
- bundle exec rake db:migrate RAILS_ENV=test
matrix:
exclude:
- rvm: 2.2.0
gemfile: gemfiles/active_record_32.gemfile
- rvm: ruby-head
gemfile: gemfiles/active_record_32.gemfile
- rvm: 2.1.5
gemfile: gemfiles/active_record_edge.gemfile
allow_failures:
- rvm: ruby-head
- gemfile: gemfiles/active_record_edge.gemfile
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ Go to your browser and open `http://localhost:3000/admin`.

## Requirements

* Ruby on Rails 3.2, 4.1, 4.2
* Ruby on Rails 4.0, 4.1, 4.2
* Ruby 2.1, 2.2

And `strong_parameters`, `everywhere` gems are required only for Rails 3.
Rails 3 is not supported.

## Development

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ class Dispatcher
new Page
when 'products:new', 'products:show', 'products:edit', 'products:update', 'products:create'
new Product
new Variant
when 'navigations:new', 'navigations:show', 'navigations:edit', 'navigations:update', 'navigations:create'
new Navigation
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
class @Navigation
constructor: ->
$(document).ready(@ready)
@add_event()

ready: =>
@navigation_items = $('#navigation-items')
@add_fields = $('.add_fields')
@add_event()

# linkable_idの検索
search_linkable_ids: ->
Expand Down
9 changes: 3 additions & 6 deletions backend/app/assets/javascripts/comable/admin/pages.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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
4 changes: 0 additions & 4 deletions backend/app/assets/javascripts/comable/admin/products.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ $(document).ready(->

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

ready: =>
@radio_published = $('#product_published_at_published')
@radio_unpublished = $('#product_published_at_unpublished')
@published_at = $('#product_published_at')
Expand Down Expand Up @@ -57,4 +54,3 @@ class @Product
@published_at.blur( =>
@radio_unpublished.click() unless @published_at.val()
)

12 changes: 6 additions & 6 deletions backend/app/assets/javascripts/comable/admin/search.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Search
class DynamicField
constructor: (@templates = {}) ->

remove_fields: (button) ->
Expand All @@ -22,14 +22,14 @@ class Search
# ---
# main
# ---
search = new Search()
dynamic_field = new DynamicField

$(document).on('click', '.ransack.add_fields', ->
search.add_fields(this, $(this).data('fieldType'), $(this).data('content'))
$(document).on('click', '.add_fields', ->
dynamic_field.add_fields(this, $(this).data('fieldType'), $(this).data('content'))
false
)

$(document).on('click', '.ransack.remove_fields', ->
search.remove_fields(this)
$(document).on('click', '.remove_fields', ->
dynamic_field.remove_fields(this)
false
)
104 changes: 104 additions & 0 deletions backend/app/assets/javascripts/comable/admin/variants.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class @Variant
initialized: false

constructor: ->
@initialize_tagits()
@register_click_event_to_add_option_button()
@register_click_event_to_remove_option_button()
@initialized = true

initialize_tagits: ->
_this = @
$('.js-tagit-option-values').each( ->
_this.initialize_tagit(this)
)

initialize_tagit: (element) ->
$element = $(element)
index = $element.data('index')
$element.tagit({
fieldName: 'product[option_types_attributes][' + index + '][values][]',
caseSensitive: false,
removeConfirmation: true,
readOnly: $element.hasClass('tagit-readonly'),
afterTagAdded: @rebuild_variants,
afterTagRemoved: @rebuild_variants
})

register_click_event_to_add_option_button: ->
$('.js-add-option').click( =>
@change_from_master()
setTimeout( =>
@initialize_tagits()
, 1)
)

register_click_event_to_remove_option_button: ->
$(document).on('click', '.js-remove-option', ->
$(this).closest('.js-new-variants').remove()
@change_to_master()
)

rebuild_variants: (event, ui) =>
return unless @initialized
@remove_variants()
@build_variants()

build_variants: ->
_this = @
option_types = []
$('.js-tagit-option-values').each( ->
return unless $(this).hasClass('tagit')
option_values = $(this).tagit('assignedTags')
option_types.push(option_values)
)
option_values_for_variants = _product(_compact(option_types))
option_values_for_variants.forEach((option_values_for_variant) ->
_this.build_variant(option_values_for_variant)
)

build_variant: (option_values) ->
$variant = @new_variant()

$variant.find('[data-name="names"] > input').val(option_values)
option_values.forEach((option_value) ->
$variant.find('[data-name="names"]').append('<span class="comable-variant-name">' + option_value + '</span> ')
)

$('.js-variants-table').find('tbody').append($variant)

remove_variants: ->
$('.js-new-variants:not(.hidden)').remove()

new_variant: ->
new_id = new Date().getTime()
$('.js-add-variant').click()
$variant = $('.js-new-variants').last()
$variant.removeClass('hidden')
$variant.html($variant.html().replace(/new_variant/g, new_id))
$variant

change_from_master: ->
$('.js-variants-table').removeClass('hidden')
$('#product_variants_attributes_0__destroy').val(1)

change_to_master: ->
$('.js-variants-table').removeClass('hidden')
$('.js-variants-table').addClass('hidden')
$('#product_variants_attributes_0__destroy').val(0) if $('.js-new-variants').length == 0

# refs http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript/2843625#2843625
_compact = (arrays) ->
$.grep(arrays, (n) -> n if n && n.length != 0)

# refs http://cwestblog.com/2011/05/02/cartesian-product-of-multiple-arrays/
_product = (arrays) ->
Array.prototype.reduce.call(arrays, (a, b) ->
ret = []
a.forEach((a) ->
b.forEach((b) ->
ret.push(a.concat([b]))
)
)
ret
, [[]])
9 changes: 9 additions & 0 deletions backend/app/assets/stylesheets/comable/admin/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ footer {
cursor: inherit;
display: block;
}

// for Tag-it
ul.tagit {
@include border-radius($input-border-radius);

&.tagit-readonly {
background-color: #eee;
}
}
40 changes: 40 additions & 0 deletions backend/app/assets/stylesheets/comable/admin/_variants.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
$comable-colors: #ffa000 #5dd39e #d81159 #06aed5 #8f2d56;

@for $i from 1 through length($comable-colors) {
$color: nth($comable-colors, $i);

.comable-variant-name:nth-of-type(#{$i}) {
color: darken($color, 5%);

&:after {
display: inline;
margin: 0 5px;
color: #444;
content: '·';
}

&:last-child:after {
display: none;
}
}

.comable-option:nth-of-type(#{$i}) {
.comable-option-name {
color: darken($color, 5%);
}

ul.comable-option-values li:not(.tagit-new) {
background-color: $color;
border-color: $color;

& > span, & > a, & > a > span {
color: #fff;
}

&.remove {
background-color: darken($color, 10%);
border-color: darken($color, 10%);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
@import 'comable/admin/navigations';
@import 'comable/admin/user_sessions';
@import 'comable/admin/themes';
@import 'comable/admin/variants';
13 changes: 5 additions & 8 deletions backend/app/controllers/comable/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ class ProductsController < Comable::Admin::ApplicationController

def index
@q = Comable::Product.ransack(params[:q])
@products = @q.result(distinct: true).includes(:stocks, :images).page(params[:page]).accessible_by(current_ability)
@products = @q.result(distinct: true).includes(:images, variants: [:option_values, :stock]).page(params[:page]).accessible_by(current_ability)
end

def show
render :edit
end

def new
@product.variants.build
@product.published_at = Date.today
end

Expand Down Expand Up @@ -66,21 +67,17 @@ 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]
images_attributes: [:id, :file, :_destroy],
variants_attributes: [:id, :price, :sku, :names, :quantity, :_destroy],
option_types_attributes: [:id, :name, { values: [] }]
)
end
# rubocop:enable Metrics/MethodLength
end
end
end
7 changes: 5 additions & 2 deletions backend/app/controllers/comable/admin/stocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class StocksController < Comable::Admin::ApplicationController

def index
@q = @stocks.ransack(params[:q])
@stocks = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability)
@stocks = @q.result.includes(variant: [:product, :option_values]).page(params[:page]).accessible_by(current_ability)
end

def show
Expand All @@ -21,6 +21,9 @@ def new
end

def create
# TODO: Remove
@stock.build_variant(product: @product) unless @stock.variant

if @stock.save
redirect_to comable.admin_stock_path(@stock), notice: Comable.t('successful')
else
Expand Down Expand Up @@ -52,7 +55,7 @@ def destroy

def export
q = @stocks.ransack(params[:q])
stocks = q.result.includes(:product).accessible_by(current_ability)
stocks = q.result.includes(variant: :product).accessible_by(current_ability)

respond_to_export_with stocks
end
Expand Down

0 comments on commit 22ae064

Please sign in to comment.