Commit 82a7292c7b5ba0e2b4eb32584c77c596651c2e35

Authored by freedomprogramer
1 parent 18ab00ae
Exists in master

modify require style

lib/generators/has_magic_fields/install/install_generator.rb
1   -require 'rails/generators/base'
  1 +require 'rails/generators'
2 2  
3 3 module HasMagicFields
4 4 class InstallGenerator < Rails::Generators::Base
... ...
lib/has_magic_fields.rb
1 1 require "has_magic_fields/version"
2 2 require "has_magic_fields/extend"
3 3  
4   -module HasMagicFields
  4 +module HasMagicFields
5 5 class Engine < ::Rails::Engine
6 6 paths = Dir[File.dirname(__FILE__), '/app/models/**/']
7   -
8   - if !Rails.env.development?
9   - config.eager_load_paths += paths
  7 +
  8 + if !Rails.env.development?
  9 + config.eager_load_paths += paths
10 10 end
11 11 end
12 12 end
13   -
... ...
lib/has_magic_fields/extend.rb
... ... @@ -9,7 +9,7 @@ module HasMagicFields
9 9 # Associations
10 10 has_many :magic_attribute_relationships, :as => :owner, :dependent => :destroy
11 11 has_many :magic_attributes, :through => :magic_attribute_relationships, :dependent => :destroy
12   -
  12 +
13 13 # Inheritence
14 14 cattr_accessor :inherited_from
15 15  
... ... @@ -32,7 +32,7 @@ module HasMagicFields
32 32 end
33 33 alias_method :magic_fields_without_scoped, :magic_fields
34 34 end
35   -
  35 +
36 36 end
37 37  
38 38 included do
... ... @@ -44,13 +44,13 @@ module HasMagicFields
44 44  
45 45 def magic_fields_with_scoped(type_scoped = nil)
46 46 type_scoped = type_scoped.blank? ? self.class.name : type_scoped.classify
47   - if magic_fields_without_scoped
  47 + if magic_fields_without_scoped
48 48 magic_fields_without_scoped.where(type_scoped: type_scoped)
49 49 else
50 50 []
51 51 end
52 52 end
53   -
  53 +
54 54 def method_missing(method_id, *args)
55 55 super(method_id, *args)
56 56 rescue NoMethodError
... ... @@ -72,9 +72,9 @@ module HasMagicFields
72 72  
73 73 def valid?(context = nil)
74 74 output = super(context)
75   - magic_fields_with_scoped.each do |field|
  75 + magic_fields_with_scoped.each do |field|
76 76 if field.is_required?
77   - validates_presence_of(field.name)
  77 + validates_presence_of(field.name)
78 78 end
79 79 end
80 80 errors.empty? && output
... ... @@ -90,22 +90,22 @@ module HasMagicFields
90 90  
91 91 def write_magic_attribute(field_name, value)
92 92 field = find_magic_field_by_name(field_name)
93   - attribute = find_magic_attribute_by_field(field)
  93 + attribute = find_magic_attribute_by_field(field)
94 94 (attr = attribute.first) ? update_magic_attribute(attr, value) : create_magic_attribute(field, value)
95 95 end
96   -
  96 +
97 97 def find_magic_attribute_by_field(field)
98 98 magic_attributes.to_a.find_all {|attr| attr.magic_field_id == field.id}
99 99 end
100   -
  100 +
101 101 def find_magic_field_by_name(attr_name)
102 102 magic_fields_with_scoped.to_a.find {|column| column.name == attr_name}
103 103 end
104   -
  104 +
105 105 def create_magic_attribute(magic_field, value)
106 106 magic_attributes << MagicAttribute.create(:magic_field => magic_field, :value => value)
107 107 end
108   -
  108 +
109 109 def update_magic_attribute(magic_attribute, value)
110 110 magic_attribute.update_attributes(:value => value)
111 111 end
... ... @@ -121,5 +121,3 @@ module HasMagicFields
121 121 end
122 122 end
123 123 end
124   -
125   -
... ...