<commit_before>module Archetype
  module Resourceful
    module Base
      extend ActiveSupport::Concern

      included do
        helper_method :resource_class
        delegate :resource_collection_name, :resource_instance_name, to: :class
      end

      module ClassMethods
        def resource_class
          controller_name.classify.safe_constantize
        end

        def resource_collection_name
          archetype_name.to_sym
        end

        def resource_instance_name
          archetype_name.singularize.to_sym
        end
      end

      protected

      def collection
        get_collection_ivar || begin
          c = end_of_association_chain
          set_collection_ivar(c.is_a?(ActiveRecord::Relation) ? c : c.all)
        end
      end

      def resource
        get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id]))
      end

      def build_resource
        get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, *resource_params))
      end

      def create_resource(object)
        object.save
      end

      def update_resource(object, attributes)
        object.update_attributes(attributes)
      end

      def destroy_resource(object)
        object.destroy
      end

      def resource_class
        @resource_class ||= self.class.resource_class
      end

      private

      def end_of_association_chain 
        apply_scopes_if_available(resource_class)
      end

      def method_for_build 
        :build
      end

      def method_for_find
        :find
      end

      def get_resource_ivar 
        instance_variable_get("@#{resource_instance_name}")
      end

      def set_resource_ivar(resource) 
        instance_variable_set("@#{resource_instance_name}", resource)
      end

      def get_collection_ivar 
        instance_variable_get("@#{resource_collection_name}")
      end

      def set_collection_ivar(collection) 
        instance_variable_set("@#{resource_collection_name}", collection)
      end

      def apply_scopes_if_available(target_object) 
        respond_to?(:apply_scopes, true) ? apply_scopes(target_object) : target_object
      end

    end
  end
end
<commit_msg>New for class, build for assoc<commit_after>module Archetype
  module Resourceful
    module Base
      extend ActiveSupport::Concern

      included do
        helper_method :resource_class
        delegate :resource_collection_name, :resource_instance_name, to: :class
      end

      module ClassMethods
        def resource_class
          controller_name.classify.safe_constantize
        end

        def resource_collection_name
          archetype_name.to_sym
        end

        def resource_instance_name
          archetype_name.singularize.to_sym
        end
      end

      protected

      def collection
        get_collection_ivar || begin
          c = end_of_association_chain
          set_collection_ivar(c.is_a?(ActiveRecord::Relation) ? c : c.all)
        end
      end

      def resource
        get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_find, params[:id]))
      end

      def build_resource
        get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, *resource_params))
      end

      def create_resource(object)
        object.save
      end

      def update_resource(object, attributes)
        object.update_attributes(attributes)
      end

      def destroy_resource(object)
        object.destroy
      end

      def resource_class
        @resource_class ||= self.class.resource_class
      end

      private

      def end_of_association_chain 
        apply_scopes_if_available(resource_class)
      end

      def method_for_build 
        :new
      end

      def method_for_find
        :find
      end

      def get_resource_ivar 
        instance_variable_get("@#{resource_instance_name}")
      end

      def set_resource_ivar(resource) 
        instance_variable_set("@#{resource_instance_name}", resource)
      end

      def get_collection_ivar 
        instance_variable_get("@#{resource_collection_name}")
      end

      def set_collection_ivar(collection) 
        instance_variable_set("@#{resource_collection_name}", collection)
      end

      def apply_scopes_if_available(target_object) 
        respond_to?(:apply_scopes, true) ? apply_scopes(target_object) : target_object
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.054748603351955305, 'fraction_numerical': 0.0, 'mean_word_length': 2.4590417310664607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5504795', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1259, 'n_words': 237}