Last active
January 3, 2016 16:49
-
-
Save csaunders/8491743 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ShopProductSink | |
module ApiCreatable | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def initialize_from_resource(resource) | |
attributes = usable_keys.reduce({}) do |result, key| | |
result[key] = resource.public_send(key) if resource.respond_to?(key) | |
result | |
end | |
record = self.new(attributes) | |
reflect_on_all_associations.each do |association| | |
if resource.respond_to?(association.name) | |
resource_or_resources = resource.public_send(association.name) | |
records = association.active_record.initialize_from_resources(resource_or_resources) | |
record.public_send("#{association.name}=", association.collection? ? records : records.first) | |
end | |
end | |
end | |
def initialize_from_resources(resources) | |
Array[*resources].map { |resource| initialize_from_resource(resource) } | |
end | |
def create_from_resource(resource) | |
initialize_from_resource(resource).save | |
end | |
def create_from_resources(resources) | |
Array[*resources].map { |resource| create_from_resource(resource) } | |
end | |
def usable_keys | |
columns.map(&:name) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
module ShopProductSink | |
class ApiCreatableTest < ShopProductSink::UnitTest | |
attr_reader :resource | |
test "it should be possible to find initialize all relations for the object" do | |
integration_object_relations_resources = [new_resource(id: 1, integration_object_id: 1, sku: 'saucey', option1: 'red')] | |
resource = new_resource(integration_object_relations: integration_object_relations_resources) | |
object = IntegrationObject.initialize_from_resource(resource) | |
assert_equal 1, object.integration_object_relations.size | |
relation = object.integration_object_relations.first | |
assert_equal object.id, relation.integration_object_id | |
end | |
private | |
def new_resource(options={}) | |
defaults = {id: 1, title: 'Fox', price: '12.00'}.merge(options) | |
ShopifyAPI::Product.new(defaults) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Error: | |
ShopProductSink::ApiCreatableTest#test_it_should_be_possible_to_find_initialize_all_relations_for_the_object: | |
NameError: uninitialized constant IntegrationObjectRelation | |
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `const_get' | |
activesupport (4.0.2) lib/active_support/inflector/methods.rb:226:in `block in constantize' | |
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `each' | |
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `inject' | |
activesupport (4.0.2) lib/active_support/inflector/methods.rb:224:in `constantize' | |
activesupport (4.0.2) lib/active_support/core_ext/string/inflections.rb:66:in `constantize' | |
activerecord (4.0.2) lib/active_record/associations/association.rb:222:in `raise_on_type_mismatch!' | |
activerecord (4.0.2) lib/active_record/associations/collection_association.rb:333:in `block in replace' | |
activerecord (4.0.2) lib/active_record/associations/collection_association.rb:333:in `each' | |
activerecord (4.0.2) lib/active_record/associations/collection_association.rb:333:in `replace' | |
activerecord (4.0.2) lib/active_record/associations/collection_association.rb:42:in `writer' | |
activerecord (4.0.2) lib/active_record/associations/builder/association.rb:78:in `integration_object_relations=' | |
/home/vagrant/src/shopProductSink/app/models/shop_product_sink/api_creatable.rb:16:in `public_send' | |
/home/vagrant/src/shopProductSink/app/models/shop_product_sink/api_creatable.rb:16:in `block in initialize_from_resource' | |
/home/vagrant/src/shopProductSink/app/models/shop_product_sink/api_creatable.rb:12:in `each' | |
/home/vagrant/src/shopProductSink/app/models/shop_product_sink/api_creatable.rb:12:in `initialize_from_resource' | |
test/models/shop_product_sink/api_creatable_test.rb:61:in `block in <class:ApiCreatableTest>' | |
minitest (4.7.5) lib/minitest/unit.rb:1258:in `run' | |
minitest (4.7.5) lib/minitest/unit.rb:933:in `block in _run_suite' | |
minitest (4.7.5) lib/minitest/unit.rb:926:in `map' | |
minitest (4.7.5) lib/minitest/unit.rb:926:in `_run_suite' | |
minitest (4.7.5) lib/minitest/parallel_each.rb:71:in `block in _run_suites' | |
minitest (4.7.5) lib/minitest/parallel_each.rb:71:in `map' | |
minitest (4.7.5) lib/minitest/parallel_each.rb:71:in `_run_suites' | |
minitest (4.7.5) lib/minitest/unit.rb:877:in `_run_anything' | |
minitest (4.7.5) lib/minitest/unit.rb:1085:in `run_tests' | |
minitest (4.7.5) lib/minitest/unit.rb:1072:in `block in _run' | |
minitest (4.7.5) lib/minitest/unit.rb:1071:in `each' | |
minitest (4.7.5) lib/minitest/unit.rb:1071:in `_run' | |
minitest (4.7.5) lib/minitest/unit.rb:1059:in `run' | |
minitest (4.7.5) lib/minitest/unit.rb:795:in `block in autorun' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment