select_extra_columns 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.markdown CHANGED
@@ -1,7 +1,10 @@
1
- 2010-04-08
1
+ 2010-04-09
2
2
  ==========
3
- * Added stricter validation for :extra_columns input.
3
+ * Changed cloning to inheritance.
4
4
 
5
+ 2010-04-09
6
+ ==========
7
+ * Added stricter validation for :extra_columns input.
5
8
 
6
9
  2010-03-08
7
10
  ==========
data/README.markdown CHANGED
@@ -60,18 +60,33 @@ Usage
60
60
  users.first.street # returns the street
61
61
  users.first.active # returns true/false
62
62
 
63
- ### Input format for `:extra_options`
63
+
64
+ Dynamically added column fields are read only. Any value set to these fields are ignored during save.
65
+
66
+ user = User.first(:joins => :address, :select => "*, addresses.street as street",
67
+ :extra_columns => :street)
68
+ user.city # => "San Francisco"
69
+ ...
70
+ user.city = "Houston" # change the value
71
+ user.save
72
+
73
+ user = User.first(:joins => :address, :select => "*, addresses.street as street",
74
+ :extra_columns => :street)
75
+ user.city # => "San Francisco"
76
+
77
+
78
+
79
+ ### Input format for `:extra_columns`
64
80
 
65
81
  Option accepts String/Symbol/Array/Hash as input.
66
82
 
67
- Example:
68
83
 
69
- #String,Symbol format
84
+ #### String,Symbol format
70
85
  :extra_columns => :first_name # Single string field: `first_name`(type is inferred as string)
71
86
 
72
87
  :extra_columns => "first_name" # Single string field: `first_name`(type is inferred as string)
73
88
 
74
- # Hash format
89
+ #### Hash format
75
90
  :extra_columns => { # Two string fields and a boolean field
76
91
  :first_name => :string,
77
92
  :last_name => :string,
@@ -84,7 +99,7 @@ Example:
84
99
  "has_flag" => :boolean
85
100
  }
86
101
 
87
- # Array format
102
+ #### Array format
88
103
  :extra_columns => [ # Two string fields and a boolean field
89
104
  [:first_name, :string],
90
105
  [:last_name, :string],
@@ -98,7 +113,7 @@ Example:
98
113
  [:has_flag, :boolean]
99
114
  ]
100
115
 
101
- ### Valid data types of fields in `:extra_columns`
116
+ ### Valid data types for column fields in `:extra_columns`
102
117
  :binary
103
118
  :boolean
104
119
  :date
@@ -35,18 +35,16 @@ module SelectExtraColumns
35
35
  def prepare_extra_column_klass extra_columns
36
36
  extra_column_definitions = prepare_extra_column_definitions(extra_columns)
37
37
  return self if extra_column_definitions.empty?
38
- cols, cols_hash = self.columns, self.columns_hash
39
- self.clone.tap do |klass|
40
- class << klass
41
- attr_accessor :extra_columns
42
- # over ride readonly_attributes to include the extra_columns
43
- def readonly_attributes
44
- (super || []) + self.extra_columns.keys(&:to_s)
45
- end
38
+ read_only_attrs = extra_column_definitions.collect{|cd| ":#{cd.name}" }.join(",")
39
+ klass_name = "#{self.name}#{Time.now.to_i}#{extra_columns.hash.abs}"
40
+ class_eval(<<-RUBY, __FILE__, __LINE__)
41
+ class ::#{klass_name} < #{self.name}
42
+ set_table_name :#{self.table_name}
43
+ attr_readonly #{read_only_attrs}
44
+ class_inheritable_accessor :extra_columns
46
45
  end
47
- #Make new copy of @columns, and @columns_hash and @extra_columns variables
48
- klass.instance_variable_set("@columns", cols.clone)
49
- klass.instance_variable_set("@columns_hash", cols_hash.clone)
46
+ RUBY
47
+ klass_name.constantize.tap do |klass|
50
48
  klass.extra_columns = extra_columns.is_a?(Symbol) ? extra_columns : extra_columns.clone
51
49
  extra_column_definitions.each do |ecd|
52
50
  klass.columns << (klass.columns_hash[ecd.name] = ecd)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kandada Boggu