Module: PrettyAssociationInspect
- Extended by:
- PrettyAssociationInspect
- Included in:
- PrettyAssociationInspect
- Defined in:
- lib/pretty_association_inspect.rb,
lib/pretty_association_inspect/version.rb
Overview
モデルに関連情報を見やすく表示するメソッドを定義します。
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.0.6"
Instance Method Summary collapse
-
#all_models_define ⇒ Object
全てのモデルにメソッドを定義する.
-
#build_association_hash(model) ⇒ Object
アソシエーションをハッシュに変換.
- #build_association_node(start, max_cost) ⇒ Object
-
#jp_scripe(str) ⇒ Object
日本語だけ抽出.
-
#load_all_models ⇒ Object
全てのモデルを読み込み、モデル名配列を返す.
-
#pretty_association_inspect_define(klass) ⇒ Object
『関連を可愛く表示するメソッド』を定義する.
-
#printed(klass, model, associations_hash) ⇒ Object
表示.
-
#value_convert(k, v, klass) ⇒ Object
バリューを整形.
Instance Method Details
#all_models_define ⇒ Object
全てのモデルにメソッドを定義する
218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/pretty_association_inspect.rb', line 218 def all_models_define model_names_array = load_all_models model_names_array.each do |model_name| begin klass = eval(model_name) pretty_association_inspect_define(klass) rescue next end end end |
#build_association_hash(model) ⇒ Object
アソシエーションをハッシュに変換
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/pretty_association_inspect.rb', line 158 def build_association_hash(model) pretty_hash = model.reflect_on_all_associations.each_with_object({}) do |m, hash| name = m.class.to_s.gsub("ActiveRecord::Reflection::", "").gsub("Reflection", "").to_sym hash[name] ||= [] human_name = PrettyAssociationInspect.jp_scripe(m.klass.model_name.human) hash[name] << [ m.name, human_name ].compact.join(' | ') hash[name] = hash[name] end end |
#build_association_node(start, max_cost) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pretty_association_inspect.rb', line 81 def build_association_node(start, max_cost) models = ActiveRecord::Base.subclasses.map(&:name) data = models.each_with_object({}) do |model_name_str, hash| eval(model_name_str).reflect_on_all_associations.each do |m| model_single_name = eval(model_name_str).model_name.singular.to_sym model_multiple_name = eval(model_name_str).model_name.plural.to_sym hash[model_single_name] ||= [] hash[model_multiple_name] ||= [] hash[model_single_name] << [1, m.name] hash[model_multiple_name] << [1, m.name] end end graph = Graph.new(data) route_hash = {} data.each do |goal, v| next if start == goal new_route = graph.print_route(graph.minimum_route(start, goal, max_cost)) _model_ = ActiveRecord::Base.module_eval(goal.to_s.camelize.singularize).model_name.human rescue nil human_str = PrettyAssociationInspect.jp_scripe(_model_) route_hash["#{goal} #{human_str}"] = new_route if new_route end route_hash end |
#jp_scripe(str) ⇒ Object
日本語だけ抽出
212 213 214 215 |
# File 'lib/pretty_association_inspect.rb', line 212 def jp_scripe(str) japanese = Regexp.new(/[亜-熙ぁ-んァ-ヶ]/) str if japanese =~ str end |
#load_all_models ⇒ Object
全てのモデルを読み込み、モデル名配列を返す
231 232 233 234 235 |
# File 'lib/pretty_association_inspect.rb', line 231 def load_all_models models_file_path = Dir.glob(Rails.root.join("app/models/*")).grep(/rb\z/) models_file_path.each { |m| require(m) rescue next } return ActiveRecord::Base.subclasses.map(&:name) end |
#pretty_association_inspect_define(klass) ⇒ Object
『関連を可愛く表示するメソッド』を定義する
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/pretty_association_inspect.rb', line 107 def pretty_association_inspect_define(klass) klass.class_eval do |model| self.define_singleton_method(:to){ associations_hash = PrettyAssociationInspect.build_association_hash(model) PrettyAssociationInspect.printed(klass, model, associations_hash) return self.first || self } define_method(:to){ associations_hash = PrettyAssociationInspect.build_association_hash(model) PrettyAssociationInspect.printed(klass, model, associations_hash) return self } self.define_singleton_method(:toto){ |max_cost=1, start = nil| model_name_sym = model_name.singular.to_sym route_arr = PrettyAssociationInspect.build_association_node(start || model_name_sym, max_cost) ap route_arr.map { |route| [route.first, eval(route.last.to_s)] rescue nil } ap route_arr return nil } define_method(:toto){ |max_cost = 1, start = nil| model_name_sym = model_name.singular.to_sym route_arr = PrettyAssociationInspect.build_association_node(start || model_name_sym, max_cost) ap route_arr.map { |route| [route.first, eval(route.last.to_s)] rescue nil } ap route_arr return nil } self.define_singleton_method(:s){ |name = nil| results = Module.const_get(self.to_s).columns.select{|m| m.sql_type=~/char/}.map(&:name).map(&:to_sym).each_with_object([]) { |attr, records| result = self.where(self.arel_table[attr].matches("%#{name}%")) records << [attr, result.ids, result] }.select{|m| m.second.present? } ap results result_where = self.where(id: results.map(&:second).flatten) ap result_where return result_where } end end |
#printed(klass, model, associations_hash) ⇒ Object
表示
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/pretty_association_inspect.rb', line 171 def printed(klass, model, associations_hash) pretty_hash = {} begin klass.class_eval{|klass| klass.first.attributes.each{|k ,v| pretty_hash[k.to_sym] = [ PrettyAssociationInspect.value_convert(k, v, klass), PrettyAssociationInspect.jp_scripe(klass.human_attribute_name(k)), v ].compact.join(' | ') } } rescue => e ap e end ap "-"*100; ap "#{klass.name} #{jp_scripe(klass.model_name.human)}" ap "[クラスメソッド]" base_pattern = "(before|after|around)_(add|remove|restore)|_associated_records_for_|inherited" extr_pattern = "attribute_type_decorations|soft_de|_restore_callback|indexed_|_by_resource" delete_pattern = Regexp.new( [ base_pattern, extr_pattern ].join('|') ) class_m = model.methods(false) - model.instance_methods ap (class_m).delete_if{|name| delete_pattern.match(name) }.sort ap "[インスタンスメソッド]" instance_m = model.instance_methods(false) - model.superclass.instance_methods ap (instance_m).delete_if{|name| delete_pattern.match(name) }.sort ap "[バリデーション]" ap model.validators.map{|m| m.class.name.gsub(/Active|Record|Validations|Model|Validator|::/,"") .concat(" #{m.attributes.join(', ')} #{m.}") }.sort.uniq ap "[アソシエーション]" ap associations_hash ap "[詳細]" ap pretty_hash ap "-"*100 end |
#value_convert(k, v, klass) ⇒ Object
バリューを整形
148 149 150 151 152 153 154 155 |
# File 'lib/pretty_association_inspect.rb', line 148 def value_convert(k, v, klass) klass.class_eval { return columns_hash[k.to_s].type if v.blank? is_e = Object.const_defined?(:Enumerize) && first.send(k).kind_of?(Enumerize::Value) return "#{v} #{first.send(k).text} #{send(k).values} #{send(k).values.map(&:text)}" if is_e return v.strftime("%y年%m月%d日 %H:%M") if v.respond_to?(:strftime) } end |