flexite 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/flexite/configs_controller.rb +2 -2
  3. data/app/controllers/flexite/entries_controller.rb +15 -11
  4. data/app/helpers/flexite/entries_helper.rb +1 -1
  5. data/app/services/flexite/action_service/result.rb +2 -0
  6. data/app/services/flexite/action_service.rb +13 -8
  7. data/app/services/flexite/config/create_service.rb +1 -1
  8. data/app/services/flexite/config/update_service.rb +1 -1
  9. data/app/services/flexite/entry/array_create_service.rb +16 -28
  10. data/app/services/flexite/entry/array_update_service.rb +34 -48
  11. data/app/services/flexite/entry/create_service.rb +1 -5
  12. data/app/services/flexite/entry/inner_processable.rb +32 -0
  13. data/app/services/flexite/entry/update_service.rb +1 -0
  14. data/app/views/flexite/entries/new_array_entry.js.haml +1 -1
  15. data/app/views/flexite/entries/update.js.haml +1 -0
  16. data/lib/flexite/version.rb +1 -1
  17. data/test/dummy/config/initializers/flexite.rb +1 -0
  18. data/test/dummy/db/development.sqlite3 +0 -0
  19. data/test/dummy/log/development.log +34804 -0
  20. data/test/dummy/tmp/cache/assets/C67/060/sprockets%2Ffaf176441f0544dd2b51901280044b40 +0 -0
  21. data/test/dummy/tmp/cache/assets/C9D/530/sprockets%2Fdcd49c063327c12052812f41c20a8e74 +0 -0
  22. data/test/dummy/tmp/cache/assets/CA3/270/sprockets%2F502b740063f8ec15e7e12811da71c772 +0 -0
  23. data/test/dummy/tmp/cache/assets/CDC/060/sprockets%2F8ff1d307d1b36810549d0829722b7aea +0 -0
  24. data/test/dummy/tmp/cache/assets/CDE/120/sprockets%2F5fd8b3fa3724451579552aed373410ce +0 -0
  25. data/test/dummy/tmp/cache/assets/CF8/980/sprockets%2F4e5077b95460dc34d7c9d7d9880e7b47 +0 -0
  26. data/test/dummy/tmp/cache/assets/D00/4C0/sprockets%2F603c6d7b825c2f4a6422b3d4af4e6203 +0 -0
  27. data/test/dummy/tmp/cache/assets/D00/C40/sprockets%2F999847c008fb4ce26fff5607c39d8358 +0 -0
  28. data/test/dummy/tmp/cache/assets/D09/A30/sprockets%2F2a71e20a2f3544acb51956504cd8d8e9 +0 -0
  29. data/test/dummy/tmp/cache/assets/D0D/E60/sprockets%2F90ea9eaa4671ec2d76703bae31972634 +0 -0
  30. data/test/dummy/tmp/cache/assets/D2A/160/sprockets%2Fbe17d4d0be4b381500e2824bbe273d70 +0 -0
  31. data/test/dummy/tmp/cache/assets/D2E/700/sprockets%2Fb21f85e0940bbcb3a8914c9cb0b07218 +0 -0
  32. data/test/dummy/tmp/cache/assets/D39/CD0/sprockets%2F0e8f6981475e49ea9fe14698fa57e4e9 +0 -0
  33. data/test/dummy/tmp/cache/assets/D40/590/sprockets%2F181dd5673b58f1ec4c89e50028bfad60 +0 -0
  34. data/test/dummy/tmp/cache/assets/D46/870/sprockets%2Fd880bdf72c5d0009b88fda8521439dc8 +0 -0
  35. data/test/dummy/tmp/cache/assets/D48/5F0/sprockets%2Fe6a83afb2d92f4692ffb391b5285a518 +0 -0
  36. data/test/dummy/tmp/cache/assets/D4B/A20/sprockets%2Febde89014596e655c35df9c4a01ee636 +0 -0
  37. data/test/dummy/tmp/cache/assets/D54/A50/sprockets%2Ff49839d906f69fd92904ebac07a3a38e +0 -0
  38. data/test/dummy/tmp/cache/assets/D69/B50/sprockets%2F4186e2787004e19ceb0c4afed46cf04b +0 -0
  39. data/test/dummy/tmp/cache/assets/D6D/C20/sprockets%2F112e96fea93f5f1e31d2077f1cdaf283 +0 -0
  40. data/test/dummy/tmp/cache/assets/D7A/8F0/sprockets%2F8eefc5f9824d950317a5c4a2e68b3c4e +0 -0
  41. data/test/dummy/tmp/cache/assets/DB3/360/sprockets%2F24b1f98ad736884b91d6ebeb2fc68ed8 +0 -0
  42. data/test/dummy/tmp/cache/assets/DD2/E90/sprockets%2Ff01f48c96ba588d15b20dd57ed3ccb7f +0 -0
  43. data/test/dummy/tmp/cache/assets/DEE/0C0/sprockets%2Feedcd74856542eedb5ea3ab1b1502ca4 +0 -0
  44. data/test/dummy/tmp/cache/assets/DF1/210/sprockets%2Ff4fe7dba1445ba1b668aaa1bd34ca984 +0 -0
  45. data/test/dummy/tmp/cache/assets/E44/790/sprockets%2Fefb8d8ea9e97da9d675a8bfa1d5de343 +0 -0
  46. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 306473e426e0b8dc5558ad5e2a5888225529c412
4
- data.tar.gz: 92484b6de2f9fa00135090fdc528150eff775d84
3
+ metadata.gz: 63dfd3a5a6e385f71c19663ad3baa5638e8fc29e
4
+ data.tar.gz: 189ee657807f3b2d7e502a542252c57b6993db85
5
5
  SHA512:
6
- metadata.gz: 353a687cd67cc63e11d5dab0cbe2dff41f1443eb39e1e6110c23979ad07a30273885452a96c8eeb859fe6977d1136322891d44e97dc25ed45ad760098e8162c2
7
- data.tar.gz: 56e88c74a1893daae25344f50b742991f7888effeb72d0ddf10a82bdf51e303cfa4db92ec12d0086f7f7aefcf80ae8476133f87aa77fa326f4c3d28384604fe3
6
+ metadata.gz: 12025176c14c3c4ef1559e9cfc385181fc37e319c7732ddc07319ed6101932e4062684038106257df654d19cce3709d7527da1faf81719441b895f958b38c4b0
7
+ data.tar.gz: 6b712735f3a2e606f2ca5ee2a20fc5de1de6dfbed5a4edb248b23f3d8a17f1af3fddf610d4a41e5fb3a930e516b545754b97e29394e947403715255dafd9e7ec
@@ -18,7 +18,7 @@ module Flexite
18
18
  result = ServiceFactory.instance.get(:config_create, Config::Form.new(config_params)).call
19
19
 
20
20
  if result.succeed?
21
- @node = result.data[:record].to_tree_node
21
+ @node = result.record.to_tree_node
22
22
  @parent_id = config_params[:config_id]
23
23
  end
24
24
 
@@ -34,7 +34,7 @@ module Flexite
34
34
  result = ServiceFactory.instance.get(:update_config, Config::Form.new(config_params)).call
35
35
 
36
36
  if result.succeed?
37
- @node = result.data[:record].to_tree_node
37
+ @node = result.record.to_tree_node
38
38
  end
39
39
 
40
40
  service_flash(result)
@@ -10,12 +10,10 @@ module Flexite
10
10
  end
11
11
 
12
12
  def create
13
- klass = entry_params[:type].constantize
14
- form = klass.form(entry_params)
15
- result = ServiceFactory.instance.get(klass.service(:create), form).call
13
+ result = call_service_for(:create, entry_params)
16
14
 
17
15
  if result.succeed?
18
- @entry = result.data[:record]
16
+ @entry = result.record
19
17
  @entry_form = @entry.class.form(@entry.form_attributes)
20
18
  end
21
19
 
@@ -29,16 +27,18 @@ module Flexite
29
27
  end
30
28
 
31
29
  def update
32
- klass = entry_params[:type].constantize
33
- form = klass.form(entry_params)
34
- result = ServiceFactory.instance.get(klass.service(:update), form).call
30
+ result = call_service_for(:update, entry_params)
31
+ @entry = Entry.find(entry_params[:id])
32
+ @entry_form = @entry.class.form(@entry.form_attributes)
35
33
  service_flash(result)
36
34
  service_response(result)
37
35
  end
38
36
 
39
37
  def new_array_entry
40
38
  klass = params[:type].constantize
41
- @form_options = [params[:prefix]]
39
+
40
+ @prefix = params[:prefix]
41
+ @form_options = [@prefix]
42
42
 
43
43
  if (@form_index = params[:form_index]).present?
44
44
  @form_options << { index: @form_index }
@@ -57,9 +57,7 @@ module Flexite
57
57
  end
58
58
 
59
59
  def destroy
60
- klass = params[:type].constantize
61
- form = klass.form(id: params[:id])
62
- result = ServiceFactory.instance.get(klass.service(:destroy), form).call
60
+ result = call_service_for(:destroy, params)
63
61
 
64
62
  if result.succeed?
65
63
  @parent_id = result.data[:parent_id]
@@ -78,5 +76,11 @@ module Flexite
78
76
  def entry_params
79
77
  params[:entry]
80
78
  end
79
+
80
+ def call_service_for(type, entry)
81
+ klass = entry[:type].constantize
82
+ form = klass.form(entry)
83
+ ServiceFactory.instance.get(klass.service(type), form).call
84
+ end
81
85
  end
82
86
  end
@@ -9,7 +9,7 @@ module Flexite
9
9
  ].freeze
10
10
 
11
11
  def render_entries(entries, form)
12
- content_tag :div, id: "array-entries-#{form.options[:index]}" do
12
+ content_tag :div, id: "#{form.object_name}-array-entries-#{form.options[:index]}" do
13
13
  entries.each_with_index do |entry, index|
14
14
  concat render_entry(entry, form, index)
15
15
  end
@@ -1,4 +1,6 @@
1
1
  class Flexite::ActionService::Result
2
+ attr_accessor :options
3
+
2
4
  def initialize(options = {})
3
5
  @options = options
4
6
  @errors = Hash.new { |h, k| h[k] = [] }
@@ -1,7 +1,6 @@
1
1
  class Flexite::ActionService
2
- def self.inherited(child)
3
- child.const_set(:Result, Result)
4
- end
2
+ extend ActiveSupport::Autoload
3
+ autoload :Result
5
4
 
6
5
  def initialize(form, params = {})
7
6
  @form = form
@@ -14,12 +13,9 @@ class Flexite::ActionService
14
13
 
15
14
  protected
16
15
 
17
- def success
18
- raise NotImplementedError
19
- end
20
-
21
16
  def failure
22
- raise NotImplementedError
17
+ save_errors
18
+ Result.new(success: false, endpoint: { status: 400 })
23
19
  end
24
20
 
25
21
  def save_errors(result)
@@ -27,4 +23,13 @@ class Flexite::ActionService
27
23
  result.add_errors(key, values)
28
24
  end
29
25
  end
26
+
27
+ def save_inner_service_errors(result, inner_result)
28
+ return if inner_result.errors.blank?
29
+ result.options[:succeed] = false if result.succeed?
30
+
31
+ inner_result.errors.each do |name, values|
32
+ result.add_errors(name, values)
33
+ end
34
+ end
30
35
  end
@@ -19,7 +19,7 @@ module Flexite
19
19
 
20
20
  def success
21
21
  Result.new(flash: { type: :success, message: 'Config was created!' },
22
- data: { record: @record })
22
+ data: @record)
23
23
  end
24
24
  end
25
25
  end
@@ -21,7 +21,7 @@ module Flexite
21
21
  end
22
22
 
23
23
  def success
24
- Result.new(flash: { type: :success, message: 'Config was updated!' }, data: { record: @record })
24
+ Result.new(flash: { type: :success, message: 'Config was updated!' }, data: @record)
25
25
  end
26
26
  end
27
27
  end
@@ -3,6 +3,8 @@ require_dependency 'flexite/action_service'
3
3
  module Flexite
4
4
  class Entry
5
5
  class ArrayCreateService < ActionService
6
+ include InnerProcessable
7
+
6
8
  def call
7
9
  if @form.invalid?
8
10
  return failure
@@ -19,51 +21,37 @@ module Flexite
19
21
  record.parent_type = @form.parent_type
20
22
  end
21
23
 
22
- Entry.transaction do
23
- create_entries(@form.new_entries, @record.id)
24
+ Entry.transaction(requires_new: true) do
25
+ create_entries(@form.new_entries)
26
+ end
27
+
28
+ if @process_result.succeed?
29
+ @process_result.options[:data] = @record
24
30
  end
25
31
 
26
- success
27
- rescue => exc
28
- exc_failure(exc)
32
+ process_result('Entry was created')
29
33
  end
30
34
 
31
- def create_entries(new_entries, parent_id)
35
+ def create_entries(new_entries)
32
36
  return if new_entries.blank?
33
37
 
34
38
  new_entries.each do |_, entry|
35
- if respond_to?("save_#{entry[:type].demodulize.underscore}", true)
36
- send("save_#{entry[:type].demodulize.underscore}", entry, parent_id)
37
- else
38
- save_entry(entry, parent_id)
39
- end
39
+ entry[:parent_id] = @record.id
40
+ entry[:parent_type] = @form.type.constantize.base_class.sti_name
41
+ save_entry(entry)
40
42
  end
41
43
  end
42
44
 
43
- def save_arr_entry(entry, parent_id)
44
- klass = entry[:type].constantize
45
- record = klass.create({ parent_id: parent_id, parent_type: klass.base_class.sti_name }, without_protection: true)
46
- create_entries(entry[:new_entries], record.id)
47
- end
48
-
49
- def save_entry(entry, parent_id)
50
- klass = entry[:type].constantize
51
- klass.create({ parent_id: parent_id, parent_type: klass.base_class.sti_name, value: entry[:value] }, without_protection: true)
45
+ def save_entry(entry)
46
+ call_service_for(:create, entry)
52
47
  end
53
48
 
54
49
  protected
55
50
 
56
51
  def failure
52
+ save_errors
57
53
  Result.new(success: false, endpoint: { status: 400 })
58
54
  end
59
-
60
- def exc_failure(exc)
61
- Result.new(success: false, message: exc.message, endpoint: { status: 500 })
62
- end
63
-
64
- def success
65
- Result.new(flash: { type: :success, message: 'Entry was created!' }, data: { record: @record })
66
- end
67
55
  end
68
56
  end
69
57
  end
@@ -1,69 +1,55 @@
1
1
  require_dependency 'flexite/action_service'
2
2
 
3
3
  module Flexite
4
- class Entry::ArrayUpdateService < ActionService
5
- def call
6
- if @form.invalid?
7
- return failure
4
+ class Entry
5
+ class ArrayUpdateService < ActionService
6
+ include InnerProcessable
7
+
8
+ def call
9
+ if @form.invalid?
10
+ return failure
11
+ end
12
+
13
+ process_entry
8
14
  end
9
15
 
10
- process_entries
11
- end
16
+ private
12
17
 
13
- private
18
+ def process_entry
19
+ Entry.transaction(requires_new: true) do
20
+ update_entries(@form.entries)
21
+ create_entries
22
+ end
14
23
 
15
- def process_entries
16
- Entry.transaction do
17
- update_entries(@form.entries)
18
- create_entries(@form.new_entries, @form.id)
24
+ process_result('Entry was updated')
19
25
  end
20
26
 
21
- success
22
- rescue => exc
23
- exc_failure(exc)
24
- end
25
-
26
- def update_entries(entries)
27
- entries.each do |_, entry|
28
- if respond_to?("save_#{entry[:type].demodulize.underscore}", true)
29
- send("save_#{entry[:type].demodulize.underscore}", entry)
30
- else
27
+ def update_entries(entries)
28
+ entries.each do |_, entry|
31
29
  save_entry(entry)
32
30
  end
33
31
  end
34
- end
35
32
 
36
- def create_entries(new_entries, parent_id)
37
- return if new_entries.blank?
38
-
39
- new_entries.each do |_, entry|
40
- klass = entry[:type].constantize
41
- klass.create({parent_id: parent_id, parent_type: klass.base_class.sti_name, value: entry[:value]}, without_protection: true)
33
+ def save_entry(entry)
34
+ call_service_for(:update, entry)
42
35
  end
43
- end
44
36
 
37
+ def create_entries
38
+ return if @form.new_entries.blank?
45
39
 
46
- def save_arr_entry(entry)
47
- update_entries(entry[:entries])
48
- create_entries(entry[:new_entries], entry[:id])
49
- end
50
-
51
- def save_entry(entry)
52
- entry[:type].constantize.update(entry[:id], value: entry[:value])
53
- end
54
-
55
- protected
56
-
57
- def failure
58
- Result.new(success: false, endpoint: { status: 400 })
59
- end
40
+ @form.new_entries.each do |_, entry|
41
+ entry[:parent_id] = @form.id
42
+ entry[:parent_type] = @form.type.constantize.base_class.sti_name
43
+ call_service_for(:create, entry)
44
+ end
45
+ end
60
46
 
61
- def exc_failure(exc)
62
- Result.new(success: false, message: exc.message, endpoint: { status: 500 })
63
- end
47
+ protected
64
48
 
65
- def success
66
- Result.new(flash: { type: :success, message: 'Entry was updated!' })
49
+ def failure
50
+ save_errors
51
+ Result.new(success: false, endpoint: { status: 400 })
52
+ end
67
53
  end
68
54
  end
69
55
  end
@@ -20,11 +20,7 @@ module Flexite
20
20
  private
21
21
 
22
22
  def success
23
- Result.new(flash: { type: :success, message: 'Entry was created!' }, data: { record: @record })
24
- end
25
-
26
- def failure
27
- Result.new(success: false, endpoint: { status: 400 })
23
+ Result.new(flash: { type: :success, message: 'Entry was created!' }, data: @record)
28
24
  end
29
25
  end
30
26
  end
@@ -0,0 +1,32 @@
1
+ module Flexite
2
+ class Entry
3
+ module InnerProcessable
4
+ extend ActiveSupport::Concern
5
+
6
+ def initialize(*)
7
+ super
8
+ @process_result = ActionService::Result.new
9
+ end
10
+
11
+ private
12
+
13
+ def call_service_for(type, entry)
14
+ klass = entry[:type].constantize
15
+ form = klass.form(entry)
16
+ result = ServiceFactory.instance.get(klass.service(type), form).call
17
+
18
+ if result.failed?
19
+ save_inner_service_errors(@process_result, result)
20
+ end
21
+ end
22
+
23
+ def process_result(success_message)
24
+ if @process_result.succeed?
25
+ @process_result.options[:flash] = { type: :success, message: success_message }
26
+ end
27
+
28
+ @process_result
29
+ end
30
+ end
31
+ end
32
+ end
@@ -16,6 +16,7 @@ module Flexite
16
16
  protected
17
17
 
18
18
  def failure
19
+ save_errors
19
20
  Result.new(success: false, endpoint: { status: 400 })
20
21
  end
21
22
 
@@ -1 +1 @@
1
- $flexite("#array-entries-#{@form_index}").append("#{j render 'new_array_entry_form'}");
1
+ $flexite("[id='#{@prefix}-array-entries-#{@form_index}']").append("#{j render 'new_array_entry_form'}");
@@ -1 +1,2 @@
1
+ $flexite("#section-2").html("#{j render 'form'}");
1
2
  = render 'flexite/shared/show_flash'
@@ -1,3 +1,3 @@
1
1
  module Flexite
2
- VERSION = "0.0.2"
2
+ VERSION = '0.0.3'
3
3
  end
@@ -2,4 +2,5 @@ Flexite.configure do |c|
2
2
  c.paths = {
3
3
  app: ["#{Rails.root}/config/application.yml"]
4
4
  }
5
+ c.app_name = 'Dummy'
5
6
  end
Binary file