Module: DutyFree::Extensions
- Defined in:
- lib/duty_free/extensions.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
._recurse_def(klass, array, import_template, order_by = [], assocs = [], joins = [], pre_prefix = '', prefix = '') ⇒ Object
Recurse and return two arrays – one with all columns in sequence, and one a hierarchy of nested hashes to be used with ActiveRecord’s .joins() to facilitate export.
-
._save_pending(to_be_saved) ⇒ Object
Called before building any object linked through a has_one or has_many so that foreign key IDs can be added properly to those new objects.
-
._template_columns(klass, import_template = nil) ⇒ Object
The snake-cased column alias names used in the query to export data.
- .included(base) ⇒ Object
Class Method Details
._recurse_def(klass, array, import_template, order_by = [], assocs = [], joins = [], pre_prefix = '', prefix = '') ⇒ Object
Recurse and return two arrays – one with all columns in sequence, and one a hierarchy of nested hashes to be used with ActiveRecord’s .joins() to facilitate export.
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/duty_free/extensions.rb', line 763 def self._recurse_def(klass, array, import_template, order_by = [], assocs = [], joins = [], pre_prefix = '', prefix = '') prefixes = ::DutyFree::Util._prefix_join([pre_prefix, prefix]) # Confirm we can actually navigate through this association prefix_assoc = (assocs.last&.klass || klass).reflect_on_association(prefix) if prefix.present? if prefix_assoc assocs = assocs.dup << prefix_assoc if prefix_assoc.macro == :has_many && (pk = prefix_assoc.active_record.primary_key) order_by << ["#{prefixes.tr('.', '_')}_", pk] end end array = array.inject([]) do |s, col| s + if col.is_a?(Hash) col.inject([]) do |s2, v| joins << { v.first.to_sym => (joins_array = []) } s2 + _recurse_def(klass, (v.last.is_a?(Array) ? v.last : [v.last]), import_template, order_by, assocs, joins_array, prefixes, v.first.to_sym).first end elsif col.nil? if assocs.empty? [] else # Bring in from another class joins << { prefix => (joins_array = []) } # %%% Also bring in uniques and requireds _recurse_def(klass, assocs.last.klass::IMPORT_TEMPLATE[:all], import_template, order_by, assocs, joins_array, prefixes).first end else [::DutyFree::Column.new(col, pre_prefix, prefix, assocs, klass, import_template[:as])] end end [array, joins] end |
._save_pending(to_be_saved) ⇒ Object
Called before building any object linked through a has_one or has_many so that foreign key IDs can be added properly to those new objects. Finally at the end also called to save everything.
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/duty_free/extensions.rb', line 722 def self._save_pending(to_be_saved) while (tbs = to_be_saved.pop) ais = (tbs.first.class.respond_to?(:around_import_save) && tbs.first.class.method(:around_import_save)) || (respond_to?(:around_import_save) && method(:around_import_save)) if ais # Send them the sub_obj even if it might be invalid so they can choose # to make it valid if they wish. ais.call(tbs.first) do |modded_obj = nil| modded_obj = (modded_obj || tbs.first) modded_obj.save if modded_obj&.valid? end elsif tbs.first.valid? tbs.first.save else puts "* Unable to save #{tbs.first.inspect}" end # puts "Save #{tbs.first.class.name} #{tbs.first&.id} #{!tbs.first.new_record?}" unless tbs[1].nil? || tbs.first.new_record? # puts "Calling #{tbs[1].class.name} #{tbs[1]&.id} .#{tbs[2]} #{tbs[3].class.name} #{tbs[3]&.id}" tbs[1].send(tbs[2], tbs[3]) end end end |
._template_columns(klass, import_template = nil) ⇒ Object
The snake-cased column alias names used in the query to export data
747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'lib/duty_free/extensions.rb', line 747 def self._template_columns(klass, import_template = nil) template_detail_columns = klass.instance_variable_get(:@template_detail_columns) if klass.instance_variable_get(:@template_import_columns) != import_template klass.instance_variable_set(:@template_import_columns, import_template) klass.instance_variable_set(:@template_detail_columns, (template_detail_columns = nil)) end unless template_detail_columns # puts "* Redoing *" template_detail_columns = _recurse_def(klass, import_template[:all], import_template).first.map(&:to_sym) klass.instance_variable_set(:@template_detail_columns, template_detail_columns) end template_detail_columns end |
.included(base) ⇒ Object
10 11 12 13 |
# File 'lib/duty_free/extensions.rb', line 10 def self.included(base) base.send :extend, ClassMethods base.send :extend, ::DutyFree::SuggestTemplate::ClassMethods end |