Permalink
Browse files

adding dynamic attributes to views

git-svn-id: https://forgetmenot-svn.cvsdude.com/main/trunk@78 a23ba2f0-501e-4c28-b130-3ad736a215ff
  • Loading branch information...
1 parent f0e7fee commit 976c33e63388bcf6323be3c00b69c8c06cdd6388 Renat Akhmerov committed Jun 1, 2007
@@ -2,6 +2,7 @@ class CommonController < ApplicationController
layout 'forgetmenot'
include ApplicationHelper
before_filter :login_required
+ before_filter :load_dynamic_attributes, :only => [ :index, :list, :search, :show, :new, :edit ]
helper_method :entity_class_name
helper_method :entity_class
@@ -143,4 +144,8 @@ def entity_human_name
entity_name.humanize
end
+ def load_dynamic_attributes
+ @dynamic_attributes = params[:table_name] == 'contacts' ? DynamicAttribute.find(:all) : []
+ end
+
end
View
@@ -57,4 +57,27 @@ def self.create_attribute(a, recreate_index = true)
def display_name
!self.first_name.nil? || !self.last_name.nil? ? "#{self.first_name} #{self.last_name}".strip : "contact ##{self.id}"
end
+
+ alias_method :old_column_for_attribute, :column_for_attribute
+ def column_for_attribute(method_name)
+ obj = old_column_for_attribute(method_name)
+ obj.nil? ? DynamicAttribute.find_by_name(method_name) : obj
+ end
+
+ alias_method :old_update_attributes, :update_attributes
+ def update_attributes(attrs)
+ dynamic_time_attrs = {}
+ attrs.each_pair { |k, v|
+ if k =~ /(\w+)\W(\d)i/
+ dynamic_time_attrs[$1.to_sym] = [] if dynamic_time_attrs[$1.to_sym].nil?
+ dynamic_time_attrs[$1.to_sym][$2.to_i - 1] = v
+ else
+ update_attribute(k, v) if !v.nil? && !v.to_s.strip.empty?
+ end
+ }
+ dynamic_time_attrs.each_pair { |k, v|
+ update_attribute(k, Time.mktime(v[0], v[1], v[2], v[3], v[4]))
+ }
+ end
+
end
@@ -1,3 +1,7 @@
class DynamicAttribute < ActiveRecord::Base
after_create { |a| Contact.create_attribute(a) }
+ def type
+ self.type_name.to_sym
+ end
end
+
@@ -1,10 +1,10 @@
<%=
- @object.get_entity_columns.collect{ |column|
- result = "<p><label for=\"object_#{column.name}\">#{column.human_name}</label>"
+ (@object.get_entity_columns + @dynamic_attributes).collect{ |column|
+ result = "<p><label for=\"object_#{column.name}\">#{column.name.humanize}</label>"
result += " <i>Markdown available (<a href=\"http://maruku.rubyforge.org/#features\">see syntax details</a>)</i>" if column.name == 'notes'
result += "<br/>\n#{input('object', column.name)}"
result +="</p>\n"
- result
+ result
}.join("\n")
%>
@@ -1,15 +1,18 @@
<tr>
- <% if entity_class.emailable? %>
+ <% if entity_class.emailable? -%>
<th>&nbsp;</th>
- <% end %>
-
- <% entity_class.get_entity_columns.each do |column| %>
- <th><%= column.human_name %></th>
- <% end %>
+ <% end -%>
+ <% entity_class.get_entity_columns.each do |column| -%>
+ <th><%= column.human_name -%></th>
+ <% end -%>
- <% (entity_class.get_single_associations(true) +
- entity_class.get_has_many_through_associations(true) +
- entity_class.get_multiple_associations(true)).each do |association| %>
- <th><%= association.name.to_s.humanize %></th>
- <% end %>
+ <% @dynamic_attributes.each do |a| -%>
+ <th><%= a.name.humanize %></th>
+ <% end -%>
+
+ <% (entity_class.get_single_associations(true) +
+ entity_class.get_has_many_through_associations(true) +
+ entity_class.get_multiple_associations(true)).each do |association| -%>
+ <th><%= association.name.to_s.humanize -%></th>
+ <% end -%>
</tr>
@@ -5,7 +5,7 @@
<%= check_box_tag "contact_ids[]", object.id %>
</td>
<% end %>
- <% object.get_entity_columns.each do |column| %>
+ <% (object.get_entity_columns + @dynamic_attributes).each do |column| %>
<td>
<% if column.name == 'notes' -%>
<%= markdown object.send(column.name) -%>
@@ -1,12 +1,12 @@
<%= breadcrumbs(@object) %>
-<% @object.get_entity_columns.each do |column| -%>
+<% (@object.get_entity_columns + @dynamic_attributes).each do |column| -%>
<p>
<% if column.name == 'notes' -%>
- <b><%= column.human_name %>:</b>
+ <b><%= column.name.humanize %>:</b>
<%= markdown @object.send(column.name) -%>
<% else %>
- <b><%= column.human_name %>:</b>
+ <b><%= column.name.humanize %>:</b>
<%= h @object.send(column.name) -%>
<% end -%>
</p>
@@ -9,27 +9,19 @@
<%= stylesheet_link_tag 'forgetmenot' %>
</head>
<body>
-
<div>
<% if logged_in? -%>
- <%= current_user.login %>
- (<%=link_to 'logout', :controller => 'account', :action => 'logout' -%>)
+ <%= current_user.login -%> (<%=link_to 'logout', :controller => 'account', :action => 'logout' -%>)
<% end -%>
</div>
-
<div id="main_menu">
<ul>
- <% get_table_names.each { |c| %>
- <li <%= "class='active'" if params[:table_name] == c %>>
- <%= link_to c.humanize, :controller => c %>
- </li>
- <% } %>
+ <% get_table_names.each { |c| -%>
+ <li<%= " class='active'" if params[:table_name] == c -%>><%= link_to c.humanize, :controller => c -%></li>
+ <% } -%>
</ul>
</div>
-
- <p style="color: green"><%= flash[:notice] %></p>
-
- <%= yield %>
-
+ <p style="color: green"><%= flash[:notice] -%></p>
+ <%= yield -%>
</body>
</html>

0 comments on commit 976c33e

Please sign in to comment.