rails_admin_sort_embedded 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a53883033a432138b65e96da0cf4b0ba650d6540
4
- data.tar.gz: 02b2517b55b56dd81c5b0c635777b5da7cda8c3b
3
+ metadata.gz: dcf6ebd2958b3b0144c17cc6c39fc5575dce5205
4
+ data.tar.gz: 3af26e237488484cf6859cde6c7a738b0f0e4cdf
5
5
  SHA512:
6
- metadata.gz: f8bc21dd4ebac547f8b9190522efe86118437564acd24c854a045bb688759e4edc837e6e72d5beaa87636673b8deae7a118c7aa2f50b870197603a047a63520c
7
- data.tar.gz: 7b7abaa9240591ab081f6a265cf3943e840c8977640799cbe497853061b0cbd76be5a2cf51e50fe4ca7e9524f038b766744e48e1c184cb3a212d7946c6d6d5ce
6
+ metadata.gz: 847b12f747a99dd133a09f7401fa9f7a071daea20ee94ff936c1e33f574f15bb4fce12f6be744f8fc39890889ff6f6079b1d88b09431779d0b76649b9d8e3b5f
7
+ data.tar.gz: 087e44e696dd10a6c7292b69ea8b6f6f63faf6a7afcf7130c093ecaa95b23473dc09c108c5a62f1282c513d4ec89a49208dc261b949a04961113789f73a1a26b
data/README.md CHANGED
@@ -37,12 +37,24 @@ In embedded model:
37
37
  field :order, type: Integer, default: 0
38
38
  scope :sorted, -> { order_by([:order, :asc]) } #optional
39
39
 
40
+ or use https://github.com/ack43/rocket_cms and:
41
+
42
+ # generate 'order' field, 'sort' as alias and scopes 'ordered' and 'sorted'
43
+ include SortField
44
+ sort_field
45
+
46
+ or
47
+
48
+ # generate fields and scopes with prefix 'another_': 'another_order' field, 'another_sort' as alias and scopes 'another_ordered' and 'another_sorted'
49
+ include SortField
50
+ sort_field :another
51
+
40
52
  In parent model:
41
53
 
42
54
  embeds_many :method_name
43
55
  rails_admin do ...
44
56
  sort_embedded({
45
- fields: [:method_name],
57
+ fields: [{model: [:order_field_1, :order_scope_1]}, {model: [:order_field_2, :order_scope_2]}...],
46
58
  toggle_fields: [:enabled],
47
59
  thumbnail_fields: [:image, :cover],
48
60
  thumbnail_size: :thumb,
@@ -50,6 +62,27 @@ In parent model:
50
62
  })
51
63
  end
52
64
 
65
+
66
+ You can not use '[:order_field_1, :order_scope_1]' and just use [:order_field_1]. Scope_name will be :order_field_1 as string + "ed"
67
+ Default field is 'order', default scope is 'ordered';
68
+
69
+ I18n:
70
+
71
+ ru:
72
+ rails_admin:
73
+ sort_embedded:
74
+ my_news:
75
+ my_news_images: Фотогалерея
76
+
77
+ # or with specified fields
78
+ ru:
79
+ rails_admin:
80
+ sort_embedded:
81
+ my_news:
82
+ my_news_images:
83
+ order: Фотогалерея
84
+ another_order: Фотогалерея(alt)
85
+
53
86
  ## Contributing
54
87
 
55
88
  1. Fork it
@@ -1,11 +1,11 @@
1
1
  #= require rails_admin/jquery.mjs.nestedSortable
2
2
 
3
- show_flash = (data)->
3
+ show_flash = (selector, data)->
4
4
  $flash = $('<div>')
5
5
  .addClass('nestable-flash alert')
6
6
  .append($('<button>').addClass('close').data('dismiss', 'alert').html('&times;'))
7
7
  .append($('<span>').addClass('body').html(data))
8
- $('#rails_admin_nestable').append($flash)
8
+ $(selector).append($flash)
9
9
  $flash.fadeIn(200).delay(2000).fadeOut 200, -> $(this).remove()
10
10
 
11
11
  js_tree_toggle = (e)->
@@ -46,6 +46,8 @@ init = ->
46
46
  ui.item.closest("ol").find("li").each ->
47
47
  ids_array.push $(this).data("id")
48
48
 
49
+ selector = "#rails_admin_nestable_" + tree_config["embedded_field"] + "_" + (tree_config["embedded_model_order_field"] || "order")
50
+
49
51
  $.ajax
50
52
  type: "POST"
51
53
  dataType: "html"
@@ -54,16 +56,17 @@ init = ->
54
56
  #item_id: ui.item.data("id")
55
57
  embedded_model: tree_config["embedded_model"]
56
58
  embedded_field: tree_config["embedded_field"]
59
+ embedded_model_order_field: tree_config["embedded_model_order_field"]
57
60
  #parent_id: ui.item.parent().parent().data("id")
58
61
  #prev_id: ui.item.prev().data("id")
59
62
  #next_id: ui.item.next().data("id")
60
63
  ids_array: ids_array.join(" ")
61
64
 
62
65
  error: (xhr, status, error) ->
63
- show_flash('Nested Set: fatal error')
66
+ show_flash(selector, 'Nested Set: fatal error')
64
67
 
65
68
  success: (data) ->
66
- show_flash(data)
69
+ show_flash(selector, data)
67
70
 
68
71
  $(document).off('pjax:end.rails_admin_sort_embedded').on('pjax:end.rails_admin_sort_embedded', init)
69
72
  $(document).off('ready.rails_admin_sort_embedded').on('ready.rails_admin_sort_embedded', init)
@@ -184,7 +184,7 @@
184
184
  text-decoration: none;
185
185
  }
186
186
 
187
- #rails_admin_nestable {
187
+ .rails_admin_nestable {
188
188
  position: relative;
189
189
  margin-bottom: 100px;
190
190
  }
@@ -5,11 +5,48 @@
5
5
  %ul.nav.nav-tabs
6
6
  - sort_embedded_fields.each do |f|
7
7
  %li
8
- = link_to I18n.t("rails_admin.sort_embedded.#{@object.class.name.tableize}.#{f}"), "#sort_embedded_#{f}", title: f, "data-toggle" => "tab"
8
+ :ruby
9
+ if f.is_a?(Hash)
10
+ embedded_field = f.keys.first
11
+ embedded_model_order_field = f.values.first
12
+ embedded_model_order_field = embedded_model_order_field.last if embedded_model_order_field.is_a?(Array)
13
+
14
+ i18n_path = "rails_admin.sort_embedded.#{@object.class.name.tableize}.#{embedded_field}.#{embedded_model_order_field}"
15
+ i18n_path_default = "rails_admin.sort_embedded.#{@object.class.name.tableize}.#{embedded_field}"
16
+
17
+ link = "#sort_embedded_#{embedded_field}_#{embedded_model_order_field}"
18
+ title = I18n.t(i18n_path, default: I18n.t(i18n_path_default))
19
+
20
+ else
21
+ i18n_path = "rails_admin.sort_embedded.#{@object.class.name.tableize}.#{f}.order"
22
+ i18n_path_default = "rails_admin.sort_embedded.#{@object.class.name.tableize}.#{f}"
23
+ link = "#sort_embedded_#{f}"
24
+ title = I18n.t(i18n_path, default: I18n.t(i18n_path_default))
25
+ end
26
+
27
+ = link_to I18n.t(i18n_path, default: I18n.t(i18n_path_default)), link, title: title, "data-toggle" => "tab"
9
28
 
10
29
  .tab-content
11
30
  - sort_embedded_fields.each do |f|
12
- .tab-pane{id: "sort_embedded_#{f}"}
13
- .row-fluid
14
- .span12#rails_admin_nestable
15
- = rails_admin_sort_embedded @object.send(f).to_a, {embedded_field: f}
31
+ - if f.is_a?(Hash)
32
+ - embedded_field = f.keys.first
33
+ - embedded_model_order_field = f.values.first
34
+ - if embedded_model_order_field.is_a?(Array)
35
+ - if embedded_model_order_field.size > 1
36
+ - embedded_model_ordered_scope = embedded_model_order_field[1]
37
+ - else
38
+ - embedded_model_ordered_scope = "#{embedded_model_order_field[0]}ed"
39
+ - embedded_model_order_field = embedded_model_order_field[0]
40
+ - else
41
+ - embedded_model_ordered_scope = "#{embedded_model_order_field}ed"
42
+
43
+ .tab-pane{id: "sort_embedded_#{embedded_field}_#{embedded_model_order_field}"}
44
+ .row-fluid
45
+ .span12.rails_admin_nestable{id: "rails_admin_nestable_#{embedded_field}_#{embedded_model_order_field}"}
46
+ = rails_admin_sort_embedded @object.send(embedded_field).send(embedded_model_ordered_scope).to_a, {embedded_field: embedded_field, embedded_model_order_field: embedded_model_order_field}
47
+
48
+ - else
49
+ .tab-pane{id: "sort_embedded_#{f}"}
50
+ .row-fluid
51
+ .span12.rails_admin_nestable{id: "rails_admin_nestable_#{f}_order"}
52
+ = rails_admin_sort_embedded @object.send(f).ordered.to_a, {embedded_field: f}
@@ -36,6 +36,10 @@ module RailsAdmin
36
36
  ids = params[:ids_array].to_s.split(" ")
37
37
  embedded_model = params[:embedded_model].to_s
38
38
  embedded_field = params[:embedded_field].to_s
39
+
40
+ embedded_model_order_field = params[:embedded_model_order_field].to_s
41
+ embedded_model_order_field = "order" if embedded_model_order_field.blank?
42
+
39
43
  # item_id = params[:item_id].to_s
40
44
  # parent_id = params[:parent_id].to_s
41
45
  # prev_id = params[:prev_id].to_s
@@ -48,7 +52,7 @@ module RailsAdmin
48
52
  main_obj = @object
49
53
  embedded = main_obj.send(embedded_field)
50
54
  ids.each_with_index do |id, i|
51
- embedded.find(id).update(order: i)
55
+ embedded.find(id).update(embedded_model_order_field.to_sym => i)
52
56
  end
53
57
 
54
58
  message = "<strong>#{I18n.t('admin.actions.sort_embedded.success')}!</strong>"
@@ -58,7 +62,7 @@ module RailsAdmin
58
62
  main_obj = @object
59
63
  embedded = main_obj.send(embedded_field).sorted
60
64
  ids.each_with_index do |id, i|
61
- embedded.find(id).update(order: i)
65
+ embedded.find(id).update(embedded_model_order_field.to_sym => i)
62
66
  end
63
67
 
64
68
  message = "<strong>#{I18n.t('admin.actions.sort_embedded.error')}</strong>: #{e}"
@@ -6,7 +6,7 @@ module RailsAdminSortEmbedded
6
6
 
7
7
  def options
8
8
  @options ||= {
9
- fields: [],
9
+ fields: [{}],
10
10
  thumbnail_fields: [:image, :cover],
11
11
  thumbnail_size: :thumb,
12
12
  thumbnail_gem: :paperclip,
@@ -1,11 +1,12 @@
1
1
  module RailsAdminSortEmbedded
2
2
  module Helper
3
3
  def rails_admin_sort_embedded(tree, opts= {})
4
- tree = tree.to_a.sort_by { |m| m.order }
4
+ tree = tree.to_a.sort_by { |m| m.send(opts[:embedded_model_order_field] || "order") }
5
5
  roots = tree#.select{|elem| elem.parent_id.nil?}
6
6
  id = "ns_#{rand(100_000_000..999_999_999)}"
7
7
  tree_config = {update_url: sort_embedded_path(model_name: @abstract_model),
8
8
  embedded_field: opts[:embedded_field],
9
+ embedded_model_order_field: opts[:embedded_model_order_field],
9
10
  embedded_model: @abstract_model.model.new.send(opts[:embedded_field]).new.class.to_s}.to_json
10
11
  content_tag(:ol, rails_admin_sort_embedded_builder(roots, tree), id: id, class: 'dd-list rails_admin_sort_embedded', 'data-config' => tree_config)
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminSortEmbedded
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_sort_embedded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Kiseliev
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-24 00:00:00.000000000 Z
12
+ date: 2015-03-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails_admin