custom_attributes_scaffold 0.1.3 → 0.1.4
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.
- checksums.yaml +4 -4
- data/app/models/custom_attributes/custom_attribute_definition.rb +7 -3
- data/app/models/custom_attributes/custom_attribute_value.rb +6 -0
- data/app/views/custom_attributes/custom_attribute_definitions/_form_fields_only.html.erb +5 -0
- data/lib/custom_attributes/version.rb +1 -1
- data/lib/generators/custom_attributes/templates/application_helper.rb +6 -6
- data/lib/generators/custom_attributes/templates/custom_attribute_definition_model.rb +7 -11
- data/lib/generators/custom_attributes/templates/custom_attributes_concern.rb +12 -0
- data/lib/generators/custom_attributes/templates/custom_attributes_inline_editing.js.erb +23 -13
- data/lib/generators/custom_attributes/templates/migration.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7970f62ea0887f6ca181d74a9387db5a0d99d32f
|
4
|
+
data.tar.gz: a7ad54b0f73040ee73d9e26ca1c25c1b08c12094
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 940ab3bec3d9be62c741d6c7175e2d250f0040f05680a2b7e9e03d074e7d33534538868e0d4057cb1fa69b7277897fd8a0fe004d917c14ba97c1b06800ecfca3
|
7
|
+
data.tar.gz: 69932678d6fc56dc33ee5c39d38e2543df93c39f7951c493ed7455f00954502e0651f4b9b6d1b61bf82d4de566ac220ca59b6b1c2d969da7888c0237e46e9ea1
|
@@ -2,9 +2,13 @@ module CustomAttributes
|
|
2
2
|
module CustomAttributeDefinition
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
included do
|
5
|
-
|
6
|
-
|
5
|
+
scope :by_sort_order, -> { order('sort_order ASC') }
|
6
|
+
validates :attr_name, format: { with: /^[a-zA-Z\_\s]+$/, multiline: true, message: 'cannot contain special characters.' }
|
7
|
+
validates :default_value, format: { with: /^[\+\-]?\d*\.?\d*$/, multiline: true, message: 'should be a number.' }, if: :number_type?
|
7
8
|
|
9
|
+
def number_type?
|
10
|
+
[CustomAttributes::CustomAttribute::TYPE_NUMBER, CustomAttributes::CustomAttribute::TYPE_DECIMAL].include?(attr_type)
|
11
|
+
end
|
8
12
|
end
|
9
13
|
end
|
10
|
-
end
|
14
|
+
end
|
@@ -11,6 +11,11 @@
|
|
11
11
|
<%= f.select :attr_type, custom_attribute_types.invert, {}, {disabled: !new_record}%>
|
12
12
|
</div>
|
13
13
|
|
14
|
+
<div class="custom-attribute-definition title <%=titleClass%>">Default Value:</div>
|
15
|
+
<div class="custom-attribute-definition field <%=fieldClass%>">
|
16
|
+
<%= f.text_field :default_value %>
|
17
|
+
</div>
|
18
|
+
|
14
19
|
<% if new_record %>
|
15
20
|
<%= f.hidden_field :sort_order %>
|
16
21
|
<% end %>
|
@@ -2,12 +2,12 @@ module CustomAttributes
|
|
2
2
|
module CustomAttributesHelper
|
3
3
|
def custom_attribute_types
|
4
4
|
types = {
|
5
|
-
CustomAttribute::TYPE_NUMBER => "Number",
|
6
|
-
CustomAttribute::TYPE_DECIMAL => "Decimal",
|
7
|
-
#
|
8
|
-
#
|
9
|
-
CustomAttribute::TYPE_TEXT => "Single Line Text",
|
10
|
-
CustomAttribute::TYPE_MULTILINE_TEXT => "Paragraph Text"
|
5
|
+
CustomAttributes::CustomAttribute::TYPE_NUMBER => "Number",
|
6
|
+
CustomAttributes::CustomAttribute::TYPE_DECIMAL => "Decimal",
|
7
|
+
# CustomAttributes::CustomAttribute::TYPE_DATE => "Date",
|
8
|
+
# CustomAttributes::CustomAttribute::TYPE_DATE_TIME => "Date Time",
|
9
|
+
CustomAttributes::CustomAttribute::TYPE_TEXT => "Single Line Text",
|
10
|
+
CustomAttributes::CustomAttribute::TYPE_MULTILINE_TEXT => "Paragraph Text"
|
11
11
|
}
|
12
12
|
end
|
13
13
|
end
|
@@ -1,17 +1,13 @@
|
|
1
1
|
class <%= name %>CustomAttributeDefinition < ActiveRecord::Base
|
2
2
|
include CustomAttributes::CustomAttributeDefinition
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
<% if options.tenant %>
|
3
|
+
<% if options.tenant %>
|
4
|
+
validates :attr_name, uniqueness: { scope: :<%= options.tenant %>_id, message: " has already been taken." }
|
5
|
+
<% else %>
|
6
|
+
validates :attr_name, uniqueness: true, message: " has already been taken."
|
7
|
+
<% end %>
|
8
|
+
<% if options.tenant %>
|
11
9
|
scope :current_tenant, -> <%= options.tenant %>_id { where(<%= options.tenant %>_id: <%= options.tenant %>_id) }
|
12
10
|
# Uncomment the following if you want to define a default_scope instead
|
13
11
|
# default_scope { where(<%= options.tenant %>_id: Tenant.current_tenant) }
|
14
|
-
|
15
|
-
validates_uniqueness_of :attr_name, scope: :company_id, message: 'a custom field with this name already exists.'
|
16
|
-
validates_format_of :attr_name, with: /^[a-zA-Z0-9\_\s]+$/, multiline: true, message: 'special characters are not allowed in the name.'
|
12
|
+
<% end %>
|
17
13
|
end
|
@@ -1,8 +1,20 @@
|
|
1
1
|
module <%= name %>CustomAttributes
|
2
2
|
extend ActiveSupport::Concern
|
3
|
+
# TODO: Move most of this file to a common parent
|
3
4
|
|
4
5
|
included do
|
5
6
|
has_many :custom_attribute_values, :class_name=>'<%= name %>CustomAttributeValue', autosave: true
|
7
|
+
after_initialize :add_custom_attributes_with_default_values
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_custom_attributes_with_default_values
|
11
|
+
return unless self.new_record?
|
12
|
+
return if self.custom_attributes.blank?
|
13
|
+
self.custom_attributes.each do |cav|
|
14
|
+
unless cav.custom_attribute_defn.default_value.blank?
|
15
|
+
self.custom_attribute_values.push(cav)
|
16
|
+
end
|
17
|
+
end
|
6
18
|
end
|
7
19
|
|
8
20
|
def custom_attributes=(map_of_custom_attributes)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
$.fn.inline_editable = function() {
|
2
2
|
|
3
|
-
var CLICK_TO_EDIT_HTML = "
|
3
|
+
var CLICK_TO_EDIT_HTML = "--"
|
4
4
|
|
5
5
|
return this.each(function () {
|
6
6
|
var $this = $(this);
|
@@ -8,35 +8,45 @@ $.fn.inline_editable = function() {
|
|
8
8
|
var attr_name = $(this).data('attr-name');
|
9
9
|
var controller = $(this).data('controller');
|
10
10
|
|
11
|
-
if ($("div.value", $this).text().trim()
|
11
|
+
if ($("div.value", $this).text().trim()=='') {
|
12
12
|
$("div.value", $this).html(CLICK_TO_EDIT_HTML);
|
13
13
|
}
|
14
14
|
|
15
15
|
$("div.value", $this).click(function() {
|
16
16
|
$("div.value", $this).hide();
|
17
17
|
$("div.field", $this).show();
|
18
|
-
$("div.field input
|
19
|
-
$("div.field input", $this).focus();
|
18
|
+
$("div.field input,textarea", $this).focus();
|
20
19
|
});
|
21
20
|
|
22
|
-
$("div.field input,textarea", $this).
|
21
|
+
$("div.field input,textarea", $this).focusout(function() {
|
22
|
+
if (! this.validity.valid) {
|
23
|
+
var attrName = $(this).closest("div.custom-attributes.editable-inline").find("div.custom-attributes.title").text().trim()
|
24
|
+
alert("Invalid value for \'"+ attrName + "\'.");
|
25
|
+
$("div.field input", $this).val($("div.value", $this).text().trim());
|
26
|
+
$("div.field", $this).hide();
|
27
|
+
$("div.value", $this).show();
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
|
23
31
|
var url = '/' + controller + '.json';
|
24
32
|
var value = this.value;
|
25
33
|
$.ajax({
|
26
|
-
type:'POST'
|
27
|
-
url: url
|
28
|
-
data: $.param({ owner_id: owner_id, name: attr_name, value: value })
|
29
|
-
success: function(data) {
|
34
|
+
type:'POST'
|
35
|
+
,url: url
|
36
|
+
,data: $.param({ owner_id: owner_id, name: attr_name, value: value })
|
37
|
+
,success: function(data) {
|
30
38
|
if (data.value) {
|
31
|
-
|
39
|
+
strValue = '' + data.value;
|
40
|
+
$("div.field input,textarea", $this).val(data.value);
|
41
|
+
$("div.value", $this).html(strValue.replace(/\n/g, '<br/>'));
|
32
42
|
} else {
|
33
43
|
$("div.value", $this).html(CLICK_TO_EDIT_HTML);
|
34
44
|
}
|
45
|
+
|
46
|
+
$("div.field", $this).hide();
|
47
|
+
$("div.value", $this).show();
|
35
48
|
}
|
36
49
|
});
|
37
|
-
|
38
|
-
$("div.field", $this).hide();
|
39
|
-
$("div.value", $this).show();
|
40
50
|
});
|
41
51
|
|
42
52
|
});
|
@@ -4,6 +4,7 @@ class CreateCustomAttributesFor<%= name %> < ActiveRecord::Migration
|
|
4
4
|
create_table (:<%= singular_table_name %>_custom_attribute_definitions) do |t|
|
5
5
|
t.string :attr_name
|
6
6
|
t.string :attr_type
|
7
|
+
t.text :default_value
|
7
8
|
t.integer :sort_order
|
8
9
|
<% if options.tenant %>
|
9
10
|
t.integer :<%= options.tenant.underscore %>_id, foreign_key: true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_attributes_scaffold
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tariq Hussain
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06
|
11
|
+
date: 2015-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|