Design pattern for a report with input data and configuration in Rails -
i need present user profile publicly, in user can choose show , not show. design is:
class report < activerecord::base belongs_to :user_data belongs_to :report_config delegate :show_name, :show_address, :to => :report_config delegate :name, :address, :to => :user_data def filter_data report = self.user_data report.name = nil if show_name.false? report.address = nil if show_address.false? return report end end class userdata < activerecord::base has_many :report end class reportconfig < activerecord::base has_many :report end
however, not design, because calling filter_data
on report object return child object. how can allow report
have attributes child objects?
i thinking of inheritance (i.e., report inherits userdata , reportconfig, won't work). other design patterns fit problem?
you can delegate attributes of user model want meta programming in ruby.
class report < activerecord::base belongs_to :user_data belongs_to :report_config delegate :show_name, :show_address, :to => :report_config self.class_eval #reject attributes don't want delegate userdata.new.attribute_names.reject { |n| %w(id created_at updated_at).include?(n) }.each |n| delegate n , to: :user_data end end def filter_data name = nil if show_name.false? address = nil if show_address.false? end end
when use it, initialize report:
report = report.find_by_user_data_id(your user data id) report.filter_data report.name report.address report.....
on other hand, need report object? using userdata , reportconfig?
class userdata < activerecord::base belongs_to :report_config delegate :show_name, :show_address, :to => :report_config def report_name name if show_name end def report_address address if show_address end end class reportconfig < activerecord::base end
i don't know detailed requirement , try supply option. hope helps :)
Comments
Post a Comment