comable-backend 0.6.0 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/comable/admin/dispatcher.coffee +12 -2
  3. data/app/assets/javascripts/comable/admin/navigations.coffee +37 -0
  4. data/app/assets/javascripts/comable/admin/pages.coffee +0 -3
  5. data/app/assets/javascripts/comable/admin/products.coffee +35 -0
  6. data/app/assets/javascripts/comable/admin/search.coffee +6 -6
  7. data/app/assets/javascripts/comable/admin/themes.coffee +53 -64
  8. data/app/assets/javascripts/comable/admin/variants.coffee +106 -0
  9. data/app/assets/stylesheets/comable/admin/_common.scss +9 -0
  10. data/app/assets/stylesheets/comable/admin/_navigations.scss +13 -0
  11. data/app/assets/stylesheets/comable/admin/_variants.scss +40 -0
  12. data/app/assets/stylesheets/comable/admin/application.scss +2 -0
  13. data/app/controllers/comable/admin/navigations_controller.rb +84 -0
  14. data/app/controllers/comable/admin/orders_controller.rb +7 -14
  15. data/app/controllers/comable/admin/products_controller.rb +14 -6
  16. data/app/controllers/comable/admin/stocks_controller.rb +5 -2
  17. data/app/controllers/comable/admin/themes_controller.rb +5 -9
  18. data/app/controllers/comable/admin/variants_controller.rb +59 -0
  19. data/app/helpers/comable/admin/application_helper.rb +19 -12
  20. data/app/helpers/comable/admin/navigations_helper.rb +23 -0
  21. data/app/helpers/comable/admin/themes_helper.rb +1 -1
  22. data/{config/initializers/awesome_admin_layout.rb → app/navigations/comable/admin/application.rb} +8 -4
  23. data/app/views/comable/admin/navigations/_form.slim +40 -0
  24. data/app/views/comable/admin/navigations/_navigation_item_fields.slim +24 -0
  25. data/app/views/comable/admin/navigations/edit.slim +27 -0
  26. data/app/views/comable/admin/navigations/index.slim +25 -0
  27. data/app/views/comable/admin/navigations/new.slim +16 -0
  28. data/app/views/comable/admin/navigations/search_linkable_ids.coffee +15 -0
  29. data/app/views/comable/admin/orders/show.slim +1 -1
  30. data/app/views/comable/admin/products/_form.slim +91 -68
  31. data/app/views/comable/admin/products/edit.slim +4 -0
  32. data/app/views/comable/admin/products/index.slim +13 -11
  33. data/app/views/comable/admin/shared/{_image_fields.slim → _images_fields.slim} +5 -5
  34. data/app/views/comable/admin/shared/_option_type_fields.slim +13 -0
  35. data/app/views/comable/admin/shared/_option_types_fields.slim +13 -0
  36. data/app/views/comable/admin/shared/_variant_form.slim +26 -0
  37. data/app/views/comable/admin/shared/_variants_fields.slim +14 -0
  38. data/app/views/comable/admin/stocks/index.slim +16 -11
  39. data/app/views/comable/admin/variants/_form.slim +34 -0
  40. data/app/views/comable/admin/variants/edit.slim +31 -0
  41. data/app/views/comable/admin/variants/index.slim +97 -0
  42. data/app/views/comable/admin/variants/new.slim +16 -0
  43. data/app/views/layouts/comable/admin/application.slim +1 -1
  44. data/config/routes.rb +8 -1
  45. metadata +166 -49
@@ -0,0 +1,84 @@
1
+ require_dependency 'comable/admin/application_controller'
2
+
3
+ module Comable
4
+ module Admin
5
+ class NavigationsController < Comable::Admin::ApplicationController
6
+ load_and_authorize_resource class: Comable::Navigation.name, except: :index
7
+
8
+ def index
9
+ @q = Comable::Navigation.ransack(params[:q])
10
+ @navigations = @q.result.accessible_by(current_ability)
11
+ end
12
+
13
+ def show
14
+ edit
15
+ render :edit
16
+ end
17
+
18
+ def new
19
+ @navigation.navigation_items.build
20
+ end
21
+
22
+ def edit
23
+ end
24
+
25
+ def create
26
+ @navigation = Comable::Navigation.new(navigation_params)
27
+ if @navigation.save
28
+ redirect_to comable.admin_navigation_path(@navigation), notice: Comable.t('successful')
29
+ else
30
+ render :new
31
+ end
32
+ end
33
+
34
+ def update
35
+ @navigation.attributes = navigation_params
36
+ if @navigation.save
37
+ redirect_to comable.admin_navigation_path(@navigation), notice: Comable.t('successful')
38
+ else
39
+ render :edit
40
+ end
41
+ end
42
+
43
+ def destroy
44
+ @navigation.destroy
45
+ redirect_to comable.admin_navigations_path, notice: Comable.t('successful')
46
+ end
47
+
48
+ def search_linkable_ids
49
+ @linkable_id_options = linkable_id_options
50
+ render layout: false
51
+ end
52
+
53
+ private
54
+
55
+ def linkable_type
56
+ return if params[:linkable_type].blank?
57
+ params[:linkable_type] if Comable.const_defined?(params[:linkable_type].demodulize)
58
+ end
59
+
60
+ def linkable_id_options
61
+ linkable_type ? linkable_type.constantize.linkable_id_options : [[]]
62
+ end
63
+
64
+ def navigation_params
65
+ params.require(:navigation).permit(
66
+ :name,
67
+ navigation_items_attributes: navigation_items_attributes_keys
68
+ )
69
+ end
70
+
71
+ def navigation_items_attributes_keys
72
+ [
73
+ :id,
74
+ :position,
75
+ :linkable_id,
76
+ :linkable_type,
77
+ :name,
78
+ :url,
79
+ :_destroy
80
+ ]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -7,6 +7,9 @@ module Comable
7
7
 
8
8
  load_and_authorize_resource class: Comable::Order.name, except: :index
9
9
 
10
+ rescue_from ActiveRecord::RecordInvalid, with: :redirect_to_back_with_alert
11
+ rescue_from Comable::PaymentError, with: :redirect_to_back_with_alert
12
+
10
13
  def index
11
14
  @q = Comable::Order.complete.ransack(params[:q])
12
15
  @orders = @q.result.page(params[:page]).per(15).recent.accessible_by(current_ability)
@@ -38,50 +41,36 @@ module Comable
38
41
  def cancel
39
42
  @order.cancel!
40
43
  redirect_to :back, notice: Comable.t('successful')
41
- rescue ActiveRecord::RecordInvalid => e
42
- redirect_to :back, alert: e.message
43
44
  end
44
45
 
45
46
  def resume
46
47
  @order.resume!
47
48
  redirect_to :back, notice: Comable.t('successful')
48
- rescue ActiveRecord::RecordInvalid => e
49
- redirect_to :back, alert: e.message
50
49
  end
51
50
 
52
51
  def cancel_payment
53
52
  @order.payment.cancel!
54
53
  redirect_to :back, notice: Comable.t('successful')
55
- rescue ActiveRecord::RecordInvalid => e
56
- redirect_to :back, alert: e.message
57
54
  end
58
55
 
59
56
  def resume_payment
60
57
  @order.payment.resume!
61
58
  redirect_to :back, notice: Comable.t('successful')
62
- rescue ActiveRecord::RecordInvalid => e
63
- redirect_to :back, alert: e.message
64
59
  end
65
60
 
66
61
  def ship
67
62
  @order.shipment.ship!
68
63
  redirect_to :back, notice: Comable.t('successful')
69
- rescue ActiveRecord::RecordInvalid => e
70
- redirect_to :back, alert: e.message
71
64
  end
72
65
 
73
66
  def cancel_shipment
74
67
  @order.shipment.cancel!
75
68
  redirect_to :back, notice: Comable.t('successful')
76
- rescue ActiveRecord::RecordInvalid => e
77
- redirect_to :back, alert: e.message
78
69
  end
79
70
 
80
71
  def resume_shipment
81
72
  @order.shipment.resume!
82
73
  redirect_to :back, notice: Comable.t('successful')
83
- rescue ActiveRecord::RecordInvalid => e
84
- redirect_to :back, alert: e.message
85
74
  end
86
75
 
87
76
  private
@@ -97,6 +86,10 @@ module Comable
97
86
  order_items_attributes: [:id, :name, :code, :price, :quantity]
98
87
  )
99
88
  end
89
+
90
+ def redirect_to_back_with_alert(exception)
91
+ redirect_to :back, alert: exception.message
92
+ end
100
93
  end
101
94
  end
102
95
  end
@@ -7,14 +7,17 @@ module Comable
7
7
 
8
8
  def index
9
9
  @q = Comable::Product.ransack(params[:q])
10
- @products = @q.result(distinct: true).includes(:stocks, :images).page(params[:page]).accessible_by(current_ability)
10
+ @products = @q.result(distinct: true).includes(:images, variants: [:option_values, :stock]).page(params[:page]).accessible_by(current_ability)
11
11
  end
12
12
 
13
13
  def show
14
+ edit
14
15
  render :edit
15
16
  end
16
17
 
17
18
  def new
19
+ @product.variants.build
20
+ @product.published_at = Date.today
18
21
  end
19
22
 
20
23
  def create
@@ -27,6 +30,7 @@ module Comable
27
30
  end
28
31
 
29
32
  def edit
33
+ set_preview_session
30
34
  end
31
35
 
32
36
  def update
@@ -68,15 +72,19 @@ module Comable
68
72
  def product_params
69
73
  params.require(:product).permit(
70
74
  :name,
71
- :code,
72
75
  :caption,
73
- :price,
74
- :sku_h_item_name,
75
- :sku_v_item_name,
76
+ :published_at,
76
77
  category_path_names: [],
77
- images_attributes: [:id, :file, :_destroy]
78
+ images_attributes: [:id, :file, :_destroy],
79
+ variants_attributes: [:id, :price, :sku, :options, :quantity, :_destroy],
80
+ option_types_attributes: [:id, :name, { values: [] }]
78
81
  )
79
82
  end
83
+
84
+ def set_preview_session
85
+ session[Comable::Product::PREVIEW_SESSION_KEY] ||= {}
86
+ session[Comable::Product::PREVIEW_SESSION_KEY][@product.to_param] = true
87
+ end
80
88
  end
81
89
  end
82
90
  end
@@ -10,7 +10,7 @@ module Comable
10
10
 
11
11
  def index
12
12
  @q = @stocks.ransack(params[:q])
13
- @stocks = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability)
13
+ @stocks = @q.result.includes(variant: [:product, :option_values]).page(params[:page]).accessible_by(current_ability)
14
14
  end
15
15
 
16
16
  def show
@@ -21,6 +21,9 @@ module Comable
21
21
  end
22
22
 
23
23
  def create
24
+ # TODO: Remove
25
+ @stock.build_variant(product: @product) unless @stock.variant
26
+
24
27
  if @stock.save
25
28
  redirect_to comable.admin_stock_path(@stock), notice: Comable.t('successful')
26
29
  else
@@ -52,7 +55,7 @@ module Comable
52
55
 
53
56
  def export
54
57
  q = @stocks.ransack(params[:q])
55
- stocks = q.result.includes(:product).accessible_by(current_ability)
58
+ stocks = q.result.includes(variant: :product).accessible_by(current_ability)
56
59
 
57
60
  respond_to_export_with stocks
58
61
  end
@@ -42,7 +42,7 @@ module Comable
42
42
 
43
43
  def destroy
44
44
  @theme.destroy
45
- FileUtils.rm_rf(theme_dir)
45
+ @theme.dir.rmtree if @theme.dir.exist?
46
46
  redirect_to comable.admin_themes_path, notice: Comable.t('successful')
47
47
  end
48
48
 
@@ -51,7 +51,7 @@ module Comable
51
51
  end
52
52
 
53
53
  def show_file
54
- @code = File.read(filepath) if filepath && File.exist?(filepath)
54
+ @code = filepath.read if filepath.try(:file?)
55
55
  end
56
56
 
57
57
  def update_file
@@ -74,17 +74,13 @@ module Comable
74
74
  # Validate the Liquid syntax
75
75
  Liquid::Template.parse(params[:code])
76
76
 
77
- FileUtils.mkdir_p(File.dirname(filepath)) unless File.exist?(filepath)
78
- File.write(filepath, params[:code])
79
- end
80
-
81
- def theme_dir
82
- File.join('themes', @theme.name)
77
+ filepath.dirname.mkpath unless filepath.dirname.exist?
78
+ filepath.write(params[:code])
83
79
  end
84
80
 
85
81
  def filepath
86
82
  return unless params[:path]
87
- File.join(theme_dir, params[:path])
83
+ @theme.dir + params[:path]
88
84
  end
89
85
 
90
86
  def theme_params
@@ -0,0 +1,59 @@
1
+ require_dependency 'comable/admin/application_controller'
2
+
3
+ module Comable
4
+ module Admin
5
+ class VariantsController < Comable::Admin::ApplicationController
6
+ load_and_authorize_resource :product, class: Comable::Product.name
7
+ load_and_authorize_resource :variant, class: Comable::Variant.name, through: :product
8
+
9
+ def index
10
+ @q = @variants.ransack(params[:q])
11
+ @variants = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability)
12
+ end
13
+
14
+ def show
15
+ render :edit
16
+ end
17
+
18
+ def new
19
+ end
20
+
21
+ def create
22
+ if @variant.save
23
+ redirect_to comable.admin_product_variant_path(@product, @variant), notice: Comable.t('successful')
24
+ else
25
+ flash.now[:alert] = Comable.t('failure')
26
+ render :new
27
+ end
28
+ end
29
+
30
+ def edit
31
+ end
32
+
33
+ def update
34
+ if @variant.update_attributes(variant_params)
35
+ redirect_to comable.admin_product_variant_path(@product, @variant), notice: Comable.t('successful')
36
+ else
37
+ flash.now[:alert] = Comable.t('failure')
38
+ render :edit
39
+ end
40
+ end
41
+
42
+ def destroy
43
+ @variant.destroy
44
+ redirect_to comable.admin_product_variants_path(@product), notice: Comable.t('successful')
45
+ end
46
+
47
+ private
48
+
49
+ def variant_params
50
+ params.require(:variant).permit(
51
+ :price,
52
+ :sku,
53
+ stock_attributes: [:quantity],
54
+ option_values_attributes: [:id, :name]
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
@@ -10,28 +10,29 @@ module Comable
10
10
  image_tag "//www.gravatar.com/avatar/#{hash}?default=mm", options
11
11
  end
12
12
 
13
- def link_to_add_fields(name, f, association, options = {})
14
- new_object = f.object.class.reflect_on_association(association).klass.new
15
- fields = f.fields_for(association, new_object, child_index: "new_#{association}") do |builder|
16
- render("comable/admin/shared/#{association.to_s.singularize}_fields", ff: builder)
17
- end
18
- link_to(name, 'javascript:void(0)', options.merge(onclick: "add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
13
+ def link_to_add_fields(name, f, type, options = {})
14
+ new_fields = build_fields(f, type)
15
+ link_to(name, 'javascript:void(0)', options.merge(class: "add_fields #{options[:class]}", 'data-field-type' => type, 'data-content' => "#{new_fields}"))
19
16
  end
20
17
 
21
18
  def button_to_remove_fields(name, options = {})
22
- content_tag(:button, name, options.merge(class: "ransack remove_fields #{options[:class]}"))
19
+ content_tag(:button, name, options.merge(class: "remove_fields #{options[:class]}"))
23
20
  end
24
21
 
25
22
  def button_to_add_fields(name, f, type, options = {})
26
23
  new_fields = build_fields(f, type)
27
- content_tag(:button, name, options.merge(class: "ransack add_fields #{options[:class]}", 'data-field-type' => type, 'data-content' => "#{new_fields}"))
24
+ content_tag(:button, name, options.merge(class: "add_fields #{options[:class]}", 'data-field-type' => type, 'data-content' => "#{new_fields}"))
28
25
  end
29
26
 
30
27
  def build_fields(f, type)
31
- new_object = f.object.send("build_#{type}")
32
-
33
- f.send("#{type}_fields", new_object, child_index: "new_#{type}") do |builder|
34
- render("comable/admin/shared/#{type}_fields", f: builder)
28
+ render_block = -> (builder) { render("comable/admin/shared/#{type}_fields", f: builder) }
29
+
30
+ if singular? type
31
+ new_object = f.object.send("build_#{type}")
32
+ f.send("#{type}_fields", new_object, child_index: "new_#{type}", &render_block)
33
+ else
34
+ new_object = f.object.send(type).build
35
+ f.send('fields_for', type, new_object, child_index: "new_#{type}", &render_block)
35
36
  end
36
37
  end
37
38
 
@@ -48,6 +49,12 @@ module Comable
48
49
  def page_name
49
50
  [controller_name, action_name].join(':')
50
51
  end
52
+
53
+ private
54
+
55
+ def singular?(string)
56
+ string.to_s.try(:singularize) == string.to_s
57
+ end
51
58
  end
52
59
  end
53
60
  end
@@ -0,0 +1,23 @@
1
+ module Comable
2
+ module Admin
3
+ module NavigationsHelper
4
+ def linkable_type_options
5
+ Comable::NavigationItem.linkable_params_lists.map { |attr| attr.slice(:name, :type).values }
6
+ end
7
+
8
+ def linkable_id_options(navigation_item)
9
+ navigation_item.linkable_class.try(:linkable_id_options) || [[]]
10
+ end
11
+
12
+ # アイテム追加ボタン設置
13
+ def add_fields_button_tag(name, f, association)
14
+ new_object = f.object.send(association).klass.new
15
+ index = new_object.object_id # 後で置換するために必要な文字を入れる
16
+ fields = f.fields_for(association, new_object, child_index: index) do |builder|
17
+ render(association.to_s.singularize + '_fields', f: builder)
18
+ end
19
+ button_tag(name, type: :button, class: 'add_fields btn btn-default pull-right', data: { index: index, fields: fields.delete("\n") })
20
+ end
21
+ end
22
+ end
23
+ end
@@ -27,7 +27,7 @@ module Comable
27
27
 
28
28
  def load_directory_tree(path, parent = nil)
29
29
  children = []
30
- tree = { (parent || :root) => children }
30
+ tree = { (parent || :root) => children }
31
31
 
32
32
  Dir.foreach(path) do |entry|
33
33
  next if entry.start_with? '.'
@@ -1,8 +1,8 @@
1
- AwesomeAdminLayout.setup(only: Comable::Admin::ApplicationController) do |controller|
1
+ AwesomeAdminLayout.define(only: Comable::Admin::ApplicationController) do |controller|
2
2
  comable = controller.comable
3
3
  current_comable_user = controller.current_comable_user
4
4
 
5
- navigation do
5
+ navigation :admin do
6
6
  brand Comable.app_name do
7
7
  external_link comable.root_path
8
8
  end
@@ -47,7 +47,7 @@ AwesomeAdminLayout.setup(only: Comable::Admin::ApplicationController) do |contro
47
47
  brand Comable.t('admin.nav.product')
48
48
 
49
49
  item Comable.t('admin.nav.products.list') do
50
- link comable.admin_products_path(hoge: 112)
50
+ link comable.admin_products_path
51
51
  end
52
52
 
53
53
  item Comable.t('admin.nav.stock') do
@@ -72,6 +72,10 @@ AwesomeAdminLayout.setup(only: Comable::Admin::ApplicationController) do |contro
72
72
  link comable.admin_pages_path
73
73
  end
74
74
 
75
+ item Comable.t('admin.nav.navigation') do
76
+ link comable.admin_navigations_path
77
+ end
78
+
75
79
  divider
76
80
 
77
81
  item Comable.t('admin.nav.shipment_method') do
@@ -104,7 +108,7 @@ AwesomeAdminLayout.setup(only: Comable::Admin::ApplicationController) do |contro
104
108
  divider
105
109
 
106
110
  item Comable.t('admin.sign_out') do
107
- link comable.destroy_user_session_path, method: :delete
111
+ link comable.destroy_admin_user_session_path, method: :delete
108
112
  end
109
113
  end
110
114
  end