在我的rails应用程序中,有一个包含大量统计数据和大量图表的报告页面,有超过40个值和超过12个图表可供表达。
有很多数据需要操作,所以我开始将代码移到装饰器上,但我不确定这是最好的方法,因为我的控制器现在看起来像狗屎。
我无法跟踪代码 - 已经过测试的,有效的,有多余的,所以维护性已经很糟糕了。
任何人都可以提供建议或提出务实的解决方案/方法,以防止我的项目变成绝对混乱吗?我的方法或过程是否存在根本错误,或者我可以从下面的示例代码中看到的Ruby / Rails知识差距,应该以另一种方式完成?
这是我的基础装饰器类,装饰器和我的控制器的一个例子:
基础装饰者:
module Insights
class InsightDecorator
attr_reader :insight
def initialize(insight)
@insight = insight
end
def method_missing(method_name, *args, &block)
insight.send(method_name, *args, &block)
end
def respond_to_missing?(method_name, include_private = false)
insight.respond_to?(method_name, include_private) || super
end
end
end
14个装饰者中的一个:
module Insights
class FanAdds < InsightDecorator
def previous
FanAdds.new(Insight.previous page_id, insight_end)
end
def get_likes_increase
previous.insight.is_a?(Insight) ? (get_monthly_total - previous.get_monthly_total).to_i : 0
end
def get_daily_total
begin
get_insight_by_title('Daily New Likes')['values']
rescue
logger.error 'No daily new likes found'
nil
end
end
def get_monthly_total
get_daily_adds.inject(0) {|total, fan| total + fan['value'] }
end
end
end
在insight contoller中显示方法(看起来很糟糕!有没有更好的方法来解决这个问题?!)
def show
@insight_page_impressions_gender = Insights::PageImpressionsGender.new(@insight)
@insight_page_monthly = Insights::PageMonthly.new(@insight)
@insight_page_stories = Insights::PageStories.new(@insight)
@insight_engagement_day_of_week = Insights::EngagementDayOfWeek.new(@insight)
@insight_charts_fan_growth = Insights::Charts::FanGrowth.new(@insight)
@insight_charts_impressions = Insights::Charts::PageImpressions.new(@insight)
@paid_organic_viral_impressions = Insights::Charts::PaidOrganicViralImpressions.new(@insight)
@story_type = Insights::Charts::StoryTypesOfImpressions.new(@insight)
@age_gender_impressions = Insights::Charts::AgeGenderOfImpressions.new(@insight)
@insight_charts_stories = Insights::Charts::Stories.new(@insight)
# ... and so on
@fan_adds = Insights::FanAdds.new(@insight)
end