netzke-basepack 0.7.7 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +15 -10
- data/{CHANGELOG.rdoc → CHANGELOG.md} +146 -110
- data/LICENSE +7 -1
- data/README.md +47 -56
- data/Rakefile +5 -5
- data/config/before-travis.sh +10 -0
- data/javascripts/basepack.js +0 -130
- data/javascripts/netzkeremotecombo.js +59 -0
- data/lib/netzke/basepack.rb +9 -14
- data/lib/netzke/basepack/accordion.rb +45 -0
- data/lib/netzke/basepack/active_record.rb +12 -0
- data/lib/netzke/basepack/active_record/relation_extensions.rb +27 -0
- data/lib/netzke/basepack/columns.rb +309 -0
- data/lib/netzke/basepack/data_accessor.rb +22 -12
- data/lib/netzke/basepack/data_adapters/abstract_adapter.rb +75 -11
- data/lib/netzke/basepack/data_adapters/active_record_adapter.rb +154 -49
- data/lib/netzke/basepack/fields.rb +162 -0
- data/lib/netzke/basepack/form.rb +136 -0
- data/lib/netzke/basepack/{form_panel → form}/javascripts/comma_list_cbg.js +0 -1
- data/lib/netzke/basepack/{form_panel/javascripts/form_panel.js → form/javascripts/form.js} +20 -26
- data/lib/netzke/basepack/{form_panel → form}/javascripts/n_radio_group.js +0 -1
- data/lib/netzke/basepack/{form_panel → form}/javascripts/readonly_mode.js +0 -0
- data/lib/netzke/basepack/form/services.rb +115 -0
- data/lib/netzke/basepack/{form_panel → form}/stylesheets/readonly_mode.css +0 -0
- data/lib/netzke/basepack/grid.rb +355 -0
- data/lib/netzke/basepack/{grid_panel → grid}/javascripts/advanced_search.js +1 -1
- data/lib/netzke/basepack/{grid_panel → grid}/javascripts/check_column_fix.js +0 -0
- data/lib/netzke/basepack/{grid_panel → grid}/javascripts/edit_in_form.js +3 -3
- data/lib/netzke/basepack/{grid_panel → grid}/javascripts/event_handling.js +5 -2
- data/lib/netzke/basepack/{grid_panel/javascripts/grid_panel.js → grid/javascripts/grid.js} +120 -132
- data/lib/netzke/basepack/{grid_panel → grid}/javascripts/misc.js +0 -0
- data/lib/netzke/basepack/grid/services.rb +216 -0
- data/lib/netzke/basepack/item_persistence.rb +44 -0
- data/lib/netzke/basepack/item_persistence/events_plugin.rb +47 -0
- data/lib/netzke/basepack/{paging_form_panel.rb → paging_form.rb} +24 -30
- data/lib/netzke/basepack/{paging_form_panel/javascripts/paging_form_panel.js → paging_form/javascripts/paging_form.js} +2 -4
- data/lib/netzke/basepack/query_builder.rb +44 -73
- data/lib/netzke/basepack/query_builder/javascripts/query_builder.js +16 -2
- data/lib/netzke/basepack/record_form_window.rb +67 -0
- data/lib/netzke/basepack/search_panel.rb +22 -24
- data/lib/netzke/basepack/search_panel/javascripts/condition_field.js +2 -2
- data/lib/netzke/basepack/search_window.rb +47 -53
- data/lib/netzke/basepack/simple_app.rb +10 -13
- data/lib/netzke/basepack/simple_app/javascripts/simple_app.js +2 -8
- data/lib/netzke/basepack/tab_panel.rb +5 -4
- data/lib/netzke/basepack/tab_panel/javascripts/tab_panel.js +5 -5
- data/lib/netzke/basepack/version.rb +2 -2
- data/lib/netzke/basepack/viewport.rb +16 -0
- data/lib/netzke/basepack/window.rb +27 -18
- data/lib/netzke/basepack/window/javascripts/window.js +7 -1
- data/lib/netzke/basepack/wrap_lazy_loaded.rb +18 -18
- data/locales/en.yml +40 -24
- data/netzke-basepack.gemspec +51 -82
- data/stylesheets/basepack.css +0 -41
- data/test/basepack_test_app/Gemfile +9 -46
- data/test/basepack_test_app/Gemfile.lock +61 -96
- data/test/basepack_test_app/app/components/author_form.rb +8 -5
- data/test/basepack_test_app/app/components/author_grid.rb +2 -2
- data/test/basepack_test_app/app/components/book_form.rb +34 -31
- data/test/basepack_test_app/app/components/book_form_with_defaults.rb +6 -7
- data/test/basepack_test_app/app/components/book_form_with_file_upload.rb +10 -0
- data/test/basepack_test_app/app/components/book_form_with_nested_attributes.rb +5 -6
- data/test/basepack_test_app/app/components/book_grid.rb +19 -8
- data/test/basepack_test_app/app/components/book_grid_filtering.rb +4 -7
- data/test/basepack_test_app/app/components/book_grid_loader.rb +28 -15
- data/test/basepack_test_app/app/components/book_grid_with_custom_columns.rb +45 -21
- data/test/basepack_test_app/app/components/book_grid_with_default_values.rb +26 -8
- data/test/basepack_test_app/app/components/book_grid_with_excluded_columns.rb +11 -0
- data/test/basepack_test_app/app/components/book_grid_with_extra_feedback.rb +2 -2
- data/test/basepack_test_app/app/components/book_grid_with_extra_filters.rb +7 -6
- data/test/basepack_test_app/app/components/book_grid_with_mass_assignment_security.rb +9 -0
- data/test/basepack_test_app/app/components/book_grid_with_nested_attributes.rb +9 -9
- data/test/basepack_test_app/app/components/book_grid_with_overridden_columns.rb +5 -3
- data/test/basepack_test_app/app/components/book_grid_with_paging.rb +6 -8
- data/test/basepack_test_app/app/components/book_grid_with_persistence.rb +6 -4
- data/test/basepack_test_app/app/components/book_grid_with_scope.rb +6 -0
- data/test/basepack_test_app/app/components/book_grid_with_scoped_authors.rb +10 -7
- data/test/basepack_test_app/app/components/book_grid_with_virtual_attributes.rb +21 -13
- data/test/basepack_test_app/app/components/book_paging_form.rb +21 -0
- data/test/basepack_test_app/app/components/book_query_builder.rb +7 -6
- data/test/basepack_test_app/app/components/book_with_custom_primary_key_grid.rb +6 -7
- data/test/basepack_test_app/app/components/books_bound_to_author.rb +9 -7
- data/test/basepack_test_app/app/components/border_layout_panel_with_persistence.rb +12 -0
- data/test/basepack_test_app/app/components/double_book_grid.rb +19 -14
- data/test/basepack_test_app/app/components/form_without_model.rb +15 -16
- data/test/basepack_test_app/app/components/grid_with_initial_sorting.rb +7 -0
- data/test/basepack_test_app/app/components/grid_with_inline_data.rb +7 -0
- data/test/basepack_test_app/app/components/paging_form_with_search.rb +2 -2
- data/test/basepack_test_app/app/components/panel_with_persistent_regions.rb +35 -0
- data/test/basepack_test_app/app/components/query_builder.rb +7 -0
- data/test/basepack_test_app/app/components/simple_panel.rb +16 -11
- data/test/basepack_test_app/app/components/simple_window.rb +7 -6
- data/test/basepack_test_app/app/components/some_accordion.rb +18 -0
- data/test/basepack_test_app/app/components/some_auth_app.rb +5 -5
- data/test/basepack_test_app/app/components/some_border_layout.rb +20 -20
- data/test/basepack_test_app/app/components/some_search_panel.rb +6 -0
- data/test/basepack_test_app/app/components/some_simple_app.rb +30 -16
- data/test/basepack_test_app/app/components/some_tab_panel.rb +18 -15
- data/test/basepack_test_app/app/components/user_form.rb +18 -16
- data/test/basepack_test_app/app/components/user_form_with_default_fields.rb +5 -6
- data/test/basepack_test_app/app/components/user_grid.rb +11 -6
- data/test/basepack_test_app/app/components/user_grid_with_customized_form_fields.rb +5 -3
- data/test/basepack_test_app/app/components/window_component_loader.rb +25 -21
- data/test/basepack_test_app/app/models/address.rb +0 -26
- data/test/basepack_test_app/app/models/author.rb +0 -31
- data/test/basepack_test_app/app/models/book.rb +1 -42
- data/test/basepack_test_app/app/models/book_with_custom_primary_key.rb +1 -23
- data/test/basepack_test_app/app/models/role.rb +0 -21
- data/test/basepack_test_app/app/models/user.rb +0 -24
- data/test/basepack_test_app/app/views/layouts/components.html.erb +1 -1
- data/test/basepack_test_app/config/application.rb +1 -1
- data/test/basepack_test_app/config/database.yml.travis +2 -6
- data/test/basepack_test_app/config/initializers/netzke.rb +1 -6
- data/test/basepack_test_app/db/schema.rb +14 -14
- data/test/basepack_test_app/features/accordion_panel.feature +2 -2
- data/test/basepack_test_app/features/form_panel.feature +7 -7
- data/test/basepack_test_app/features/grid_panel.feature +93 -39
- data/test/basepack_test_app/features/grid_panel_with_custom_primary_key.feature +2 -1
- data/test/basepack_test_app/features/grid_sorting.feature +30 -6
- data/test/basepack_test_app/features/paging_form_panel.feature +7 -7
- data/test/basepack_test_app/features/persistent_regions.feature +30 -0
- data/test/basepack_test_app/features/search_in_grid.feature +5 -5
- data/test/basepack_test_app/features/simple_app.feature +6 -7
- data/test/basepack_test_app/features/step_definitions/form_panel_steps.rb +1 -1
- data/test/basepack_test_app/features/step_definitions/generic_steps.rb +109 -4
- data/test/basepack_test_app/features/step_definitions/grid_panel_steps.rb +8 -10
- data/test/basepack_test_app/features/step_definitions/window_steps.rb +27 -0
- data/test/basepack_test_app/features/tab_panel.feature +1 -1
- data/test/basepack_test_app/features/window.feature +17 -0
- data/test/unit/accordion_panel_test.rb +2 -2
- data/test/unit/grid_panel_test.rb +4 -4
- metadata +57 -83
- data/TODO.rdoc +0 -8
- data/lib/generators/netzke/basepack_generator.rb +0 -10
- data/lib/generators/netzke/templates/assets/ts-checkbox.gif +0 -0
- data/lib/generators/netzke/templates/create_netzke_field_lists.rb +0 -18
- data/lib/netzke/active_record.rb +0 -20
- data/lib/netzke/active_record/attributes.rb +0 -259
- data/lib/netzke/active_record/combobox_options.rb +0 -16
- data/lib/netzke/active_record/relation_extensions.rb +0 -37
- data/lib/netzke/basepack/accordion_panel.rb +0 -39
- data/lib/netzke/basepack/action_column.rb +0 -68
- data/lib/netzke/basepack/action_column/javascripts/action_column.js +0 -61
- data/lib/netzke/basepack/auth_app.rb +0 -159
- data/lib/netzke/basepack/basic_app.rb +0 -7
- data/lib/netzke/basepack/border_layout_panel.rb +0 -53
- data/lib/netzke/basepack/border_layout_panel/javascripts/border_layout_panel.js +0 -40
- data/lib/netzke/basepack/data_adapters/data_mapper_adapter.rb +0 -264
- data/lib/netzke/basepack/data_adapters/sequel_adapter.rb +0 -260
- data/lib/netzke/basepack/form_panel.rb +0 -144
- data/lib/netzke/basepack/form_panel/fields.rb +0 -208
- data/lib/netzke/basepack/form_panel/javascripts/misc.js +0 -4
- data/lib/netzke/basepack/form_panel/services.rb +0 -142
- data/lib/netzke/basepack/grid_panel.rb +0 -441
- data/lib/netzke/basepack/grid_panel/columns.rb +0 -400
- data/lib/netzke/basepack/grid_panel/javascripts/rows-dd.js +0 -281
- data/lib/netzke/basepack/grid_panel/record_form_window.rb +0 -41
- data/lib/netzke/basepack/grid_panel/services.rb +0 -235
- data/lib/netzke/basepack/panel.rb +0 -11
- data/lib/netzke/basepack/wrapper.rb +0 -28
- data/lib/netzke/data_mapper.rb +0 -18
- data/lib/netzke/data_mapper/attributes.rb +0 -273
- data/lib/netzke/data_mapper/combobox_options.rb +0 -11
- data/lib/netzke/data_mapper/relation_extensions.rb +0 -38
- data/lib/netzke/sequel.rb +0 -18
- data/lib/netzke/sequel/attributes.rb +0 -274
- data/lib/netzke/sequel/combobox_options.rb +0 -10
- data/lib/netzke/sequel/relation_extensions.rb +0 -40
- data/locales/zh-cn.yml +0 -79
- data/test/basepack_test_app/app/components/book_form_with_custom_fields.rb +0 -21
- data/test/basepack_test_app/app/components/book_grid_with_column_actions.rb +0 -15
- data/test/basepack_test_app/app/components/book_grid_with_defaults.rb +0 -6
- data/test/basepack_test_app/app/components/book_paging_form_panel.rb +0 -22
- data/test/basepack_test_app/app/components/generic_user_form.rb +0 -12
- data/test/basepack_test_app/app/components/simple_accordion.rb +0 -11
- data/test/basepack_test_app/app/components/simple_tab_panel.rb +0 -11
- data/test/basepack_test_app/app/components/simple_wrapper.rb +0 -7
- data/test/basepack_test_app/app/components/some_accordion_panel.rb +0 -22
- data/test/basepack_test_app/app/presenters/forms/generic_user.rb +0 -6
- data/test/basepack_test_app/app/views/components/loadable_window.html.erb +0 -9
- data/test/basepack_test_app/app/views/components/simple_panel.html.erb +0 -1
- data/test/basepack_test_app/features/components_in_view.feature +0 -11
- data/test/basepack_test_app/features/simple_panel.feature +0 -11
- data/test/basepack_test_app/features/validations_in_grid.feature +0 -13
- data/test/basepack_test_app/features/virtual_attributes.feature +0 -16
- data/test/basepack_test_app/spec/components/form_panel_spec.rb +0 -53
- data/test/basepack_test_app/spec/components/grid_panel_spec.rb +0 -10
- data/test/basepack_test_app/spec/data_adapter/adapter_spec.rb +0 -68
- data/test/basepack_test_app/spec/data_adapter/attributes_spec.rb +0 -56
- data/test/basepack_test_app/spec/data_adapter/relation_extensions_spec.rb +0 -125
- data/test/basepack_test_app/spec/factories.rb +0 -28
- data/test/basepack_test_app/spec/spec_helper.rb +0 -39
@@ -1,11 +0,0 @@
|
|
1
|
-
module Netzke
|
2
|
-
module DataMapper
|
3
|
-
module ComboboxOptions
|
4
|
-
def netzke_combo_options_for(column, query = "")
|
5
|
-
# NOTE: :order=>[column.to_sym.asc] is necessary as per http://datamapper.org/docs/find.html, Version 1.2.0
|
6
|
-
values=all(:fields=>[column], :unique=>true, :order=>[column.to_sym.asc])
|
7
|
-
(query.blank? ? values : values.all(column.to_sym.like => "#{query}%")).map &column.to_sym
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Netzke
|
2
|
-
module DataMapper
|
3
|
-
module RelationExtensions
|
4
|
-
|
5
|
-
def extend_with(*params)
|
6
|
-
scope = params.shift
|
7
|
-
case scope.class.name
|
8
|
-
when "Symbol" # model's scope
|
9
|
-
# In DataMapper case this is just a method
|
10
|
-
self.send(scope, *params)
|
11
|
-
when "String" # SQL query or SQL query with params (e.g. ["created_at < ?", 1.day.ago])
|
12
|
-
raise NotImplementedError.new("This method is unsupported, as DM doen't allow to extend relations with SQL")
|
13
|
-
when "Array"
|
14
|
-
self.extend_with(*scope)
|
15
|
-
when "Hash" # conditions hash
|
16
|
-
self.all(scope)
|
17
|
-
when "ActiveSupport::HashWithIndifferentAccess" # conditions hash
|
18
|
-
self.all(scope)
|
19
|
-
when "Proc" # receives a relation, must return a relation
|
20
|
-
scope.call(self)
|
21
|
-
else
|
22
|
-
raise ArgumentError, "Wrong parameter type for ActiveRecord::Relation#extend_with"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Non-destructively extends itself whith a hash of double-underscore'd conditions,
|
27
|
-
# where the last part "__" is MetaWhere operator (which is required), e.g.:
|
28
|
-
# {:role__name__like => "%admin"}
|
29
|
-
def extend_with_netzke_conditions(cond)
|
30
|
-
cond.each_pair.inject(self) do |r, (k,v)|
|
31
|
-
assoc, method, *operator = k.to_s.split("__")
|
32
|
-
operator.empty? ? r.where(assoc.to_sym.send(method) => v) : r.where(assoc.to_sym => {method.to_sym.send(operator.last) => v}).joins(assoc.to_sym)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/lib/netzke/sequel.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'netzke/sequel/attributes'
|
2
|
-
require 'netzke/sequel/combobox_options'
|
3
|
-
#require 'netzke/sequel/relation_extensions'
|
4
|
-
|
5
|
-
module Netzke
|
6
|
-
module Sequel
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
if defined? Sequel
|
11
|
-
# Extend Sequel
|
12
|
-
Sequel::Model.class_eval do
|
13
|
-
include ::Netzke::Sequel::Attributes
|
14
|
-
include ::Netzke::Sequel::ComboboxOptions
|
15
|
-
include ::Netzke::Sequel::RelationExtensions
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
@@ -1,274 +0,0 @@
|
|
1
|
-
module Netzke
|
2
|
-
module Sequel
|
3
|
-
module Attributes
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
class_attribute :netzke_declared_attr
|
8
|
-
self.netzke_declared_attr = []
|
9
|
-
|
10
|
-
class_attribute :netzke_excluded_attr
|
11
|
-
self.netzke_excluded_attr = []
|
12
|
-
|
13
|
-
class_attribute :netzke_exposed_attr
|
14
|
-
self.netzke_exposed_attr = []
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.included receiver
|
18
|
-
receiver.extend ClassMethods
|
19
|
-
end
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
def data_adapter
|
23
|
-
@data_adapter = Netzke::Basepack::DataAdapters::AbstractAdapter.adapter_class(self).new(self)
|
24
|
-
end
|
25
|
-
|
26
|
-
# mostly AR compatible for our purposes ;-)
|
27
|
-
def columns_hash
|
28
|
-
db_schema.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
|
29
|
-
end
|
30
|
-
|
31
|
-
def column_names
|
32
|
-
columns.map &:to_s
|
33
|
-
end
|
34
|
-
|
35
|
-
# human attribute name
|
36
|
-
def human_attribute_name attr
|
37
|
-
I18n.translate(attr, :scope => [:activerecord, :attributes, model_name.downcase.to_sym], :default => attr.to_s.humanize)
|
38
|
-
end
|
39
|
-
|
40
|
-
# Example:
|
41
|
-
# netzke_attribute :recent, :type => :boolean, :read_only => true
|
42
|
-
def netzke_attribute(name, options = {})
|
43
|
-
name = name.to_s
|
44
|
-
options[:attr_type] = options.delete(:type) || options.delete(:attr_type) || :string
|
45
|
-
declared_attrs = self.netzke_declared_attr.dup
|
46
|
-
# if the attr was declared already, simply merge it with the new options
|
47
|
-
existing = declared_attrs.detect{ |va| va[:name] == name }
|
48
|
-
if existing
|
49
|
-
existing.merge!(options)
|
50
|
-
else
|
51
|
-
attr_config = {:name => name}.merge(options)
|
52
|
-
# if primary_key, insert in front, otherwise append
|
53
|
-
if name == self.primary_key.to_s
|
54
|
-
declared_attrs.insert(0, attr_config)
|
55
|
-
else
|
56
|
-
declared_attrs << {:name => name}.merge(options)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
self.netzke_declared_attr = declared_attrs
|
60
|
-
end
|
61
|
-
|
62
|
-
# Exclude attributes from being picked up by grids and forms.
|
63
|
-
# Accepts an array of attribute names (as symbols).
|
64
|
-
# Example:
|
65
|
-
# netzke_expose_attributes :created_at, :updated_at, :crypted_password
|
66
|
-
def netzke_exclude_attributes(*args)
|
67
|
-
self.netzke_excluded_attr = args.map(&:to_s)
|
68
|
-
end
|
69
|
-
|
70
|
-
# Explicitly expose attributes that should be picked up by grids and forms.
|
71
|
-
# Accepts an array of attribute names (as symbols).
|
72
|
-
# Takes precedence over <tt>netzke_exclude_attributes</tt>.
|
73
|
-
# Example:
|
74
|
-
# netzke_expose_attributes :name, :role__name
|
75
|
-
def netzke_expose_attributes(*args)
|
76
|
-
self.netzke_exposed_attr = args.map(&:to_s)
|
77
|
-
end
|
78
|
-
|
79
|
-
# Returns the attributes that will be picked up by grids and forms.
|
80
|
-
def netzke_attributes
|
81
|
-
exposed = netzke_exposed_attributes
|
82
|
-
exposed ? netzke_attrs_in_forced_order(exposed) : netzke_attrs_in_natural_order
|
83
|
-
end
|
84
|
-
|
85
|
-
def netzke_attribute_hash
|
86
|
-
netzke_attributes.inject({}){ |r,a| r.merge(a[:name].to_sym => a) }
|
87
|
-
end
|
88
|
-
|
89
|
-
def netzke_exposed_attributes
|
90
|
-
exposed = self.netzke_exposed_attr
|
91
|
-
if exposed && !exposed.include?(self.primary_key.to_s)
|
92
|
-
# automatically declare primary key as a netzke attribute
|
93
|
-
netzke_attribute(self.primary_key.to_s)
|
94
|
-
exposed.insert(0, self.primary_key.to_s)
|
95
|
-
end
|
96
|
-
exposed
|
97
|
-
end
|
98
|
-
|
99
|
-
private
|
100
|
-
def netzke_attrs_in_forced_order(attrs)
|
101
|
-
attrs.collect do |attr_name|
|
102
|
-
declared = self.netzke_declared_attr.detect { |va| va[:name] == attr_name } || {}
|
103
|
-
in_columns_hash = columns_hash[attr_name] && {:name => attr_name, :attr_type => columns_hash[attr_name][:type], :default_value => columns_hash[attr_name][:default]} || {} # {:virtual => true} # if nothing found in columns, mark it as "virtual" or not?
|
104
|
-
if in_columns_hash.empty?
|
105
|
-
# If not among the model columns, it's either virtual, or an association
|
106
|
-
merged = association_attr?(attr_name) ? declared.merge!(:name => attr_name) : declared.merge(:virtual => true)
|
107
|
-
else
|
108
|
-
# .. otherwise merge with what's declared
|
109
|
-
merged = in_columns_hash.merge(declared)
|
110
|
-
end
|
111
|
-
|
112
|
-
# We didn't find it among declared, nor among the model columns, nor does it seem association attribute
|
113
|
-
merged[:name].nil? && raise(ArgumentError, "Unknown attribute '#{attr_name}' for model #{self.name}", caller)
|
114
|
-
|
115
|
-
merged
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
# Returns netzke attributes in the order of columns in the table, followed by extra declared attributes
|
120
|
-
# Detects many-to-one association columns and replaces the name of the column with association column name (Netzke style), e.g.:
|
121
|
-
#
|
122
|
-
# role_id => role__name
|
123
|
-
def netzke_attrs_in_natural_order
|
124
|
-
(
|
125
|
-
declared_attrs = self.netzke_declared_attr
|
126
|
-
|
127
|
-
column_names.map do |name|
|
128
|
-
c = {:name => name, :attr_type => columns_hash[name][:type]}
|
129
|
-
|
130
|
-
# If it's named as foreign key of some association, then it's an association column
|
131
|
-
assoc = all_association_reflections.detect { |a| a[:key].to_s == c[:name] && a[:type] == :many_to_one }
|
132
|
-
if assoc
|
133
|
-
candidates = %w{name title label} << assoc[:key].to_s
|
134
|
-
assoc_class = assoc[:class_name].constantize
|
135
|
-
assoc_method = candidates.detect{|m| ( assoc_class.instance_methods.map(&:to_s) + column_names).include?(m) }
|
136
|
-
c[:name] = "#{assoc[:name].to_s}__#{assoc_method}"
|
137
|
-
c[:attr_type] = assoc_class.columns_hash[assoc_method].try(:[], :type) || :string # when it's an instance method rather than a column, fall back to :string
|
138
|
-
end
|
139
|
-
|
140
|
-
# auto set up the default value from the column settings
|
141
|
-
c.merge!(:default_value => columns_hash[name][:default]) if columns_hash[name][:default]
|
142
|
-
|
143
|
-
# if there's a declared attr with the same name, simply merge it with what's taken from the model's columns
|
144
|
-
if declared = declared_attrs.detect{ |va| va[:name] == c[:name] }
|
145
|
-
c.merge!(declared)
|
146
|
-
declared_attrs.delete(declared)
|
147
|
-
end
|
148
|
-
c
|
149
|
-
end +
|
150
|
-
declared_attrs
|
151
|
-
).reject { |attr| self.netzke_excluded_attr.include?(attr[:name]) }
|
152
|
-
end
|
153
|
-
|
154
|
-
def association_attr?(attr_name)
|
155
|
-
!!attr_name.index("__") # probably we can't do much better than this, as we don't know at this moment if the associated model has a specific attribute, and we don't really want to find it out
|
156
|
-
end
|
157
|
-
|
158
|
-
end
|
159
|
-
|
160
|
-
# AR compatibility
|
161
|
-
def attributes
|
162
|
-
values
|
163
|
-
end
|
164
|
-
|
165
|
-
# Transforms a record to array of values according to the passed attributes
|
166
|
-
def netzke_array(attributes = self.class.netzke_attributes)
|
167
|
-
res = []
|
168
|
-
for a in attributes
|
169
|
-
next if a[:included] == false
|
170
|
-
res << value_for_attribute(a, a[:nested_attribute])
|
171
|
-
end
|
172
|
-
res
|
173
|
-
end
|
174
|
-
|
175
|
-
# convenience method to convert all netzke attributes of a model to nifty json
|
176
|
-
def netzke_json
|
177
|
-
netzke_hash.to_nifty_json
|
178
|
-
end
|
179
|
-
|
180
|
-
# Accepts both hash and array of attributes
|
181
|
-
def netzke_hash(attributes = self.class.netzke_attributes)
|
182
|
-
res = {}
|
183
|
-
for a in (attributes.is_a?(Hash) ? attributes.values : attributes)
|
184
|
-
next if a[:included] == false
|
185
|
-
res[a[:name].to_sym] = self.value_for_attribute(a, a[:nested_attribute])
|
186
|
-
end
|
187
|
-
res
|
188
|
-
end
|
189
|
-
|
190
|
-
# Fetches the value specified by an (association) attribute
|
191
|
-
# If +through_association+ is true, get the value of the association by provided method, *not* the associated record's id
|
192
|
-
# E.g., author__name with through_association set to true may return "Vladimir Nabokov", while with through_association set to false, it'll return author_id for the current record
|
193
|
-
def value_for_attribute(a, through_association = false)
|
194
|
-
v = if a[:getter]
|
195
|
-
a[:getter].call(self)
|
196
|
-
elsif respond_to?("#{a[:name]}")
|
197
|
-
send("#{a[:name]}")
|
198
|
-
elsif is_association_attr?(a)
|
199
|
-
split = a[:name].to_s.split(/\.|__/)
|
200
|
-
assoc = self.class.association_reflection(split.first.to_sym)
|
201
|
-
if through_association
|
202
|
-
split.inject(self) do |r,m| # TODO: do we really need to descend deeper than 1 level?
|
203
|
-
if r.respond_to?(m)
|
204
|
-
r.send(m)
|
205
|
-
else
|
206
|
-
logger.debug "Netzke::Basepack: Wrong attribute name: #{a[:name]}" unless r.nil?
|
207
|
-
nil
|
208
|
-
end
|
209
|
-
end
|
210
|
-
else
|
211
|
-
self.send("#{assoc[:key].to_s}")
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
# need to serialize Date and Time objects with to_s :db for compatibility with client side
|
216
|
-
# DATETIME fields in database are given as Time by Sequel
|
217
|
-
v = v.to_s(:db) if [Date, Time].include?(v.class)
|
218
|
-
v
|
219
|
-
end
|
220
|
-
|
221
|
-
# Assigns new value to an (association) attribute
|
222
|
-
def set_value_for_attribute(a, v)
|
223
|
-
v = v.to_time_in_current_zone if v.is_a?(Date) # convert Date to Time
|
224
|
-
|
225
|
-
if a[:setter]
|
226
|
-
a[:setter].call(self, v)
|
227
|
-
elsif respond_to?("#{a[:name]}=")
|
228
|
-
unless primary_key.to_s == a[:name] && v.blank? # In contrast to ActiveRecord, Sequel doesn't allow setting nil/NULL primary keys
|
229
|
-
send("#{a[:name]}=", v)
|
230
|
-
end
|
231
|
-
elsif is_association_attr?(a)
|
232
|
-
split = a[:name].to_s.split(/\.|__/)
|
233
|
-
if a[:nested_attribute]
|
234
|
-
# We want:
|
235
|
-
# set_value_for_attribute({:name => :assoc_1__assoc_2__method, :nested_attribute => true}, 100)
|
236
|
-
# =>
|
237
|
-
# self.assoc_1.assoc_2.method = 100
|
238
|
-
split.inject(self) { |r,m| m == split.last ? (r && r.send("#{m}=", v) && r.save) : r.send(m) }
|
239
|
-
else
|
240
|
-
if split.size == 2
|
241
|
-
# search for association and assign it to self
|
242
|
-
assoc = self.class.association_reflection(split.first.to_sym)
|
243
|
-
assoc_method = split.last
|
244
|
-
if assoc
|
245
|
-
if assoc[:type] == :one_to_one
|
246
|
-
assoc_instance = self.send(assoc[:name])
|
247
|
-
if assoc_instance
|
248
|
-
assoc_instance.send("#{assoc_method}=", v)
|
249
|
-
assoc_instance.save # what should we do when this fails?..
|
250
|
-
else
|
251
|
-
# what should we do in this case?
|
252
|
-
end
|
253
|
-
else
|
254
|
-
self.send("#{assoc[:key]}=", v)
|
255
|
-
end
|
256
|
-
else
|
257
|
-
logger.debug "Netzke::Basepack: Association #{assoc} is not known for class #{self.class.name}"
|
258
|
-
end
|
259
|
-
else
|
260
|
-
logger.debug "Netzke::Basepack: Wrong attribute name: #{a[:name]}"
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
protected
|
267
|
-
# Returns true if passed attribute is an "association attribute"
|
268
|
-
def is_association_attr?(a)
|
269
|
-
# maybe the check is too simplistic, but will do for now
|
270
|
-
!!a[:name].to_s.index("__")
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Netzke
|
2
|
-
module Sequel
|
3
|
-
module RelationExtensions
|
4
|
-
def self.included receiver
|
5
|
-
receiver.class_eval do
|
6
|
-
def_dataset_method :extend_with do |*params|
|
7
|
-
scope = params.shift
|
8
|
-
case scope.class.name
|
9
|
-
when "Symbol" # model's scope
|
10
|
-
self.send(scope, *params)
|
11
|
-
when "String" # SQL query or SQL query with params (e.g. ["created_at < ?", 1.day.ago])
|
12
|
-
params.empty? ? self.where(scope) : self.where([scope, *params])
|
13
|
-
when "Array"
|
14
|
-
self.extend_with(*scope)
|
15
|
-
when "Hash" # conditions hash
|
16
|
-
self.where(scope)
|
17
|
-
when "ActiveSupport::HashWithIndifferentAccess" # conditions hash
|
18
|
-
self.where(scope)
|
19
|
-
when "Proc" # receives a relation, must return a relation
|
20
|
-
scope.call(self)
|
21
|
-
else
|
22
|
-
raise ArgumentError, "Wrong parameter type for ActiveRecord::Relation#extend_with"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Non-destructively extends itself whith a hash of double-underscore'd conditions,
|
27
|
-
# where the last part "__" is MetaWhere operator (which is required), e.g.:
|
28
|
-
# {:role__name__like => "%admin"}
|
29
|
-
|
30
|
-
def_dataset_method :extend_with_netzke_conditions do |cond|
|
31
|
-
cond.each_pair.inject(self) do |r, (k,v)|
|
32
|
-
assoc, method, *operator = k.to_s.split("__")
|
33
|
-
operator.empty? ? r.where(assoc.to_sym.send(method) => v) : r.where(assoc.to_sym => {method.to_sym.send(operator.last) => v}).joins(assoc.to_sym)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
data/locales/zh-cn.yml
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
zh-cn:
|
2
|
-
netzke:
|
3
|
-
basepack:
|
4
|
-
grid_panel:
|
5
|
-
actions:
|
6
|
-
add: "增加"
|
7
|
-
edit: "编辑"
|
8
|
-
del: "删除"
|
9
|
-
apply: "保存"
|
10
|
-
add_in_form: "表格添加"
|
11
|
-
edit_in_form: "表格编辑"
|
12
|
-
search: "搜索"
|
13
|
-
|
14
|
-
deleted_n_records: "删除了 %{n} 条记录"
|
15
|
-
cannot_delete: "本用户无权限删除"
|
16
|
-
confirmation: "确认"
|
17
|
-
are_you_sure: "确认改操作吗?"
|
18
|
-
|
19
|
-
record_form_window:
|
20
|
-
actions:
|
21
|
-
ok: "确定"
|
22
|
-
cancel: "取消"
|
23
|
-
|
24
|
-
search_window:
|
25
|
-
title: "高级搜索"
|
26
|
-
actions:
|
27
|
-
search: "搜索"
|
28
|
-
cancel: "取消"
|
29
|
-
|
30
|
-
form_panel:
|
31
|
-
actions:
|
32
|
-
edit: "编辑"
|
33
|
-
edit_tooltip: "编辑"
|
34
|
-
cancel: "取消"
|
35
|
-
cancel_tooltip: "取消编辑"
|
36
|
-
apply: "保存"
|
37
|
-
apply_tooltip: "保存修改"
|
38
|
-
|
39
|
-
search_panel:
|
40
|
-
presets: "呈现"
|
41
|
-
equals: "等于"
|
42
|
-
greater_than: "大于"
|
43
|
-
less_than: "小于"
|
44
|
-
contains: "包含"
|
45
|
-
matches: "匹配"
|
46
|
-
date_equals: "On"
|
47
|
-
before: "之前"
|
48
|
-
after: "之后"
|
49
|
-
|
50
|
-
paging_form_panel:
|
51
|
-
actions:
|
52
|
-
search: "搜索"
|
53
|
-
search_tooltip: "高级搜索"
|
54
|
-
|
55
|
-
query_builder:
|
56
|
-
presets: "预设"
|
57
|
-
preset_saved: "预设成功保存"
|
58
|
-
preset_deleted: "预设成功删除"
|
59
|
-
overwrite_confirm: "确定覆盖已存在的预设 '{0}' 吗?"
|
60
|
-
overwrite_confirm_title: "覆盖已存在预设"
|
61
|
-
delete_confirm: "确定删除预设 '{0}' 吗?"
|
62
|
-
delete_confirm_title: "删除预设确认"
|
63
|
-
actions:
|
64
|
-
clear_all: "清除"
|
65
|
-
clear_all_tooltip: "清除所有条件"
|
66
|
-
reset: "重置"
|
67
|
-
reset_tooltip: "重置条件值"
|
68
|
-
save_preset: "保存预设"
|
69
|
-
save_preset_tooltip: "保存预设"
|
70
|
-
delete_preset: "删除预设"
|
71
|
-
delete_preset_tooltip: "删除预设"
|
72
|
-
apply: "保存"
|
73
|
-
apply_tooltip: "应用条件"
|
74
|
-
|
75
|
-
|
76
|
-
formats:
|
77
|
-
date: Y-m-d
|
78
|
-
datetime: Y-m-d H:i:s
|
79
|
-
time: H:i:s
|