awesome_form_attributes 0.0.3 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +10 -0
- data/awesome_form_attributes.gemspec +1 -1
- data/lib/awesome_form_attributes.rb +6 -1
- data/lib/awesome_form_attributes/config.rb +48 -0
- data/lib/awesome_form_attributes/helpers.rb +40 -4
- data/lib/awesome_form_attributes/shared_helper.rb +25 -0
- data/lib/generators/awesome_form_attributes/config_generator.rb +12 -0
- data/lib/generators/awesome_form_attributes/templates/awesome_form_attributes.rb +7 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3368dbc94707dd8c9ff71d7efeab198f77ab6e8c
|
4
|
+
data.tar.gz: 7ae715111503407ebee564b530b6ed6215571b4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec421ddfe96252234601ddbb280be7b8c959d2aca4c97e67a3f65b1e5079893910075bc0d7afe009c39b21941867c798ff4320718da457952e9bb48809df92e5
|
7
|
+
data.tar.gz: be8bc505835a3a87d3acc10f9ea97f559118147028ca96e5f89869347e9ad7dc0a062855a429a3c0267a389ea5a77107a398fef3c2d1162db421ac6bc752d2ca
|
data/Rakefile
ADDED
@@ -1,7 +1,12 @@
|
|
1
1
|
require "rails"
|
2
|
+
require "pry"
|
3
|
+
require 'awesome_form_attributes/config'
|
2
4
|
require "awesome_form_attributes/version"
|
3
5
|
require "awesome_form_attributes/setting"
|
4
6
|
require "awesome_form_attributes/helpers"
|
7
|
+
require "awesome_form_attributes/shared_helper"
|
5
8
|
require "awesome_form_attributes/used_attr_columns"
|
6
9
|
|
7
|
-
ActiveRecord::Base.send
|
10
|
+
ActiveRecord::Base.send(:include, UsedAttrColumns) # if defined?(ActiveRecord::Base)
|
11
|
+
ActionView::Helpers.send(:include, SharedHelper)
|
12
|
+
binding.pry
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module AwesomeFormAttributes
|
4
|
+
def self.configure(&block)
|
5
|
+
yield @config ||= AwesomeFormAttributes::Configuration.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.config
|
9
|
+
@config
|
10
|
+
end
|
11
|
+
|
12
|
+
class Configuration
|
13
|
+
include ActiveSupport::Configurable
|
14
|
+
config_accessor :default_tag
|
15
|
+
config_accessor :text_area_words
|
16
|
+
config_accessor :select_words
|
17
|
+
config_accessor :boolean_words
|
18
|
+
config_accessor :file_words
|
19
|
+
config_accessor :text_area_reg
|
20
|
+
config_accessor :select_reg
|
21
|
+
config_accessor :boolean_reg
|
22
|
+
config_accessor :file_reg
|
23
|
+
|
24
|
+
def param_name
|
25
|
+
config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
|
26
|
+
end
|
27
|
+
|
28
|
+
writer, line = 'def param_name=(value); config.param_name = value; end', __LINE__
|
29
|
+
singleton_class.class_eval writer, __FILE__, line
|
30
|
+
class_eval writer, __FILE__, line
|
31
|
+
end
|
32
|
+
|
33
|
+
configure do |config|
|
34
|
+
config.default_tag = "text_field"
|
35
|
+
config.text_area_words = %w(描述 简介 介绍)
|
36
|
+
config.select_words = %w(select 类型)
|
37
|
+
config.boolean_words = %w(是否)
|
38
|
+
config.file_words = %w(文件)
|
39
|
+
class << config
|
40
|
+
%w(text_area select boolean file).each do |c|
|
41
|
+
define_method(:"#{c}_reg") do
|
42
|
+
/#{config.send(:"#{c}_words").join("|")}/
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -1,13 +1,11 @@
|
|
1
|
+
# require File.expand_path("../shared_helper",__FILE__)
|
1
2
|
module ActionView
|
2
3
|
module Helpers
|
4
|
+
# ApplicationHelper.send(:include,SharedHelper) if defined?(ApplicationHelper)
|
3
5
|
def boolean_collection
|
4
6
|
[["是", 1], ["否", 0]]
|
5
7
|
end
|
6
8
|
|
7
|
-
def klass
|
8
|
-
controller_name.classify.constantize
|
9
|
-
end
|
10
|
-
|
11
9
|
def has_displayed_names?
|
12
10
|
I18n.t(klass.displayed_columns_local_path).is_a?Hash
|
13
11
|
end
|
@@ -18,5 +16,43 @@ module ActionView
|
|
18
16
|
return displayed_name if has_displayed_names? && !(displayed_name =~ /translation missing/)
|
19
17
|
basic_name
|
20
18
|
end
|
19
|
+
|
20
|
+
def table_th_columns(wrapper = :th, style = {})
|
21
|
+
join_content(wrapper, style) {|a| localize_attr(a)}
|
22
|
+
end
|
23
|
+
|
24
|
+
def values_for_columns(obj, wrapper = :td, style = {})
|
25
|
+
join_content(wrapper, style) {|a| obj.send(a)}
|
26
|
+
end
|
27
|
+
|
28
|
+
def join_content(wrapper = :td, style = {}, &lamb)
|
29
|
+
content = klass.displayed_columns.map do |a|
|
30
|
+
content_tag(wrapper, lamb.call(a), style)
|
31
|
+
end.join
|
32
|
+
raw content
|
33
|
+
end
|
34
|
+
|
35
|
+
def awesome_fileds(a, f, opts = {})
|
36
|
+
cs = AwesomeFormAttributes.config.config
|
37
|
+
text_field = cs.delete(:default_tag)
|
38
|
+
title = localize_attr(a)
|
39
|
+
cur_tag_hash = cs.select {|k, v| title =~ /#{v.join("|")}/}.presence || {text_field: ""}
|
40
|
+
cur_tag = cur_tag_hash.keys.first.to_s.gsub("_words", "")
|
41
|
+
opts = default_styles_for(cur_tag, opts)
|
42
|
+
return f.send(:check_box, a, opts) if cur_tag == 'boolean'
|
43
|
+
f.send(cur_tag, a, opts)
|
44
|
+
end
|
45
|
+
|
46
|
+
def default_styles_for(tag, opts = {})
|
47
|
+
{
|
48
|
+
text_area: {rows: 6, cols: 50},
|
49
|
+
text_field: {},
|
50
|
+
file: {},
|
51
|
+
select: {},
|
52
|
+
check_box: {},
|
53
|
+
boolean: {}
|
54
|
+
}[tag.to_sym].deep_merge(opts) rescue opts
|
55
|
+
end
|
56
|
+
|
21
57
|
end
|
22
58
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module SharedHelper
|
2
|
+
def klass
|
3
|
+
controller_name.classify.constantize
|
4
|
+
end
|
5
|
+
|
6
|
+
def obj_str
|
7
|
+
controller_name.singularize
|
8
|
+
end
|
9
|
+
|
10
|
+
def single_obj
|
11
|
+
instance_variable_get("@#{obj_str}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def collections
|
15
|
+
instance_variable_get("@#{controller_name}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def single_obj=(obj)
|
19
|
+
instance_variable_set("@#{obj_str}", obj)
|
20
|
+
end
|
21
|
+
|
22
|
+
def collections=(objs)
|
23
|
+
instance_variable_set("@#{controller_name}", objs)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module AwesomeFormAttributes
|
2
|
+
module Generators
|
3
|
+
class ConfigGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
|
6
|
+
def copy_config_file
|
7
|
+
template 'awesome_form_attributes.rb', 'config/initializers/awesome_form_attributes.rb'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_form_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zhimeng Sun
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -33,12 +33,17 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- .gitignore
|
35
35
|
- README.md
|
36
|
+
- Rakefile
|
36
37
|
- awesome_form_attributes.gemspec
|
37
38
|
- lib/awesome_form_attributes.rb
|
39
|
+
- lib/awesome_form_attributes/config.rb
|
38
40
|
- lib/awesome_form_attributes/helpers.rb
|
39
41
|
- lib/awesome_form_attributes/setting.rb
|
42
|
+
- lib/awesome_form_attributes/shared_helper.rb
|
40
43
|
- lib/awesome_form_attributes/used_attr_columns.rb
|
41
44
|
- lib/awesome_form_attributes/version.rb
|
45
|
+
- lib/generators/awesome_form_attributes/config_generator.rb
|
46
|
+
- lib/generators/awesome_form_attributes/templates/awesome_form_attributes.rb
|
42
47
|
homepage: https://github.com/zhimengSun/awesome_form_attributes
|
43
48
|
licenses: []
|
44
49
|
metadata: {}
|
@@ -58,8 +63,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
63
|
version: '0'
|
59
64
|
requirements: []
|
60
65
|
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
66
|
+
rubygems_version: 2.0.3
|
62
67
|
signing_key:
|
63
68
|
specification_version: 4
|
64
69
|
summary: Easy way to write attributes for DB based Object
|
65
70
|
test_files: []
|
71
|
+
has_rdoc:
|