Last active
August 29, 2015 14:07
-
-
Save apotema/9ede119305334df05968 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 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