awesome_form_attributes 0.0.3 → 1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3930f76f028727104a329fdfb0b4fe2307d27ee8
4
- data.tar.gz: c6f537cf775ef060de8cbd4c9e3e3bf536ad93aa
3
+ metadata.gz: 3368dbc94707dd8c9ff71d7efeab198f77ab6e8c
4
+ data.tar.gz: 7ae715111503407ebee564b530b6ed6215571b4d
5
5
  SHA512:
6
- metadata.gz: fb1d4eba245c48caf90e7b56d229b742de0d7d0cdc55261ecd0ef5722372ca8cbf33eda5c4459f3aced05d8b4c346b9ddee5d823aa4f3442c26e4abd047f5b4b
7
- data.tar.gz: 5ea01fa6348d31e49edfe537c0fe10dc453e301bedc63a0e3898ad025b9b2a93a048893fce6bc1e5588614a3453232cb3f2ee28c2b0b40d1f2f4b67bffeeb027
6
+ metadata.gz: ec421ddfe96252234601ddbb280be7b8c959d2aca4c97e67a3f65b1e5079893910075bc0d7afe009c39b21941867c798ff4320718da457952e9bb48809df92e5
7
+ data.tar.gz: be8bc505835a3a87d3acc10f9ea97f559118147028ca96e5f89869347e9ad7dc0a062855a429a3c0267a389ea5a77107a398fef3c2d1162db421ac6bc752d2ca
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ require "bundler/gem_tasks"
3
+
4
+ namespace :awesome_form_attributes do
5
+ desc "generate config files"
6
+ task :config do
7
+ puts Time.now
8
+ end
9
+ end
10
+
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'awesome_form_attributes'
5
- s.version = '0.0.3'
5
+ s.version = '1.0'
6
6
  s.authors = ['Zhimeng Sun']
7
7
  s.email = ['zhimengSun@gmail.com']
8
8
  s.homepage = 'https://github.com/zhimengSun/awesome_form_attributes'
@@ -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 :include, UsedAttrColumns
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
+
@@ -0,0 +1,7 @@
1
+ AwesomeFormAttributes.configure do |config|
2
+ # config.default_tag = "text_field"
3
+ # config.text_area_words += []
4
+ # config.select_words += []
5
+ # config.boolean_words += []
6
+ # config.file_words += []
7
+ end
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: 0.0.3
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: 2014-11-07 00:00:00.000000000 Z
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.4.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: