lib/kojac/kojac_rails.rb in kojac-0.11.0 vs lib/kojac/kojac_rails.rb in kojac-0.12.0

- old
+ new

@@ -76,13 +76,14 @@ else aObject.respond_to?(:attributes) ? ::KojacBaseSerializer : ActiveModel::DefaultSerializer end end - def to_jsono(aObject,aOptions) + def to_jsono(aObject,aOptions=nil) + aOptions ||= {} if aObject.is_a?(Hash) or aObject.respond_to? :attributes - serializer_for(aObject).new(aObject,aOptions).serializable_object + serializer_for(aObject).new(aObject,aOptions).serializable_hash elsif aObject.respond_to?(:to_ary) && aObject.first # Array item_sz = serializer_for(aObject.first) ActiveModel::ArraySerializer.new(aObject,aOptions.merge(each_serializer: item_sz)).as_json(aOptions) else aObject.as_json(aOptions) @@ -196,23 +197,21 @@ module ControllerOpMethods def self.included(aClass) #aClass.send :extend, ClassMethods - aClass.send :include, ActiveSupport::Callbacks - aClass.send :define_callbacks, :update_op, :scope => [:kind, :name] + # aClass.send :include, ActiveSupport::Callbacks + # aClass.send :define_callbacks, :update_op, :scope => [:kind, :name] end #module ClassMethods #end module_function public - attr_accessor :item - def results @results ||= {} end def deduce_model_class @@ -340,38 +339,39 @@ def merge_model_into_results(aItem,aResultKey=nil,aOptions=nil) ring = current_ring aResultKey ||= aItem.g? :kojac_key results[aResultKey] = (aItem && KojacUtils.to_jsono(aItem,scope: kojac_current_user)) - return unless policy = Kojac.policy!(kojac_current_user,aItem) - aOptions ||= {} - if included_assocs = aOptions[:include] - included_assocs = included_assocs.split(',') if included_assocs.is_a?(String) - included_assocs = [included_assocs] unless included_assocs.is_a?(Array) - included_assocs.map!(&:to_sym) if included_assocs.is_a?(Array) - p_assocs = policy.permitted_associations(:read) # *** - use_assocs = p_assocs.delete_if do |a| - if included_assocs.include?(a) and ma = aItem.class.reflect_on_association(a) - ![:belongs_to,:has_many].include?(ma.macro) # is supported association type - else - true # no such assoc + if policy = Kojac.policy!(kojac_current_user,aItem) + aOptions ||= {} + if included_assocs = aOptions[:include] + included_assocs = included_assocs.split(',') if included_assocs.is_a?(String) + included_assocs = [included_assocs] unless included_assocs.is_a?(Array) + included_assocs.map!(&:to_sym) if included_assocs.is_a?(Array) + p_assocs = policy.permitted_associations(:read) # *** + use_assocs = p_assocs.delete_if do |a| + if included_assocs.include?(a) and ma = aItem.class.reflect_on_association(a) + ![:belongs_to,:has_many].include?(ma.macro) # is supported association type + else + true # no such assoc + end end - end - use_assocs.each do |a| - next unless a_contents = aItem.send(a) - if a_contents.is_a? Array - contents_h = [] - a_contents.each do |sub_item| - results[sub_item.kojac_key] = KojacUtils.to_jsono(sub_item,scope: kojac_current_user) - #contents_h << sub_item.id + use_assocs.each do |a| + next unless a_contents = aItem.send(a) + if a_contents.is_a? Array + contents_h = [] + a_contents.each do |sub_item| + results[sub_item.kojac_key] = KojacUtils.to_jsono(sub_item,scope: kojac_current_user) + end + else + results[a_contents.kojac_key] = KojacUtils.to_jsono(a_contents,scope: kojac_current_user) end - #results[aResultKey] = contents_h - else - results[a_contents.kojac_key] = KojacUtils.to_jsono(a_contents,scope: kojac_current_user) end end end + results_insert_filter(results,aItem,aResultKey,aOptions) if respond_to?(:results_insert_filter) + results end public def read_op @@ -429,13 +429,13 @@ op = params[:op] result_key = nil model = deduce_model_class scope = Kojac.policy_scope(current_user, model, op) || model - if self.item = scope.load_by_key(op[:key],op) + if item = scope.load_by_key(op[:key],op) - run_callbacks :update_op do + #run_callbacks :update_op do policy = Kojac.policy!(kojac_current_user,item,op) item.update_permitted_attributes!(op[:value], policy) associations = policy.permitted_associations(:write) associations.each do |k| @@ -458,10 +458,10 @@ next unless assoc_item = item.send(a) next unless key = assoc_item.respond_to?(:kojac_key) && assoc_item.kojac_key #results[key] = assoc_item merge_model_into_results(assoc_item,key) end - end + #end end { key: op[:key], verb: op[:verb], result_key: result_key, \ No newline at end of file