Skip to content

Instantly share code, notes, and snippets.

@apotema
Last active August 29, 2015 14:07
Show Gist options
  • Save apotema/9ede119305334df05968 to your computer and use it in GitHub Desktop.
Save apotema/9ede119305334df05968 to your computer and use it in GitHub Desktop.
module Qbwc
module OrderedFields
module ClassMethods
def field attribute_name
@attr_order ||= []
@attr_order << attribute_name
attr_accessor attribute_name
end
def attr_order
@attr_order
end
end
def self.included(base)
base.extend(ClassMethods)
end
def ordered_fields
new_hash = {}
for attribute in self.class.attr_order
value = send(attribute)
if value.present?
if value.respond_to?(:ordered_fields)
new_hash[attribute] = value.ordered_fields
else
new_hash[attribute] = value
end
end
end
new_hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment